forked from zoontek/react-native-dev-menu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (36 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// @flow
'use strict';
// $FlowFixMe
const ReactNative = require('react-native');
const NativeEventEmitter = ReactNative.NativeEventEmitter;
const RNDevMenu = ReactNative.NativeModules.RNDevMenu;
type RNDevMenuModule = {
_eventHandlers?: Map<string, () => any>,
_eventEmitter?: NativeEventEmitter,
addItem: (name: string, handler: () => any) => Promise<void>
};
let DevMenu: RNDevMenuModule = {
addItem(name, handler) {
// $FlowFixMe
if (!__DEV__) {
return Promise.resolve();
}
if (!this._eventHandlers) {
this._eventHandlers = 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);
if (handler) {
handler();
}
}
});
}
return RNDevMenu.addItem(name);
}
};
module.exports = DevMenu;