Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Avoid exposition of _eventHandlers / _eventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek committed Mar 14, 2020
1 parent fbd0768 commit 45ab2ea
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, () => any> | void;

type RNDevMenuModule = {
_eventHandlers?: Map<string, () => any>,
_eventEmitter?: NativeEventEmitter,
addItem: (name: string, handler: () => any) => Promise<void>
};

Expand All @@ -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);
}
Expand Down

0 comments on commit 45ab2ea

Please sign in to comment.