2017-12-22 15:24:31 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
*/
|
2018-01-03 20:00:38 +00:00
|
|
|
import { NativeEventEmitter, NativeModules } from 'react-native';
|
2017-12-22 15:24:31 +00:00
|
|
|
import EventEmitter from './emitter/EventEmitter';
|
|
|
|
|
|
|
|
import type ModuleBase from './ModuleBase';
|
2018-01-03 20:00:38 +00:00
|
|
|
import type { FirebaseModuleConfig, FirebaseModuleName } from '../types';
|
2017-12-22 15:24:31 +00:00
|
|
|
|
|
|
|
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}`;
|
2017-12-22 15:24:31 +00:00
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
const getNativeEmitter = (
|
|
|
|
moduleName: FirebaseModuleName,
|
|
|
|
module: ModuleBase
|
|
|
|
): NativeEventEmitter => {
|
2018-01-03 20:00:38 +00:00
|
|
|
const name = `${module.app.name}-${moduleName}`;
|
|
|
|
const nativeModule = NativeModules[moduleName];
|
2017-12-22 15:24:31 +00:00
|
|
|
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 => {
|
2017-12-22 15:24:31 +00:00
|
|
|
if (!NATIVE_SUBSCRIPTIONS[eventName]) {
|
2018-01-03 20:00:38 +00:00
|
|
|
const nativeEmitter = getNativeEmitter(moduleName, module);
|
2018-01-25 18:25:39 +00:00
|
|
|
nativeEmitter.addListener(eventName, event => {
|
2017-12-22 15:24:31 +00:00
|
|
|
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 => {
|
2018-01-03 20:00:38 +00:00
|
|
|
const { events, moduleName } = config;
|
2017-12-22 15:24:31 +00:00
|
|
|
if (events && events.length) {
|
|
|
|
for (let i = 0, len = events.length; i < len; i++) {
|
2018-01-03 20:00:38 +00:00
|
|
|
subscribeToNativeModuleEvents(moduleName, module, events[i]);
|
2017-12-22 15:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|