Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ajax): Add option for streaming progress #6001

Merged

Conversation

benlesh
Copy link
Member

@benlesh benlesh commented Feb 7, 2021

Edit: I've moved the spec changes to another PR that should be reviewed first here: #6002

  • Fixes a bunch of tests that were slightly off or had bad assumptions
  • Adds two new features: includeUploadProgress and includeDownloadProgress that will add additional events to the output stream before the event that is usually emitted.

Resolves #2833

NOTE: We still need to add an example of usage.

@benlesh
Copy link
Member Author

benlesh commented Feb 7, 2021

There will be a follow up refactor to make this smaller as well.

@benlesh
Copy link
Member Author

benlesh commented Feb 8, 2021

Blocked on merging #6002

@benlesh benlesh force-pushed the feat-issue-2833-ajax-with-progress branch from b5edbe3 to 35169c6 Compare February 8, 2021 05:27
api_guard/dist/types/ajax/index.d.ts Show resolved Hide resolved

expect(results).to.deep.equal([
{
type: 'download_loadstart',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is this an enum? Can we export one if not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it. I think that we have to do enum and not const enum, and therefore we'd end up exporting an object. That doesn't really mesh with how events work elsewhere. I'll consider this a vote in favor of an enum, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, my vote would be against exporting an enum. If something is to be exported, I'd vote for a type that's a union of literals, rather than an enum.

src/internal/ajax/ajax.ts Show resolved Hide resolved
@@ -1124,10 +1032,365 @@ describe('ajax', () => {
});
});
});

describe('with includeDownloadProgress', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests that have a binary type? Not for this PR but may be something to consider adding

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binary, as in streaming data? XHR doesn't really support that. Fetch does, though.

If you mean "binary" as in a binary data type like a "Content-Type": "image/png" or something... All of that is handled directly by the XHR we're using under the hood, and I'm not sure how I'd test that without an end-to-end test of some sort, which we aren't quite set up for. Right now we're mocking the XHR. I could test that within the confines of our mocks though, I guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of that said, it should "just work", because in the event of binary data like that, the XHR in the browser will have a responseType of "arraybuffer", and the response itself would be something like Uint8Array, which is used directly in our AjaxResponse, without any additional processing.

@benlesh benlesh removed the blocked label Feb 9, 2021
- Adds configuration for `includeDownloadProgress` and `includeUploadProgress` that will add streaming download and upload events (respectively) to the resulting observable.
- Removes two tests that are no longer relevant.
- Adds some additional documentation.
- Updates ajax tests to support EventTarget APIs on the mock XHR impl.
- Attempts to reduce the size of the ajax implementation.
@benlesh benlesh force-pushed the feat-issue-2833-ajax-with-progress branch from 35169c6 to c7ed50a Compare February 9, 2021 05:17
@@ -948,85 +948,6 @@ describe('ajax', () => {
delete root.XMLHttpRequest.prototype.ontimeout;
});

it('should work fine when XMLHttpRequest onprogress property is monkey patched', function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are no longer relevant, as they were only for when we were using the property method versions of event handlers like xhr.onprogress. Now we're using xhr.addEventListener('progress', ...), so it's a non-issue. Since Zone.js is patching EventTarget, this should "just work". Also, hopefully in the not-so-distant future, Zone.js goes away.

@benlesh benlesh merged commit 873e52d into ReactiveX:master Feb 9, 2021
* If both this and {@link includeDownloadProgress} are `false`, then only the {@link AjaxResponse} will
* be emitted from the resulting observable.
*/
includeUploadProgress?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this option is true, shouldn't ajax return Observable<AjaxUploadProgressEvents | AjaxDownloadCompleteEvent | AjaxResponse<T>> instead of Observable<AjaxResponse<T>>? Likewise for includeDownloadProgress and AjaxProgressEvents. /cc @benlesh

If that makes sense I can try to send a PR or file an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rx.Observable.ajax onProgress only set for Upload
4 participants