react-native-firebase/lib/modules/storage/task.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-02-14 11:31:42 +00:00
export const UPLOAD_TASK = 'upload';
export const DOWNLOAD_TASK = 'download';
/**
* @url https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask
*/
export default class StorageTask {
constructor(type: string, promise, storageRef) {
this.type = type;
this.ref = storageRef;
this.promise = promise;
this.storage = storageRef.storage;
// 'proxy' original promise
this.then = promise.then;
this.catch = promise.catch;
}
/**
*
* @param event
* @param nextOrObserver
* @param error
* @param complete
* @returns {function()}
*/
on(event = 'state_changed', nextOrObserver, error, complete) {
if (nextOrObserver) this.storage._addListener(this.path, 'state_changed', nextOrObserver);
if (error) this.storage._addListener(this.path, `${this.type}_failure`, error);
if (complete) this.storage._addListener(this.path, `${this.type}_success`, complete);
// off
// todo support add callback syntax as per https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#on
return () => {
if (nextOrObserver) this.storage._removeListener(this.path, 'state_changed', nextOrObserver);
if (error) this.storage._removeListener(this.path, `${this.type}_failure`, error);
if (complete) this.storage._removeListener(this.path, `${this.type}_success`, complete);
};
}
pause() {
// todo
throw new Error('.pause() is not currently supported by react-native-firebase');
}
resume() {
// todo
throw new Error('.resume() is not currently supported by react-native-firebase');
}
cancel() {
// todo
throw new Error('.cancel() is not currently supported by react-native-firebase');
}
}