I ran into an interesting issue today. I'm working on an app where we have file uploads, and we want to implement a progress bar. The app is written using React/Redux/Redux-Observable. I want to dispatch actions for upload progress. Here's what I did to implement it:
withProgress(method, url, body = {}, headers = {}) {
const progressSubscriber = Subscriber.create();
return {
Subscriber: progressSubscriber,
Request: this.ajax({ url, method, body, headers, progressSubscriber }),
};
}
I have a class that I use to make all my ajax requests. this.ajax
calls Observable.ajax
with the passed in parameters.
export const blobStorageUploadEpic = (action$) => {
return action$.ofType(a.BLOB_STORAGE_UPLOAD)
.mergeMap(({ payload }) => {
const { url, valetKey, blobId, blobData, contentType } = payload;
const { Subscriber, Request } = RxAjax.withProgress('PUT', `${url}?${valetKey}`, blobData, {
'x-ms-blob-type': 'BlockBlob',
'Content-Type': contentType,
});
const requestObservable = Request
.map(() => ({ type: a.BLOB_STORAGE_UPLOAD_SUCCESS, payload: { blobId } }))
.catch((err) => Observable.of({ type: a.BLOB_STORAGE_UPLOAD_FAILURE, err }));
return Observable.fromSubscriber(Subscriber)
.map((e) => ({ percentage: (e.loaded / e.total) * 100 }))
.map((percentage) => ({ type: a.BLOB_STORAGE_UPLOAD_PROGRESS, payload: { percentage} }))
.merge(requestObservable);
});
};
This is my epic. I get the subscriber back and I wrote a custom static method of Observable
to take in a subscriber. I then merge that with the Request
(which is an Observable
).
Observable.fromSubscriber = function fromSubscriber(externalSubscriber) {
return Observable.create((subscriber) => {
externalSubscriber.next = (val) => subscriber.next(val);
externalSubscriber.error = (err) => subscriber.error(err);
externalSubscriber.complete = () => subscriber.complete();
});
};
Finally, here is the custom static method I wrote on Observable
. I wrote this for two reasons. 1. As an example for anyone else dealing with a similar problem (I spent a lot of time trying to figure out how to make an Observable
from a Subscriber
before writing my own) and 2. To ask whether this is the best way to accomplish this goal. rxjs
is deep, and I figure that there's an existing way to do this, but I just couldn't find it.