react-native-firebase/lib/utils/events.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

/**
* @flow
*/
import { NativeEventEmitter, NativeModules } from 'react-native';
import EventEmitter from './emitter/EventEmitter';
import type ModuleBase from './ModuleBase';
import type { FirebaseModuleConfig, FirebaseModuleName } from '../types';
const NATIVE_EMITTERS: { [string]: NativeEventEmitter } = {};
const NATIVE_SUBSCRIPTIONS: { [string]: boolean } = {};
export const SharedEventEmitter = new EventEmitter();
2018-01-25 18:25:39 +00:00
export const getAppEventName = (
module: ModuleBase,
eventName: string
): string => `${module.app.name}-${eventName}`;
2018-01-25 18:25:39 +00:00
const getNativeEmitter = (
moduleName: FirebaseModuleName,
module: ModuleBase
): NativeEventEmitter => {
const name = `${module.app.name}-${moduleName}`;
const nativeModule = NativeModules[moduleName];
if (!NATIVE_EMITTERS[name]) {
NATIVE_EMITTERS[name] = new NativeEventEmitter(nativeModule);
}
return NATIVE_EMITTERS[name];
};
/**
* Subscribe to a native event for js side distribution by appName
* React Native events are hard set at compile - cant do dynamic event names
* so we use a single event send it to js and js then internally can prefix it
* and distribute dynamically.
*
* @param module
* @param eventName
* @private
*/
2018-01-25 18:25:39 +00:00
const subscribeToNativeModuleEvents = (
moduleName: FirebaseModuleName,
module: ModuleBase,
eventName: string
): void => {
if (!NATIVE_SUBSCRIPTIONS[eventName]) {
const nativeEmitter = getNativeEmitter(moduleName, module);
2018-01-25 18:25:39 +00:00
nativeEmitter.addListener(eventName, event => {
if (event.appName) {
// native event has an appName property - auto prefix and internally emit
SharedEventEmitter.emit(`${event.appName}-${eventName}`, event);
} else {
// standard event - no need to prefix
SharedEventEmitter.emit(eventName, event);
}
});
NATIVE_SUBSCRIPTIONS[eventName] = true;
}
};
2018-01-25 18:25:39 +00:00
export const initialiseNativeModuleEventEmitter = (
module: ModuleBase,
config: FirebaseModuleConfig
): void => {
const { events, moduleName } = config;
if (events && events.length) {
for (let i = 0, len = events.length; i < len; i++) {
subscribeToNativeModuleEvents(moduleName, module, events[i]);
}
}
};