From 67dc829427b004bd066a1f13b2b3bd7a8b4a85b1 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sat, 26 Aug 2017 23:41:59 +0100 Subject: [PATCH] [core] add extendApp support --- lib/firebase-app.js | 22 ++++++++++++++++++++++ lib/internals.js | 14 ++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/firebase-app.js b/lib/firebase-app.js index efeda6a8..d99f88ae 100644 --- a/lib/firebase-app.js +++ b/lib/firebase-app.js @@ -1,6 +1,7 @@ import { NativeModules } from 'react-native'; import INTERNALS from './internals'; +import { isObject } from './utils'; import AdMob, { statics as AdMobStatics } from './modules/admob'; import Auth, { statics as AuthStatics } from './modules/auth'; @@ -34,6 +35,7 @@ export default class FirebaseApp { this.messaging = this._staticsOrModuleInstance(MessagingStatics, Messaging); this.perf = this._staticsOrModuleInstance({}, Performance); this.storage = this._staticsOrModuleInstance(StorageStatics, Storage); + this._extendedProps = {}; } /** @@ -78,6 +80,26 @@ export default class FirebaseApp { return Object.assign({}, this._options); } + /** + * + * @param props + */ + extendApp(props: Object) { + if (!isObject(props)) throw new Error(INTERNALS.ERROR_MISSING_ARG('Object', 'extendApp')); + 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)) { + throw new Error(INTERNALS.ERROR_PROTECTED_PROP(key)); + } + + this[key] = props[key]; + this._extendedProps[key] = true; + } + } + /** * * @return {Promise} diff --git a/lib/internals.js b/lib/internals.js index 480d4970..3c0a6d52 100644 --- a/lib/internals.js +++ b/lib/internals.js @@ -63,6 +63,20 @@ export default { return `Missing required callback for method ${method}().`; }, + /** + * @return {string} + */ + ERROR_MISSING_ARG(type, method) { + return `Missing required argument of type '${type}' for method '${method}()'.`; + }, + + /** + * @return {string} + */ + ERROR_PROTECTED_PROP(name) { + return `Property '${name}' is protected and can not be overridden by extendApp.`; + }, + /** * @return {string} * @param namespace