From 3d360348d5d346e6e081608be9b3735b635afb61 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sat, 7 Oct 2017 01:49:12 +0100 Subject: [PATCH] [utils] run play services check automatically after any module usage - once --- lib/firebase.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/firebase.js b/lib/firebase.js index ee8d3ddb..2ce9317c 100644 --- a/lib/firebase.js +++ b/lib/firebase.js @@ -6,7 +6,7 @@ import { NativeModules, NativeEventEmitter } from 'react-native'; import INTERNALS from './internals'; import FirebaseApp from './firebase-app'; -import { isObject, isString } from './utils'; +import { isObject, isString, isAndroid } from './utils'; // module imports import AdMob, { statics as AdMobStatics } from './modules/admob'; @@ -27,18 +27,13 @@ class FirebaseCore { constructor() { this._nativeEmitters = {}; this._nativeSubscriptions = {}; + this._checkedPlayServices = false; if (!FirebaseCoreModule) { throw (new Error(INTERNALS.STRINGS.ERROR_MISSING_CORE)); } - 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); - } + this._initializeNativeApps(); // modules this.admob = this._appNamespaceOrStatics(AdMobStatics, AdMob); @@ -54,6 +49,20 @@ class FirebaseCore { this.utils = this._appNamespaceOrStatics(UtilsStatics, Utils); } + /** + * 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); + } + } + /** * Web SDK initializeApp * @@ -171,7 +180,6 @@ class FirebaseCore { /** * - * @param namespace * @param statics * @param InstanceClass * @return {function(FirebaseApp=)} @@ -179,8 +187,18 @@ class FirebaseCore { */ _appNamespaceOrStatics(statics = {}, InstanceClass): Function { const namespace = InstanceClass._NAMESPACE; + + // play services checks will run for the first time on any module + // usage - except for the utils module - to allow you to configure + // play services options + if (isAndroid && namespace !== Utils._NAMESPACE && !this._checkedPlayServices) { + this._checkedPlayServices = true; + this.utils().checkPlayServicesAvailability(); + } + const getNamespace = (app?: FirebaseApp) => { let _app = app; + // 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)); @@ -193,6 +211,7 @@ class FirebaseCore { Object.assign(getNamespace, statics, { nativeModuleExists: !!NativeModules[InstanceClass._NATIVE_MODULE], }); + return getNamespace; }