import { NativeEventEmitter, NativeModules, DeviceEventEmitter, Platform, } from 'react-native'; type EventCallback = (info: any) => void; type EventMap = { [key: string]: EventCallback }; const { RangersAppLogEmitter } = NativeModules; const prefix = 'RangersAppLogEmitter_'; const subscriptionMap = new Map(); const isIOS = Platform.OS === 'ios'; let emitter: any = DeviceEventEmitter; if (isIOS) { emitter = new NativeEventEmitter(RangersAppLogEmitter); } function bindEvent(eventName: string, callback: EventCallback) { if (!isIOS) { eventName = prefix + eventName; } const subscription = emitter.addListener(eventName, callback); subscriptionMap.set(eventName, subscription); } function unbindEvent(eventName: string) { if (!isIOS) { eventName = prefix + eventName; } const subscription = subscriptionMap.get(eventName); if (subscription) { subscription.remove(); } } const eventEmitter = { bindEvents(eventMap: EventMap) { Object.keys(eventMap).forEach((key) => { bindEvent(key, eventMap[key]); }); RangersAppLogEmitter.connect(); }, unbindEvents(...eventNames: string[]) { eventNames.forEach((eventName) => { unbindEvent(eventName); }); }, }; export default eventEmitter;