more work on multiple app initialization

This commit is contained in:
Salakar
2017-06-30 17:23:32 +01:00
parent 80ae8425ce
commit ec22e510bc
25 changed files with 493 additions and 455 deletions

View File

@@ -1,39 +1,29 @@
/* @flow */
import { NativeModules, NativeEventEmitter } from 'react-native';
import { NativeModules } from 'react-native';
import { Base } from './../base';
import { nativeSDKMissing } from './../../utils';
import StorageRef from './reference';
import ModuleBase from './../../utils/ModuleBase';
const FirebaseStorage = NativeModules.RNFirebaseStorage;
const FirebaseStorageEvt = FirebaseStorage && new NativeEventEmitter(FirebaseStorage);
type StorageOptionsType = {
storageBucket?: ?string,
};
export default class Storage extends Base {
export default class Storage extends ModuleBase {
/**
*
* @param firebase
* @param firebaseApp
* @param options
*/
constructor(firebase: Object, options: StorageOptionsType = {}) {
super(firebase, options);
if (!FirebaseStorage) {
return nativeSDKMissing('storage');
}
constructor(firebaseApp: Object, options: Object = {}) {
super(firebaseApp, options, 'Storage', true);
this._subscriptions = {};
this.subscriptions = {};
this.successListener = FirebaseStorageEvt.addListener(
this._successListener = this._eventEmitter.addListener(
'storage_event',
event => this._handleStorageEvent(event)
event => this._handleStorageEvent(event),
);
this.errorListener = FirebaseStorageEvt.addListener(
this._errorListener = this._eventEmitter.addListener(
'storage_error',
err => this._handleStorageError(err)
err => this._handleStorageError(err),
);
}
@@ -64,7 +54,7 @@ export default class Storage extends Base {
* @param time The new maximum operation retry time in milliseconds.
*/
setMaxOperationRetryTime(time: number) {
FirebaseStorage.setMaxOperationRetryTime(time);
this._native.setMaxOperationRetryTime(time);
}
/**
@@ -73,7 +63,7 @@ export default class Storage extends Base {
* @param time The new maximum upload retry time in milliseconds.
*/
setMaxUploadRetryTime(time: number) {
FirebaseStorage.setMaxUploadRetryTime(time);
this._native.setMaxUploadRetryTime(time);
}
/**
@@ -82,7 +72,7 @@ export default class Storage extends Base {
* @param time The new maximum download retry time in milliseconds.
*/
setMaxDownloadRetryTime(time: number) {
FirebaseStorage.setMaxDownloadRetryTime(time);
this._native.setMaxDownloadRetryTime(time);
}
/** **********
@@ -95,8 +85,8 @@ export default class Storage extends Base {
this.log.debug('_handleStorageEvent: ', path, eventName, body);
if (this.subscriptions[path] && this.subscriptions[path][eventName]) {
this.subscriptions[path][eventName].forEach((cb) => {
if (this._subscriptions[path] && this._subscriptions[path][eventName]) {
this._subscriptions[path][eventName].forEach((cb) => {
cb(body);
});
}
@@ -107,34 +97,30 @@ export default class Storage extends Base {
}
_addListener(path: string, eventName: string, cb: (evt: Object) => Object) {
if (!this.subscriptions[path]) this.subscriptions[path] = {};
if (!this.subscriptions[path][eventName]) this.subscriptions[path][eventName] = [];
this.subscriptions[path][eventName].push(cb);
if (!this._subscriptions[path]) this._subscriptions[path] = {};
if (!this._subscriptions[path][eventName]) this._subscriptions[path][eventName] = [];
this._subscriptions[path][eventName].push(cb);
}
_removeListener(path: string, eventName: string, origCB: (evt: Object) => Object) {
if (!this.subscriptions[path] || (eventName && !this.subscriptions[path][eventName])) {
if (!this._subscriptions[path] || (eventName && !this._subscriptions[path][eventName])) {
this.log.warn('_removeListener() called, but not currently listening at that location (bad path)', path, eventName);
return;
}
if (eventName && origCB) {
const i = this.subscriptions[path][eventName].indexOf(origCB);
const i = this._subscriptions[path][eventName].indexOf(origCB);
if (i === -1) {
this.log.warn('_removeListener() called, but the callback specified is not listening at this location (bad path)', path, eventName);
} else {
this.subscriptions[path][eventName].splice(i, 1);
this._subscriptions[path][eventName].splice(i, 1);
}
} else if (eventName) {
this.subscriptions[path][eventName] = [];
this._subscriptions[path][eventName] = [];
} else {
this.subscriptions[path] = {};
this._subscriptions[path] = {};
}
}
get namespace(): string {
return 'firebase:storage';
}
}
export const statics = {

View File

@@ -1,11 +1,9 @@
/* @flow */
import { NativeModules } from 'react-native';
import { ReferenceBase } from '../base';
import ReferenceBase from '../../utils/ReferenceBase';
import StorageTask, { UPLOAD_TASK, DOWNLOAD_TASK } from './task';
import Storage from './';
const FirebaseStorage = NativeModules.RNFirebaseStorage;
/**
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference
@@ -39,7 +37,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
delete(): Promise<*> {
return FirebaseStorage.delete(this.path);
return this.storate._native.delete(this.path);
}
/**
@@ -47,7 +45,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
getDownloadURL(): Promise<String> {
return FirebaseStorage.getDownloadURL(this.path);
return this.storate._native.getDownloadURL(this.path);
}
/**
@@ -55,7 +53,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
getMetadata(): Promise<Object> {
return FirebaseStorage.getMetadata(this.path);
return this.storate._native.getMetadata(this.path);
}
/**
@@ -64,7 +62,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
updateMetadata(metadata: Object = {}): Promise<Object> {
return FirebaseStorage.updateMetadata(this.path, metadata);
return this.storate._native.updateMetadata(this.path, metadata);
}
/**
@@ -73,7 +71,7 @@ export default class StorageReference extends ReferenceBase {
* @return {Promise}
*/
downloadFile(filePath: string): Promise<Object> {
return new StorageTask(DOWNLOAD_TASK, FirebaseStorage.downloadFile(this.path, filePath), this);
return new StorageTask(DOWNLOAD_TASK, this.storate._native.downloadFile(this.path, filePath), this);
}
/**
@@ -92,6 +90,6 @@ export default class StorageReference extends ReferenceBase {
*/
putFile(filePath: Object, metadata: Object = {}): Promise<Object> {
const _filePath = filePath.replace('file://', '');
return new StorageTask(UPLOAD_TASK, FirebaseStorage.putFile(this.path, _filePath, metadata), this);
return new StorageTask(UPLOAD_TASK, this.storate._native.putFile(this.path, _filePath, metadata), this);
}
}