From 45ab2ea6e617b877fe3162fc191a5019a63cd644 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Sat, 14 Mar 2020 19:45:08 +0100 Subject: [PATCH] Avoid exposition of _eventHandlers / _eventEmitter --- index.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 87e2e79..8e6751d 100644 --- a/index.js +++ b/index.js @@ -7,9 +7,10 @@ const ReactNative = require('react-native'); const NativeEventEmitter = ReactNative.NativeEventEmitter; const RNDevMenu = ReactNative.NativeModules.RNDevMenu; +let emitter: NativeEventEmitter | void; +let handlers: Map any> | void; + type RNDevMenuModule = { - _eventHandlers?: Map any>, - _eventEmitter?: NativeEventEmitter, addItem: (name: string, handler: () => any) => Promise }; @@ -19,25 +20,21 @@ let DevMenu: RNDevMenuModule = { return Promise.resolve(); } - if (!this._eventHandlers) { - this._eventHandlers = new Map(); + if (!emitter) { + emitter = new NativeEventEmitter(RNDevMenu); + } + if (!handlers) { + handlers = new Map(); } - this._eventHandlers.set(name, handler); - - if (!this._eventEmitter) { - this._eventEmitter = new NativeEventEmitter(RNDevMenu); - - this._eventEmitter.addListener('customDevOptionTap', (name: string) => { - if (this._eventHandlers) { - const handler = this._eventHandlers.get(name); + handlers.set(name, handler); - if (handler) { - handler(); - } - } - }); - } + emitter.addListener('customDevOptionTap', (name: string) => { + if (handlers) { + const handler = handlers.get(name); + handler && handler(); + } + }); return RNDevMenu.addItem(name); }