[both] continued work on multi app support - storage now full supported and auth WIP

This commit is contained in:
Salakar
2017-07-12 15:49:33 +01:00
parent 82a2afd59b
commit 282f953f52
18 changed files with 280 additions and 194 deletions

View File

@@ -3,6 +3,7 @@ import { NativeModules } from 'react-native';
import StorageRef from './reference';
import ModuleBase from './../../utils/ModuleBase';
import INTERNALS from './../../internals';
const FirebaseStorage = NativeModules.RNFirebaseStorage;
@@ -16,14 +17,14 @@ export default class Storage extends ModuleBase {
super(firebaseApp, options, 'Storage', true);
this._subscriptions = {};
this._successListener = this._eventEmitter.addListener(
'storage_event',
event => this._handleStorageEvent(event),
this.addListener(
this._getAppEventName('storage_event'),
this._handleStorageEvent.bind(this),
);
this._errorListener = this._eventEmitter.addListener(
'storage_error',
err => this._handleStorageError(err),
this.addListener(
this._getAppEventName('storage_error'),
this._handleStorageEvent.bind(this),
);
}
@@ -78,48 +79,32 @@ export default class Storage extends ModuleBase {
/** **********
* INTERNALS
********** **/
_getSubEventName(path, eventName) {
return this._getAppEventName(`${path}-${eventName}`);
}
_handleStorageEvent(event: Object) {
const { path, eventName } = event;
const body = event.body || {};
this.log.debug('_handleStorageEvent: ', path, eventName, body);
if (this._subscriptions[path] && this._subscriptions[path][eventName]) {
this._subscriptions[path][eventName].forEach((cb) => {
cb(body);
});
}
this.emit(this._getSubEventName(path, eventName), body);
}
_handleStorageError(err: Object) {
const { path, eventName } = event;
const body = event.body || {};
this.log.debug('_handleStorageError ->', err);
this.emit(this._getSubEventName(path, eventName), body);
}
_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);
this.on(this._getSubEventName(path, eventName), cb);
}
_removeListener(path: string, eventName: string, origCB: (evt: Object) => Object) {
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);
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);
}
} else if (eventName) {
this._subscriptions[path][eventName] = [];
} else {
this._subscriptions[path] = {};
}
this.removeListener(this._getSubEventName(path, eventName), origCB);
}
}

View File

@@ -10,8 +10,7 @@ import Storage from './';
*/
export default class StorageReference extends ReferenceBase {
constructor(storage: Storage, path: string) {
super(path);
this.storage = storage;
super(path, storage);
}
get fullPath(): string {
@@ -29,7 +28,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {StorageReference}
*/
child(path: string) {
return new StorageReference(this.storage, `${this.path}/${path}`);
return new StorageReference(this._module, `${this.path}/${path}`);
}
/**
@@ -37,7 +36,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
delete(): Promise<*> {
return this.storate._native.delete(this.path);
return this._module._native.delete(this.path);
}
/**
@@ -45,7 +44,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
getDownloadURL(): Promise<String> {
return this.storate._native.getDownloadURL(this.path);
return this._module._native.getDownloadURL(this.path);
}
/**
@@ -53,7 +52,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
getMetadata(): Promise<Object> {
return this.storate._native.getMetadata(this.path);
return this._module._native.getMetadata(this.path);
}
/**
@@ -62,7 +61,7 @@ export default class StorageReference extends ReferenceBase {
* @returns {Promise.<T>|*}
*/
updateMetadata(metadata: Object = {}): Promise<Object> {
return this.storate._native.updateMetadata(this.path, metadata);
return this._module._native.updateMetadata(this.path, metadata);
}
/**
@@ -71,7 +70,7 @@ export default class StorageReference extends ReferenceBase {
* @return {Promise}
*/
downloadFile(filePath: string): Promise<Object> {
return new StorageTask(DOWNLOAD_TASK, this.storate._native.downloadFile(this.path, filePath), this);
return new StorageTask(DOWNLOAD_TASK, this._module._native.downloadFile(this.path, filePath), this);
}
/**
@@ -90,6 +89,6 @@ export default class StorageReference extends ReferenceBase {
*/
putFile(filePath: Object, metadata: Object = {}): Promise<Object> {
const _filePath = filePath.replace('file://', '');
return new StorageTask(UPLOAD_TASK, this.storate._native.putFile(this.path, _filePath, metadata), this);
return new StorageTask(UPLOAD_TASK, this._module._native.putFile(this.path, _filePath, metadata), this);
}
}

View File

@@ -1,6 +1,6 @@
/* @flow */
import { statics as StorageStatics } from './';
import { isObject, isFunction } from './../../utils';
import { isFunction } from './../../utils';
import StorageReference from './reference';
export const UPLOAD_TASK = 'upload';
@@ -38,17 +38,17 @@ declare type NextOrObserverType = null |
* @url https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask
*/
export default class StorageTask {
type: typeof UPLOAD_TASK | typeof DOWNLOAD_TASK
ref: StorageReference
storage: StorageReference.storage
path: StorageReference.path
then: () => Promise<*>
catch: () => Promise<*>
type: typeof UPLOAD_TASK | typeof DOWNLOAD_TASK;
ref: StorageReference;
storage: StorageReference.storage;
path: StorageReference.path;
then: () => Promise<*>;
catch: () => Promise<*>;
constructor(type: typeof UPLOAD_TASK | typeof DOWNLOAD_TASK, promise: Promise<*>, storageRef: StorageReference) {
this.type = type;
this.ref = storageRef;
this.storage = storageRef.storage;
this.storage = storageRef._module;
this.path = storageRef.path;
// 'proxy' original promise
@@ -115,21 +115,21 @@ export default class StorageTask {
this.storage._addListener(
this.path,
StorageStatics.TaskEvent.STATE_CHANGED,
_next
_next,
);
}
if (_error) {
this.storage._addListener(
this.path,
`${this.type}_failure`,
_error
_error,
);
}
if (_complete) {
this.storage._addListener(
this.path,
`${this.type}_success`,
_complete
_complete,
);
}