Skip to content

Instantly share code, notes, and snippets.

@pascalwhoop
Last active March 20, 2017 11:45
Show Gist options
  • Save pascalwhoop/bfc291d060cdc7a4dca27c033767e046 to your computer and use it in GitHub Desktop.
Save pascalwhoop/bfc291d060cdc7a4dca27c033767e046 to your computer and use it in GitHub Desktop.
//imports ...
export class CustomBrowserXhr extends BrowserXhr {
private _observable: Observable<XhrEvent>;
private _subscriber: Subscriber<XhrEvent>;
constructor() {
super();
this._observable = Observable.create(subscriber => {
this._subscriber = subscriber;
}).share();
}
get observable(): Observable<XhrEvent> {
return this._observable;
}
build(): any {
let xhr = super.build();
if (!this._subscriber) return xhr;
//at the beginning, we create an event that notifies an opening of a connection
this._subscriber.next({type: 'open', event: {}});
xhr.onprogress = (event) => {
this._subscriber.next({type: 'progress', event: event});
};
xhr.onload = (event) => {
this._subscriber.next({type: 'load', event: event});
};
xhr.onerror = (event) => {
this._subscriber.next({type: 'error', event: event});
};
xhr.onabort = (event) => {
this._subscriber.next({type: 'abort', event: event});
};
return xhr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment