react-native-firebase-compiled
Version:
A well tested, feature rich Firebase implementation for React Native, supporting iOS & Android. Individual module support for Admob, Analytics, Auth, Crash Reporting, Cloud Firestore, Database, Dynamic Links, Functions, Messaging (FCM), Remote Config, Sto
73 lines (58 loc) • 2.38 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.initialiseNativeModuleEventEmitter = exports.getAppEventName = exports.SharedEventEmitter = void 0;
var _reactNative = require("react-native");
var _EventEmitter = _interopRequireDefault(require("react-native/Libraries/vendor/emitter/EventEmitter"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const NATIVE_EMITTERS = {};
const NATIVE_SUBSCRIPTIONS = {};
const SharedEventEmitter = new _EventEmitter.default();
exports.SharedEventEmitter = SharedEventEmitter;
const getAppEventName = (module, eventName) => `${module.app.name}-${eventName}`;
exports.getAppEventName = getAppEventName;
const getNativeEmitter = (moduleName, module) => {
const name = `${module.app.name}-${moduleName}`;
const nativeModule = _reactNative.NativeModules[moduleName];
if (!NATIVE_EMITTERS[name]) {
NATIVE_EMITTERS[name] = new _reactNative.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 moduleName
* @param module
* @param eventName
* @private
*/
const subscribeToNativeModuleEvents = (moduleName, module, eventName) => {
if (!NATIVE_SUBSCRIPTIONS[eventName]) {
const nativeEmitter = getNativeEmitter(moduleName, module);
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;
}
};
const initialiseNativeModuleEventEmitter = (module, config) => {
const events = config.events,
moduleName = config.moduleName;
if (events && events.length) {
for (let i = 0, len = events.length; i < len; i++) {
subscribeToNativeModuleEvents(moduleName, module, events[i]);
}
}
};
exports.initialiseNativeModuleEventEmitter = initialiseNativeModuleEventEmitter;
;