MCPcopy
hub / github.com/angular/angularfire / fromTask

Function fromTask

src/compat/storage/observable/fromTask.ts:10–39  ·  view source on GitHub ↗
(task: UploadTask)

Source from the content-addressed store, hash-verified

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
10export 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}

Callers 2

createUploadTaskFunction · 0.90
storage.spec.tsFile · 0.90

Calls 4

progressFunction · 0.85
thenMethod · 0.80
completeFunction · 0.70
errorFunction · 0.70

Tested by

no test coverage detected