2017-03-02 13:10:10 +00:00
|
|
|
/**
|
|
|
|
* @providesModule Firebase
|
|
|
|
* @flow
|
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
import { NativeModules, NativeEventEmitter } from 'react-native';
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
import INTERNALS from './internals';
|
|
|
|
import FirebaseApp from './firebase-app';
|
2017-10-07 00:49:12 +00:00
|
|
|
import { isObject, isString, isAndroid } from './utils';
|
2017-06-29 16:24:34 +00:00
|
|
|
|
|
|
|
// module imports
|
2017-07-12 15:11:07 +00:00
|
|
|
import AdMob, { statics as AdMobStatics } from './modules/admob';
|
2017-03-27 18:11:26 +00:00
|
|
|
import Auth, { statics as AuthStatics } from './modules/auth';
|
2017-07-12 15:11:07 +00:00
|
|
|
import Analytics from './modules/analytics';
|
|
|
|
import Crash from './modules/crash';
|
|
|
|
import Performance from './modules/perf';
|
2017-07-04 12:05:19 +00:00
|
|
|
import RemoteConfig from './modules/config';
|
2017-03-22 20:15:02 +00:00
|
|
|
import Storage, { statics as StorageStatics } from './modules/storage';
|
|
|
|
import Database, { statics as DatabaseStatics } from './modules/database';
|
2017-03-30 15:25:27 +00:00
|
|
|
import Messaging, { statics as MessagingStatics } from './modules/messaging';
|
2017-09-27 14:41:25 +00:00
|
|
|
import Firestore, { statics as FirestoreStatics } from './modules/firestore';
|
2017-10-06 23:24:00 +00:00
|
|
|
import Utils, { statics as UtilsStatics } from './modules/utils';
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
const FirebaseCoreModule = NativeModules.RNFirebase;
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
class FirebaseCore {
|
|
|
|
constructor() {
|
|
|
|
this._nativeEmitters = {};
|
2017-07-12 14:49:33 +00:00
|
|
|
this._nativeSubscriptions = {};
|
2017-07-17 16:36:50 +00:00
|
|
|
|
2017-08-18 20:05:47 +00:00
|
|
|
if (!FirebaseCoreModule) {
|
|
|
|
throw (new Error(INTERNALS.STRINGS.ERROR_MISSING_CORE));
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:49:12 +00:00
|
|
|
this._initializeNativeApps();
|
2017-07-18 05:04:12 +00:00
|
|
|
|
|
|
|
// modules
|
2017-08-17 16:58:28 +00:00
|
|
|
this.admob = this._appNamespaceOrStatics(AdMobStatics, AdMob);
|
|
|
|
this.auth = this._appNamespaceOrStatics(AuthStatics, Auth);
|
|
|
|
this.analytics = this._appNamespaceOrStatics({}, Analytics);
|
|
|
|
this.config = this._appNamespaceOrStatics({}, RemoteConfig);
|
|
|
|
this.crash = this._appNamespaceOrStatics({}, Crash);
|
|
|
|
this.database = this._appNamespaceOrStatics(DatabaseStatics, Database);
|
2017-09-27 14:41:25 +00:00
|
|
|
this.firestore = this._appNamespaceOrStatics(FirestoreStatics, Firestore);
|
2017-08-17 16:58:28 +00:00
|
|
|
this.messaging = this._appNamespaceOrStatics(MessagingStatics, Messaging);
|
|
|
|
this.perf = this._appNamespaceOrStatics(DatabaseStatics, Performance);
|
|
|
|
this.storage = this._appNamespaceOrStatics(StorageStatics, Storage);
|
2017-10-06 23:24:00 +00:00
|
|
|
this.utils = this._appNamespaceOrStatics(UtilsStatics, Utils);
|
2017-06-29 16:24:34 +00:00
|
|
|
}
|
2017-04-04 16:58:20 +00:00
|
|
|
|
2017-10-07 00:49:12 +00:00
|
|
|
/**
|
|
|
|
* Bootstraps all native app instances that were discovered on boot
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_initializeNativeApps() {
|
|
|
|
for (let i = 0, len = FirebaseCoreModule.apps.length; i < len; i++) {
|
|
|
|
const app = FirebaseCoreModule.apps[i];
|
|
|
|
const options = Object.assign({}, app);
|
|
|
|
delete options.name;
|
|
|
|
INTERNALS.APPS[app.name] = new FirebaseApp(app.name, options);
|
|
|
|
INTERNALS.APPS[app.name]._initializeApp(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:10:10 +00:00
|
|
|
/**
|
2017-06-29 16:24:34 +00:00
|
|
|
* Web SDK initializeApp
|
2017-03-02 13:10:10 +00:00
|
|
|
*
|
|
|
|
* @param options
|
2017-06-29 16:24:34 +00:00
|
|
|
* @param name
|
|
|
|
* @return {*}
|
2017-03-02 13:10:10 +00:00
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
initializeApp(options: Object = {}, name: string): FirebaseApp {
|
2017-07-18 05:04:12 +00:00
|
|
|
if (name && !isString(name)) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_INIT_STRING_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
const _name = (name || INTERNALS.STRINGS.DEFAULT_APP_NAME).toUpperCase();
|
|
|
|
|
|
|
|
// return an existing app if found
|
2017-08-11 14:58:42 +00:00
|
|
|
// todo in v4 remove deprecation and throw an error
|
2017-08-18 20:05:47 +00:00
|
|
|
if (INTERNALS.APPS[_name]) {
|
|
|
|
console.warn(INTERNALS.STRINGS.WARN_INITIALIZE_DEPRECATION);
|
|
|
|
return INTERNALS.APPS[_name];
|
|
|
|
}
|
2017-07-18 05:04:12 +00:00
|
|
|
|
|
|
|
// only validate if app doesn't already exist
|
|
|
|
// to allow apps already initialized natively
|
|
|
|
// to still go through init without erroring (backwards compatibility)
|
2017-06-29 16:24:34 +00:00
|
|
|
if (!isObject(options)) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_INIT_OBJECT);
|
2017-03-10 18:12:46 +00:00
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-07-18 05:04:12 +00:00
|
|
|
if (!options.apiKey) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_OPT('apiKey'));
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-07-18 05:04:12 +00:00
|
|
|
if (!options.appId) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_OPT('appId'));
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
2017-03-22 20:15:02 +00:00
|
|
|
|
2017-07-18 05:04:12 +00:00
|
|
|
if (!options.databaseURL) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_OPT('databaseURL'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.messagingSenderId) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_OPT('messagingSenderId'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.projectId) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_OPT('projectId'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.storageBucket) {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_OPT('storageBucket'));
|
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
|
2017-07-18 05:04:12 +00:00
|
|
|
INTERNALS.APPS[_name] = new FirebaseApp(_name, options);
|
|
|
|
// only initialize if certain props are available
|
|
|
|
if (options.databaseURL && options.apiKey) {
|
|
|
|
INTERNALS.APPS[_name]._initializeApp();
|
2017-05-30 15:23:51 +00:00
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
|
|
|
|
return INTERNALS.APPS[_name];
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-29 16:24:34 +00:00
|
|
|
* Retrieves a Firebase app instance.
|
|
|
|
*
|
|
|
|
* When called with no arguments, the default app is returned.
|
|
|
|
* When an app name is provided, the app corresponding to that name is returned.
|
|
|
|
*
|
2017-03-02 13:10:10 +00:00
|
|
|
* @param name
|
2017-06-29 16:24:34 +00:00
|
|
|
* @return {*}
|
2017-03-02 13:10:10 +00:00
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
app(name?: string): FirebaseApp {
|
|
|
|
const _name = name ? name.toUpperCase() : INTERNALS.STRINGS.DEFAULT_APP_NAME;
|
|
|
|
const app = INTERNALS.APPS[_name];
|
|
|
|
if (!app) throw new Error(INTERNALS.STRINGS.ERROR_APP_NOT_INIT(_name));
|
|
|
|
return app;
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
/**
|
|
|
|
* A (read-only) array of all initialized apps.
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
|
|
|
get apps(): Array<Object> {
|
|
|
|
return Object.values(INTERNALS.APPS);
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
/*
|
|
|
|
* INTERNALS
|
|
|
|
*/
|
|
|
|
|
2017-03-02 13:10:10 +00:00
|
|
|
/**
|
2017-06-29 16:24:34 +00:00
|
|
|
* Subscribe to a native event for js side distribution by appName
|
|
|
|
* React Native events are hard set at compile - cant do dynamic event names
|
|
|
|
* so we use a single event send it to js and js then internally can prefix it
|
|
|
|
* and distribute dynamically.
|
|
|
|
*
|
|
|
|
* @param eventName
|
|
|
|
* @param nativeEmitter
|
|
|
|
* @private
|
2017-03-02 13:10:10 +00:00
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
_subscribeForDistribution(eventName, nativeEmitter) {
|
2017-07-12 14:49:33 +00:00
|
|
|
if (!this._nativeSubscriptions[eventName]) {
|
|
|
|
nativeEmitter.addListener(eventName, (event) => {
|
|
|
|
if (event.appName) {
|
|
|
|
// native event has an appName property - auto prefix and internally emit
|
|
|
|
INTERNALS.SharedEventEmitter.emit(`${event.appName}-${eventName}`, event);
|
|
|
|
} else {
|
|
|
|
// standard event - no need to prefix
|
|
|
|
INTERNALS.SharedEventEmitter.emit(eventName, event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._nativeSubscriptions[eventName] = true;
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-22 20:45:53 +00:00
|
|
|
*
|
|
|
|
* @param statics
|
2017-08-17 16:25:13 +00:00
|
|
|
* @param InstanceClass
|
2017-06-29 16:24:34 +00:00
|
|
|
* @return {function(FirebaseApp=)}
|
2017-03-22 20:45:53 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2017-08-17 16:58:28 +00:00
|
|
|
_appNamespaceOrStatics(statics = {}, InstanceClass): Function {
|
|
|
|
const namespace = InstanceClass._NAMESPACE;
|
2017-10-07 00:49:12 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
const getNamespace = (app?: FirebaseApp) => {
|
|
|
|
let _app = app;
|
2017-10-07 00:49:12 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
// throw an error if it's not a valid app instance
|
|
|
|
if (_app && !(_app instanceof FirebaseApp)) throw new Error(INTERNALS.STRINGS.ERROR_NOT_APP(namespace));
|
2017-07-12 14:49:33 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
// default to the 'DEFAULT' app if no arg provided - will throw an error
|
|
|
|
// if default app not initialized
|
|
|
|
else if (!_app) _app = this.app(INTERNALS.STRINGS.DEFAULT_APP_NAME);
|
2017-08-23 10:18:51 +00:00
|
|
|
return INTERNALS.APPS[_app._name][namespace](_app);
|
2017-03-22 20:45:53 +00:00
|
|
|
};
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-08-15 16:28:51 +00:00
|
|
|
Object.assign(getNamespace, statics, {
|
2017-08-17 16:58:28 +00:00
|
|
|
nativeModuleExists: !!NativeModules[InstanceClass._NATIVE_MODULE],
|
2017-08-15 16:28:51 +00:00
|
|
|
});
|
2017-10-07 00:49:12 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
return getNamespace;
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
|
2017-07-12 14:49:33 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param nativeModule
|
|
|
|
* @return {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getOrSetNativeEmitter(name, nativeModule) {
|
|
|
|
if (this._nativeEmitters[name]) {
|
|
|
|
return this._nativeEmitters[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._nativeEmitters[name] = new NativeEventEmitter(nativeModule);
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
|
|
|
|
export default new FirebaseCore();
|