Skip to content

Commit

Permalink
fix(ajax): upload progress event is not set correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 19, 2016
1 parent 88f56fb commit a2d1ff2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
29 changes: 29 additions & 0 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,35 @@ describe('Observable.ajax', () => {
expect(complete).to.be.true;
});

it('should emit progress event when progressSubscriber is specified', function() {
const spy = sinon.spy();
const progressSubscriber = (<any>{
next: spy,
error: () => {
// noop
},
complete: () => {
// noop
}
});

Rx.Observable.ajax({
url: '/flibbertyJibbet',
progressSubscriber
})
.subscribe();

const request = MockXMLHttpRequest.mostRecent;

request.respondWith({
'status': 200,
'contentType': 'application/json',
'responseText': JSON.stringify({})
}, 3);

expect(spy).to.be.calledThrice;
});

});

it('should work fine when XMLHttpRequest onreadystatechange property is monkey patched', function() {
Expand Down
11 changes: 5 additions & 6 deletions src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
} else {
this.xhr = xhr;

// open XHR first
// set up the events before open XHR
this.setupEvents(xhr, request);
// open XHR
let result: any;
if (user) {
result = tryCatch(xhr.open).call(xhr, method, url, async, user, password);
Expand All @@ -244,9 +246,6 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
// set headers
this.setHeaders(xhr, headers);

// now set up the events
this.setupEvents(xhr, request);

// finally send the request
result = body ? tryCatch(xhr.send).call(xhr, body) : tryCatch(xhr.send).call(xhr);
if (result === errorObject) {
Expand Down Expand Up @@ -304,14 +303,14 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
(<any>xhrTimeout).request = request;
(<any>xhrTimeout).subscriber = this;
(<any>xhrTimeout).progressSubscriber = progressSubscriber;
if (xhr.upload && 'withCredentials' in xhr && root.XDomainRequest) {
if (xhr.upload && 'withCredentials' in xhr) {
if (progressSubscriber) {
let xhrProgress: (e: ProgressEvent) => void;
xhrProgress = function(e: ProgressEvent) {
const { progressSubscriber } = (<any>xhrProgress);
progressSubscriber.next(e);
};
xhr.onprogress = xhrProgress;
xhr.upload.onprogress = xhrProgress;
(<any>xhrProgress).progressSubscriber = progressSubscriber;
}
let xhrError: (e: ErrorEvent) => void;
Expand Down

0 comments on commit a2d1ff2

Please sign in to comment.