2017-11-17 11:07:52 +00:00
|
|
|
/*
|
|
|
|
* @flow
|
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
import APPS from '../../utils/apps';
|
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
2017-11-17 16:17:27 +00:00
|
|
|
import INTERNALS from '../../utils/internals';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { isObject } from '../../utils';
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
import AdMob, { NAMESPACE as AdmobNamespace } from '../admob';
|
|
|
|
import Auth, { NAMESPACE as AuthNamespace } from '../auth';
|
|
|
|
import Analytics, { NAMESPACE as AnalyticsNamespace } from '../analytics';
|
|
|
|
import Config, { NAMESPACE as ConfigNamespace } from '../config';
|
|
|
|
import Crash, { NAMESPACE as CrashNamespace } from '../crash';
|
2018-04-03 15:51:05 +00:00
|
|
|
import Crashlytics, { NAMESPACE as CrashlyticsNamespace } from '../crashlytics';
|
2018-01-03 20:00:38 +00:00
|
|
|
import Database, { NAMESPACE as DatabaseNamespace } from '../database';
|
|
|
|
import Firestore, { NAMESPACE as FirestoreNamespace } from '../firestore';
|
2018-03-23 14:59:00 +00:00
|
|
|
import InstanceId, { NAMESPACE as InstanceIdNamespace } from '../iid';
|
2018-03-22 12:46:37 +00:00
|
|
|
import Invites, { NAMESPACE as InvitesNamespace } from '../invites';
|
2018-01-03 20:00:38 +00:00
|
|
|
import Links, { NAMESPACE as LinksNamespace } from '../links';
|
|
|
|
import Messaging, { NAMESPACE as MessagingNamespace } from '../messaging';
|
2018-02-02 08:40:48 +00:00
|
|
|
import Notifications, {
|
|
|
|
NAMESPACE as NotificationsNamespace,
|
|
|
|
} from '../notifications';
|
2018-01-03 20:00:38 +00:00
|
|
|
import Performance, { NAMESPACE as PerfNamespace } from '../perf';
|
|
|
|
import Storage, { NAMESPACE as StorageNamespace } from '../storage';
|
|
|
|
import Utils, { NAMESPACE as UtilsNamespace } from '../utils';
|
2017-11-17 11:07:52 +00:00
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
import type { FirebaseOptions } from '../../types';
|
2017-11-23 17:29:40 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
const FirebaseCoreModule = NativeModules.RNFirebase;
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
export default class App {
|
2018-01-25 18:25:39 +00:00
|
|
|
_extendedProps: { [string]: boolean };
|
2017-12-22 15:24:31 +00:00
|
|
|
_initialized: boolean = false;
|
2017-11-17 11:07:52 +00:00
|
|
|
_name: string;
|
2017-12-22 15:24:31 +00:00
|
|
|
_nativeInitialized: boolean = false;
|
2017-11-17 11:07:52 +00:00
|
|
|
_options: FirebaseOptions;
|
2017-12-22 15:24:31 +00:00
|
|
|
admob: () => AdMob;
|
|
|
|
analytics: () => Analytics;
|
|
|
|
auth: () => Auth;
|
|
|
|
config: () => Config;
|
|
|
|
crash: () => Crash;
|
2018-04-03 15:51:05 +00:00
|
|
|
crashlytics: () => Crashlytics;
|
2017-12-22 15:24:31 +00:00
|
|
|
database: () => Database;
|
|
|
|
firestore: () => Firestore;
|
2018-03-23 14:59:00 +00:00
|
|
|
iid: () => InstanceId;
|
2018-03-22 12:46:37 +00:00
|
|
|
invites: () => Invites;
|
2017-12-22 15:24:31 +00:00
|
|
|
links: () => Links;
|
|
|
|
messaging: () => Messaging;
|
2018-02-02 08:40:48 +00:00
|
|
|
notifications: () => Notifications;
|
2017-12-22 15:24:31 +00:00
|
|
|
perf: () => Performance;
|
|
|
|
storage: () => Storage;
|
|
|
|
utils: () => Utils;
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
constructor(
|
|
|
|
name: string,
|
|
|
|
options: FirebaseOptions,
|
|
|
|
fromNative: boolean = false
|
|
|
|
) {
|
2017-06-29 16:24:34 +00:00
|
|
|
this._name = name;
|
|
|
|
this._options = Object.assign({}, options);
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
if (fromNative) {
|
2017-06-29 16:24:34 +00:00
|
|
|
this._initialized = true;
|
2017-07-17 17:20:27 +00:00
|
|
|
this._nativeInitialized = true;
|
2017-12-22 15:24:31 +00:00
|
|
|
} else if (options.databaseURL && options.apiKey) {
|
2018-01-25 18:25:39 +00:00
|
|
|
FirebaseCoreModule.initializeApp(
|
|
|
|
this._name,
|
|
|
|
this._options,
|
|
|
|
(error, result) => {
|
|
|
|
this._initialized = true;
|
|
|
|
SharedEventEmitter.emit(`AppReady:${this._name}`, { error, result });
|
|
|
|
}
|
|
|
|
);
|
2017-07-17 16:36:50 +00:00
|
|
|
}
|
2017-12-22 15:24:31 +00:00
|
|
|
|
|
|
|
// modules
|
2018-01-03 20:00:38 +00:00
|
|
|
this.admob = APPS.appModule(this, AdmobNamespace, AdMob);
|
|
|
|
this.analytics = APPS.appModule(this, AnalyticsNamespace, Analytics);
|
|
|
|
this.auth = APPS.appModule(this, AuthNamespace, Auth);
|
|
|
|
this.config = APPS.appModule(this, ConfigNamespace, Config);
|
|
|
|
this.crash = APPS.appModule(this, CrashNamespace, Crash);
|
2018-04-03 15:51:05 +00:00
|
|
|
this.crashlytics = APPS.appModule(this, CrashlyticsNamespace, Crashlytics);
|
2018-01-03 20:00:38 +00:00
|
|
|
this.database = APPS.appModule(this, DatabaseNamespace, Database);
|
|
|
|
this.firestore = APPS.appModule(this, FirestoreNamespace, Firestore);
|
2018-03-23 14:59:00 +00:00
|
|
|
this.iid = APPS.appModule(this, InstanceIdNamespace, InstanceId);
|
2018-03-22 12:46:37 +00:00
|
|
|
this.invites = APPS.appModule(this, InvitesNamespace, Invites);
|
2018-01-03 20:00:38 +00:00
|
|
|
this.links = APPS.appModule(this, LinksNamespace, Links);
|
|
|
|
this.messaging = APPS.appModule(this, MessagingNamespace, Messaging);
|
2018-02-02 08:40:48 +00:00
|
|
|
this.notifications = APPS.appModule(
|
2018-01-30 11:15:59 +00:00
|
|
|
this,
|
2018-02-02 08:40:48 +00:00
|
|
|
NotificationsNamespace,
|
|
|
|
Notifications
|
2018-01-30 11:15:59 +00:00
|
|
|
);
|
2018-01-03 20:00:38 +00:00
|
|
|
this.perf = APPS.appModule(this, PerfNamespace, Performance);
|
|
|
|
this.storage = APPS.appModule(this, StorageNamespace, Storage);
|
|
|
|
this.utils = APPS.appModule(this, UtilsNamespace, Utils);
|
2017-12-22 15:24:31 +00:00
|
|
|
this._extendedProps = {};
|
2017-06-29 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-11-17 11:07:52 +00:00
|
|
|
get name(): string {
|
2017-06-29 16:24:34 +00:00
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-11-17 11:07:52 +00:00
|
|
|
get options(): FirebaseOptions {
|
2017-06-29 16:24:34 +00:00
|
|
|
return Object.assign({}, this._options);
|
|
|
|
}
|
|
|
|
|
2017-08-26 22:41:59 +00:00
|
|
|
/**
|
2017-08-26 22:47:31 +00:00
|
|
|
* Undocumented firebase web sdk method that allows adding additional properties onto
|
|
|
|
* a firebase app instance.
|
|
|
|
*
|
|
|
|
* See: https://github.com/firebase/firebase-js-sdk/blob/master/tests/app/firebase_app.test.ts#L328
|
2017-08-26 22:41:59 +00:00
|
|
|
*
|
|
|
|
* @param props
|
|
|
|
*/
|
|
|
|
extendApp(props: Object) {
|
2018-02-24 05:53:07 +00:00
|
|
|
if (!isObject(props)) {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_MISSING_ARG('Object', 'extendApp')
|
|
|
|
);
|
2018-02-24 05:53:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 22:41:59 +00:00
|
|
|
const keys = Object.keys(props);
|
|
|
|
|
|
|
|
for (let i = 0, len = keys.length; i < len; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
|
|
|
|
if (!this._extendedProps[key] && Object.hasOwnProperty.call(this, key)) {
|
2017-11-17 11:07:52 +00:00
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_PROTECTED_PROP(key));
|
2017-08-26 22:41:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 12:55:19 +00:00
|
|
|
// $FlowExpectedError: Flow doesn't support indexable signatures on classes: https://github.com/facebook/flow/issues/1323
|
2017-08-26 22:41:59 +00:00
|
|
|
this[key] = props[key];
|
|
|
|
this._extendedProps[key] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:20:27 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-06-29 16:24:34 +00:00
|
|
|
delete() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('app', 'delete')
|
|
|
|
);
|
2017-10-03 12:39:55 +00:00
|
|
|
// TODO only the ios sdk currently supports delete, add back in when android also supports it
|
2018-01-03 20:00:38 +00:00
|
|
|
// if (this._name === APPS.DEFAULT_APP_NAME && this._nativeInitialized) {
|
2017-10-03 12:39:55 +00:00
|
|
|
// return Promise.reject(
|
|
|
|
// new Error('Unable to delete the default native firebase app instance.'),
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return FirebaseCoreModule.deleteApp(this._name);
|
2017-06-29 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 05:02:53 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2018-01-05 17:20:02 +00:00
|
|
|
onReady(): Promise<App> {
|
2017-07-17 19:56:08 +00:00
|
|
|
if (this._initialized) return Promise.resolve(this);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.once(`AppReady:${this._name}`, ({ error }) => {
|
2017-07-17 19:56:08 +00:00
|
|
|
if (error) return reject(new Error(error)); // error is a string as it's from native
|
|
|
|
return resolve(this); // return app
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-02-24 05:53:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* toString returns the name of the app.
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
toString() {
|
|
|
|
return this._name;
|
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
}
|