[js][storage] switch to native promises

This commit is contained in:
Salakar 2017-03-22 19:46:08 +00:00
parent dfa72c03f9
commit 00889e1a69
1 changed files with 7 additions and 37 deletions

View File

@ -1,7 +1,6 @@
/* @flow */ /* @flow */
import { NativeModules } from 'react-native'; import { NativeModules } from 'react-native';
import { promisify } from '../../utils';
import { ReferenceBase } from './../base'; import { ReferenceBase } from './../base';
import StorageTask, { UPLOAD_TASK, DOWNLOAD_TASK } from './task'; import StorageTask, { UPLOAD_TASK, DOWNLOAD_TASK } from './task';
import Storage from './'; import Storage from './';
@ -21,17 +20,7 @@ export default class StorageReference extends ReferenceBase {
return this.path; return this.path;
} }
// todo add support for method // todo return full gs://bucket/path
put(data: Object, metadata: Object = {}): /*UploadTask*/Promise<Object> {
throw new Error('put() is not currently supported by react-native-firebase');
}
// todo add support for method
putString(data: string, format: String, metadata: Object = {}): /*UploadTask*/Promise<Object> {
throw new Error('putString() is not currently supported by react-native-firebase');
}
// todo eturn full gs://bucket/path
toString(): String { toString(): String {
return this.path; return this.path;
} }
@ -50,11 +39,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*} * @returns {Promise.<T>|*}
*/ */
delete(): Promise<*> { delete(): Promise<*> {
return promisify('delete', FirebaseStorage)(this.path) return FirebaseStorage.delete(this.path);
.catch((error) => {
this.log.error('Error deleting reference ', this.path, '. Error: ', error);
throw error;
});
} }
/** /**
@ -62,12 +47,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*} * @returns {Promise.<T>|*}
*/ */
getDownloadURL(): Promise<String> { getDownloadURL(): Promise<String> {
this.log.debug('getDownloadURL(', this.path, ')'); return FirebaseStorage.getDownloadURL(this.path);
return promisify('getDownloadURL', FirebaseStorage)(this.path)
.catch((err) => {
this.log.error('Error downloading URL for ', this.path, '. Error: ', err);
throw err;
});
} }
/** /**
@ -75,11 +55,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*} * @returns {Promise.<T>|*}
*/ */
getMetadata(): Promise<Object> { getMetadata(): Promise<Object> {
return promisify('getMetadata', FirebaseStorage)(this.path) return FirebaseStorage.getMetadata(this.path);
.catch((error) => {
this.log.error('Error getting metadata for ', this.path, '. Error: ', error);
throw error;
});
} }
/** /**
@ -88,11 +64,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*} * @returns {Promise.<T>|*}
*/ */
updateMetadata(metadata: Object = {}): Promise<Object> { updateMetadata(metadata: Object = {}): Promise<Object> {
return promisify('updateMetadata', FirebaseStorage)(this.path, metadata) return FirebaseStorage.updateMetadata(this.path, metadata);
.catch((error) => {
this.log.error('Error updating metadata for ', this.path, '. Error: ', error);
throw error;
});
} }
/** /**
@ -101,8 +73,7 @@ export default class StorageReference extends ReferenceBase {
* @return {Promise} * @return {Promise}
*/ */
downloadFile(filePath: string): Promise<Object> { downloadFile(filePath: string): Promise<Object> {
this.log.debug('download(', this.path, ') -> ', filePath); return new StorageTask(DOWNLOAD_TASK, FirebaseStorage.downloadFile(this.path, filePath), this);
return new StorageTask(DOWNLOAD_TASK, promisify('downloadFile', FirebaseStorage)(this.path, filePath), this);
} }
/** /**
@ -113,7 +84,6 @@ export default class StorageReference extends ReferenceBase {
*/ */
putFile(filePath: Object, metadata: Object = {}): Promise<Object> { putFile(filePath: Object, metadata: Object = {}): Promise<Object> {
const _filePath = filePath.replace('file://', ''); const _filePath = filePath.replace('file://', '');
this.log.debug('putFile(', _filePath, ') -> ', this.path); return new StorageTask(UPLOAD_TASK, FirebaseStorage.putFile(this.path, _filePath, metadata), this);
return new StorageTask(UPLOAD_TASK, promisify('putFile', FirebaseStorage)(this.path, _filePath, metadata), this);
} }
} }