[internals] Move native events to individual modules to make it easier to see the relationship

This commit is contained in:
Chris Bianca 2017-12-22 15:57:33 +00:00
parent f2c2007fdc
commit 61d7c3b2eb
9 changed files with 44 additions and 32 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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(

View File

@ -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 {

View File

@ -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 {

View File

@ -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'),

View File

@ -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);
}
/**

View File

@ -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]);