mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-23 03:59:18 +08:00
[both] continued work on multi app support - storage now full supported and auth WIP
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
/**
|
||||
* @flow
|
||||
*/
|
||||
import { NativeModules, NativeEventEmitter } from 'react-native';
|
||||
import { NativeModules } from 'react-native';
|
||||
import Log from '../utils/log';
|
||||
import { nativeWithApp } from './../utils';
|
||||
import INTERNALS from './../internals';
|
||||
import FirebaseCore from './../firebase';
|
||||
|
||||
const logs = {};
|
||||
|
||||
@@ -15,6 +16,16 @@ const MULTI_APP_MODULES = [
|
||||
'storage',
|
||||
];
|
||||
|
||||
const NATIVE_MODULE_EVENTS = {
|
||||
Storage: [
|
||||
'storage_event',
|
||||
'storage_error',
|
||||
],
|
||||
Auth: [
|
||||
'onAuthStateChanged',
|
||||
],
|
||||
};
|
||||
|
||||
export default class ModuleBase {
|
||||
constructor(firebaseApp, options, moduleName, withEventEmitter = false) {
|
||||
this._options = Object.assign({}, options);
|
||||
@@ -23,14 +34,16 @@ export default class ModuleBase {
|
||||
this._appName = firebaseApp.name;
|
||||
this._namespace = `${this._appName}:${this._module}`;
|
||||
|
||||
// check if native module exists as all native modules are now optionally part of build
|
||||
// check if native module exists as all native
|
||||
// modules are now optionally part of build
|
||||
const nativeModule = NativeModules[`RNFirebase${moduleName}`];
|
||||
|
||||
if (!nativeModule) {
|
||||
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_MODULE(moduleName));
|
||||
}
|
||||
|
||||
// used by the modules that extend ModuleBase to access their native module counterpart
|
||||
// used by the modules that extend ModuleBase
|
||||
// to access their native module counterpart
|
||||
if (!MULTI_APP_MODULES.includes(moduleName.toLowerCase())) {
|
||||
this._native = nativeModule;
|
||||
} else {
|
||||
@@ -38,10 +51,37 @@ export default class ModuleBase {
|
||||
}
|
||||
|
||||
if (withEventEmitter) {
|
||||
this._eventEmitter = new NativeEventEmitter(this._native);
|
||||
this._setupEventEmitter(nativeModule, moduleName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param nativeModule
|
||||
* @param moduleName
|
||||
* @private
|
||||
*/
|
||||
_setupEventEmitter(nativeModule, moduleName) {
|
||||
this._eventEmitter = FirebaseCore._getOrSetNativeEmitter(this._appName, nativeModule);
|
||||
const events = NATIVE_MODULE_EVENTS[moduleName];
|
||||
|
||||
if (events && events.length) {
|
||||
for (let i = 0, len = events.length; i < len; i++) {
|
||||
FirebaseCore._subscribeForDistribution(events[i], this._eventEmitter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param eventName
|
||||
* @return {string}
|
||||
* @private
|
||||
*/
|
||||
_getAppEventName(eventName) {
|
||||
return `${this._appName}-${eventName}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the FirebaseApp instance for current module
|
||||
* @return {*}
|
||||
|
||||
Reference in New Issue
Block a user