diff --git a/lib/modules/admob/index.js b/lib/modules/admob/index.js index 070e1a3f..fc971d01 100644 --- a/lib/modules/admob/index.js +++ b/lib/modules/admob/index.js @@ -26,6 +26,11 @@ type NativeEvent = { type: string, } +const NATIVE_EVENTS = [ + 'interstitial_event', + 'rewarded_video_event', +]; + export default class AdMob extends ModuleBase { static _NAMESPACE = 'admob'; static _NATIVE_MODULE = 'RNFirebaseAdMob'; @@ -34,7 +39,7 @@ export default class AdMob extends ModuleBase { _initialized: boolean; constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); this._initialized = false; this._appId = null; diff --git a/lib/modules/auth/index.js b/lib/modules/auth/index.js index 18e27066..e9ac0296 100644 --- a/lib/modules/auth/index.js +++ b/lib/modules/auth/index.js @@ -27,6 +27,11 @@ type AuthResult = { user: Object|null } | null; +const NATIVE_EVENTS = [ + 'auth_state_changed', + 'phone_auth_state_changed', +]; + export default class Auth extends ModuleBase { static _NAMESPACE = 'auth'; static _NATIVE_MODULE = 'RNFirebaseAuth'; @@ -35,7 +40,7 @@ export default class Auth extends ModuleBase { _user: User | null; constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); this._user = null; this._authResult = null; diff --git a/lib/modules/database/index.js b/lib/modules/database/index.js index ddcae474..850f6152 100644 --- a/lib/modules/database/index.js +++ b/lib/modules/database/index.js @@ -10,6 +10,11 @@ import ModuleBase from './../../utils/ModuleBase'; import type FirebaseApp from '../core/firebase-app'; +const NATIVE_EVENTS = [ + 'database_transaction_event', + // 'database_server_offset', // TODO +]; + /** * @class Database */ @@ -22,7 +27,7 @@ export default class Database extends ModuleBase { _transactionHandler: TransactionHandler; constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); this._transactionHandler = new TransactionHandler(this); if (this._options.persistence) { diff --git a/lib/modules/firestore/index.js b/lib/modules/firestore/index.js index 78c94e3d..c3d3a0fb 100644 --- a/lib/modules/firestore/index.js +++ b/lib/modules/firestore/index.js @@ -34,6 +34,11 @@ type DocumentSyncEvent = { path: string, } +const NATIVE_EVENTS = [ + 'firestore_collection_sync_event', + 'firestore_document_sync_event', +]; + /** * @class Firestore */ @@ -44,7 +49,7 @@ export default class Firestore extends ModuleBase { _referencePath: Path; constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); this._referencePath = new Path([]); SharedEventEmitter.addListener( diff --git a/lib/modules/links/index.js b/lib/modules/links/index.js index c4c26e6f..fc36c1ae 100644 --- a/lib/modules/links/index.js +++ b/lib/modules/links/index.js @@ -12,6 +12,10 @@ const EVENT_TYPE = { Link: 'dynamic_link_received', }; +const NATIVE_EVENTS = [ + EVENT_TYPE.Link, +]; + function validateParameters(parameters: Object): void { const suportedParametersObject = { dynamicLinkDomain: 'string', @@ -67,7 +71,7 @@ export default class Links extends ModuleBase { static _NATIVE_MODULE = 'RNFirebaseLinks'; constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); } get EVENT_TYPE(): Object { diff --git a/lib/modules/messaging/index.js b/lib/modules/messaging/index.js index 4a631137..091b5b05 100644 --- a/lib/modules/messaging/index.js +++ b/lib/modules/messaging/index.js @@ -32,6 +32,11 @@ const WILL_PRESENT_RESULT = { None: 'UNNotificationPresentationOptionNone', }; +const NATIVE_EVENTS = [ + EVENT_TYPE.RefreshToken, + EVENT_TYPE.Notification, +]; + const FirebaseMessaging = NativeModules.RNFirebaseMessaging; /** @@ -83,7 +88,7 @@ export default class Messaging extends ModuleBase { static _NATIVE_MODULE = 'RNFirebaseMessaging'; constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); } get EVENT_TYPE(): Object { diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js index a8e12c87..358f7594 100644 --- a/lib/modules/storage/index.js +++ b/lib/modules/storage/index.js @@ -13,6 +13,11 @@ import type FirebaseApp from '../core/firebase-app'; const FirebaseStorage = NativeModules.RNFirebaseStorage; +const NATIVE_EVENTS = [ + 'storage_event', + 'storage_error', +]; + export default class Storage extends ModuleBase { static _NAMESPACE = 'storage'; static _NATIVE_MODULE = 'RNFirebaseStorage'; @@ -23,7 +28,7 @@ export default class Storage extends ModuleBase { * @param options */ constructor(firebaseApp: FirebaseApp, options: Object = {}) { - super(firebaseApp, options, true); + super(firebaseApp, options, NATIVE_EVENTS); SharedEventEmitter.addListener( getAppEventName(this, 'storage_event'), diff --git a/lib/utils/ModuleBase.js b/lib/utils/ModuleBase.js index 1b432583..cb0a974f 100644 --- a/lib/utils/ModuleBase.js +++ b/lib/utils/ModuleBase.js @@ -29,7 +29,7 @@ export default class ModuleBase { * @param options * @param withEventEmitter */ - constructor(firebaseApp: FirebaseApp, options: Object, withEventEmitter: boolean = false) { + constructor(firebaseApp: FirebaseApp, options: Object, events: string[] = []) { this._module = this.constructor._NATIVE_MODULE.replace('RNFirebase', ''); this._firebaseApp = firebaseApp; this._appName = firebaseApp._name; @@ -41,9 +41,7 @@ export default class ModuleBase { // TODO: Get rid of this._native = getNativeModule(this); - if (withEventEmitter) { - initialiseNativeModuleEventEmitter(this); - } + initialiseNativeModuleEventEmitter(this, events); } /** diff --git a/lib/utils/events.js b/lib/utils/events.js index 07a0e1e3..90be83c1 100644 --- a/lib/utils/events.js +++ b/lib/utils/events.js @@ -7,24 +7,6 @@ import { getRawNativeModule } from './native'; import type ModuleBase from './ModuleBase'; -const NATIVE_MODULE_EVENTS = { - Storage: [ - 'storage_event', - 'storage_error', - ], - Auth: [ - 'auth_state_changed', - 'phone_auth_state_changed', - ], - Database: [ - 'database_transaction_event', - // 'database_server_offset', // TODO - ], - Firestore: [ - 'firestore_collection_sync_event', - 'firestore_document_sync_event', - ], -}; const NATIVE_EMITTERS: { [string]: NativeEventEmitter } = {}; const NATIVE_SUBSCRIPTIONS: { [string]: boolean } = {}; @@ -70,9 +52,7 @@ const subscribeToNativeModuleEvents = (module: ModuleBase, eventName: string): v } }; -export const initialiseNativeModuleEventEmitter = (module: ModuleBase): void => { - const events = NATIVE_MODULE_EVENTS[module._module]; - +export const initialiseNativeModuleEventEmitter = (module: ModuleBase, events: string[]): void => { if (events && events.length) { for (let i = 0, len = events.length; i < len; i++) { subscribeToNativeModuleEvents(module, events[i]);