Skip to content

Commit

Permalink
use RxFire's fromTask (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuleatt authored Sep 9, 2021
1 parent 8596428 commit a359de4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
6 changes: 3 additions & 3 deletions docs/reference/modules/storage.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 2 additions & 24 deletions src/storage.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
import * as React from 'react';
import { getDownloadURL } from 'rxfire/storage';
import { Observable } from 'rxjs';
import { getDownloadURL, fromTask } from 'rxfire/storage';
import { ReactFireOptions, useObservable, ObservableStatus, useStorage } from './';
import { useSuspenseEnabledFromConfigAndContext } from './firebaseApp';
import { ref } from 'firebase/storage';

import type { UploadTask, UploadTaskSnapshot, StorageReference, FirebaseStorage } from 'firebase/storage';

/**
* modified version of rxFire's _fromTask
*
* @param task
*/
function _fromTask(task: UploadTask) {
return new Observable<UploadTaskSnapshot>((subscriber) => {
const progress = (snap: UploadTaskSnapshot) => {
return subscriber.next(snap);
};
const error = (e: any) => subscriber.error(e);
const complete = () => {
return subscriber.complete();
};
task.on('state_changed', progress, error, complete);

// I REMOVED THE UNSUBSCRIBE RETURN BECAUSE IT CANCELS THE UPLOAD
// https://github.com/firebase/firebase-js-sdk/issues/1659
});
}

/**
* Subscribe to the progress of a storage task
*
Expand All @@ -37,7 +15,7 @@ function _fromTask(task: UploadTask) {
*/
export function useStorageTask<T = unknown>(task: UploadTask, ref: StorageReference, options?: ReactFireOptions<T>): ObservableStatus<UploadTaskSnapshot | T> {
const observableId = `storage:task:${ref.toString()}`;
const observable$ = _fromTask(task);
const observable$ = fromTask(task);

return useObservable(observableId, observable$, options);
}
Expand Down
6 changes: 3 additions & 3 deletions test/storage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Storage', () => {

const uploadTask = uploadBytesResumable(testFileRef, someBytes);

const { result } = renderHook(() => useStorageTask<UploadTaskSnapshot>(uploadTask, testFileRef), { wrapper: Provider });
const { result, waitFor } = renderHook(() => useStorageTask<UploadTaskSnapshot>(uploadTask, testFileRef), { wrapper: Provider });

const uploadTaskSnapshots: Array<UploadTaskSnapshot> = [];
let hasUploadTaskCompleted = false;
Expand All @@ -44,8 +44,8 @@ describe('Storage', () => {
}
);

await actOnHooks(async () => {
await uploadTask.then();
await waitFor(() => {
return hasUploadTaskCompleted && result.current.isComplete;
});

expect(result.error).toEqual(uploadTaskError);
Expand Down

0 comments on commit a359de4

Please sign in to comment.