2017-06-29 16:24:34 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
|
|
|
|
import INTERNALS from './internals';
|
|
|
|
|
|
|
|
import AdMob, { statics as AdMobStatics } from './modules/admob';
|
2017-07-04 12:05:19 +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-06-29 16:24:34 +00:00
|
|
|
import Storage, { statics as StorageStatics } from './modules/storage';
|
|
|
|
import Database, { statics as DatabaseStatics } from './modules/database';
|
|
|
|
import Messaging, { statics as MessagingStatics } from './modules/messaging';
|
|
|
|
|
|
|
|
const FirebaseCoreModule = NativeModules.RNFirebase;
|
|
|
|
|
|
|
|
export default class FirebaseApp {
|
|
|
|
constructor(name: string, options: Object = {}) {
|
|
|
|
this._name = name;
|
|
|
|
this._namespaces = {};
|
|
|
|
this._options = Object.assign({}, options);
|
|
|
|
|
|
|
|
// native ios/android to confirm initialized
|
2017-07-17 17:20:27 +00:00
|
|
|
this._initialized = false;
|
2017-06-29 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param native
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_initializeApp(native = false) {
|
|
|
|
if (native) {
|
2017-07-17 16:36:50 +00:00
|
|
|
// for apps already initialized natively that we have info
|
2017-07-17 17:20:27 +00:00
|
|
|
// from RN constants
|
2017-06-29 16:24:34 +00:00
|
|
|
this._initialized = true;
|
2017-07-17 17:20:27 +00:00
|
|
|
this._nativeInitialized = true;
|
2017-07-17 16:36:50 +00:00
|
|
|
} else {
|
|
|
|
FirebaseCoreModule.initializeApp(this._name, this._options, (error, result) => {
|
|
|
|
// todo check error/result
|
|
|
|
this._initialized = true;
|
2017-07-17 17:20:27 +00:00
|
|
|
this._nativeInitialized = false;
|
2017-07-17 16:36:50 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
get name() {
|
2017-07-17 16:36:50 +00:00
|
|
|
if (this._name === INTERNALS.STRINGS.DEFAULT_APP_NAME) {
|
|
|
|
// ios and android firebase sdk's return different
|
|
|
|
// app names - so we just return what the web sdk
|
|
|
|
// would if it was default.
|
|
|
|
return '[DEFAULT]';
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
get options() {
|
|
|
|
return Object.assign({}, this._options);
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
delete() {
|
2017-07-17 17:20:27 +00:00
|
|
|
if (this._name === INTERNALS.STRINGS.DEFAULT_APP_NAME && this._nativeInitialized) {
|
|
|
|
return Promise.reject(
|
|
|
|
new Error('Unable to delete the default native firebase app instance.'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MODULES
|
|
|
|
*/
|
|
|
|
|
2017-07-12 15:11:07 +00:00
|
|
|
get admob() {
|
|
|
|
return this._staticsOrModuleInstance('admob', AdMobStatics, AdMob);
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
get auth() {
|
|
|
|
return this._staticsOrModuleInstance('auth', AuthStatics, Auth);
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:11:07 +00:00
|
|
|
get analytics() {
|
|
|
|
return this._staticsOrModuleInstance('analytics', {}, Analytics);
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
get config() {
|
|
|
|
return this._staticsOrModuleInstance('config', {}, RemoteConfig);
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:11:07 +00:00
|
|
|
get crash() {
|
|
|
|
return this._staticsOrModuleInstance('crash', {}, Crash);
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
get database() {
|
|
|
|
return this._staticsOrModuleInstance('database', DatabaseStatics, Database);
|
|
|
|
}
|
|
|
|
|
|
|
|
get messaging() {
|
|
|
|
return this._staticsOrModuleInstance('messaging', MessagingStatics, Messaging);
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:11:07 +00:00
|
|
|
get perf() {
|
|
|
|
return this._staticsOrModuleInstance('perf', {}, Performance);
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
get storage() {
|
|
|
|
return this._staticsOrModuleInstance('storage', StorageStatics, Storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param statics
|
|
|
|
* @param InstanceClass
|
|
|
|
* @return {function()}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_staticsOrModuleInstance(name, statics = {}, InstanceClass): Function {
|
|
|
|
const getInstance = () => {
|
|
|
|
const _name = `_${name}`;
|
|
|
|
|
|
|
|
if (!this._namespaces[_name]) {
|
|
|
|
this._namespaces[_name] = new InstanceClass(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._namespaces[_name];
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(getInstance, statics);
|
|
|
|
return getInstance;
|
|
|
|
}
|
|
|
|
}
|