(task: UploadTask)
| 8 | // Things aren't working great, I'm having to put in a lot of work-arounds for what |
| 9 | // appear to be Firebase JS SDK bugs https://github.com/firebase/firebase-js-sdk/issues/4158 |
| 10 | export function fromTask(task: UploadTask) { |
| 11 | return new Observable<UploadTaskSnapshot>(subscriber => { |
| 12 | const progress = (snap: UploadTaskSnapshot) => subscriber.next(snap); |
| 13 | const error = e => subscriber.error(e); |
| 14 | const complete = () => subscriber.complete(); |
| 15 | // emit the current snapshot, so they don't have to wait for state_changes |
| 16 | // to fire next... this is stale if the task is no longer running :( |
| 17 | progress(task.snapshot); |
| 18 | const unsub = task.on('state_changed', progress); |
| 19 | // it turns out that neither task snapshot nor 'state_changed' fire the last |
| 20 | // snapshot before completion, the one with status 'success" and 100% progress |
| 21 | // so let's use the promise form of the task for that |
| 22 | task.then(snapshot => { |
| 23 | progress(snapshot); |
| 24 | complete(); |
| 25 | }, e => { |
| 26 | // TODO investigate, again this is stale, we never fire a canceled or error it seems |
| 27 | progress(task.snapshot); |
| 28 | error(e); |
| 29 | }); |
| 30 | // on's type if Function, rather than () => void, need to wrap |
| 31 | return function unsubscribe() { |
| 32 | unsub(); |
| 33 | }; |
| 34 | }).pipe( |
| 35 | // deal with sync emissions from first emitting `task.snapshot`, this makes sure |
| 36 | // that if the task is already finished we don't emit the old running state |
| 37 | debounceTime(0) |
| 38 | ); |
| 39 | } |
no test coverage detected