|
4 | 4 | * This source code is licensed under the MIT license found in the
|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | *
|
7 |
| - * @format |
8 | 7 | * @flow strict-local
|
| 8 | + * @format |
9 | 9 | */
|
10 | 10 |
|
11 | 11 | import NativeDevSettings from '../NativeModules/specs/NativeDevSettings';
|
12 | 12 | import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
|
13 | 13 |
|
14 |
| -interface IDevSettings { |
15 |
| - addMenuItem(title: string, handler: () => mixed): void; |
16 |
| - reload(reason?: string): void; |
17 |
| - onFastRefresh(): void; |
18 |
| -} |
| 14 | +let DevSettings: { |
| 15 | + addMenuItem(title: string, handler: () => mixed): void, |
| 16 | + reload(reason?: string): void, |
| 17 | + onFastRefresh(): void, |
| 18 | +} = { |
| 19 | + addMenuItem(title: string, handler: () => mixed): void {}, |
| 20 | + reload(reason?: string): void {}, |
| 21 | + onFastRefresh(): void {}, |
| 22 | +}; |
19 | 23 |
|
20 | 24 | type DevSettingsEventDefinitions = {
|
21 | 25 | didPressMenuItem: [{title: string}],
|
22 | 26 | };
|
23 | 27 |
|
24 |
| -class DevSettings extends NativeEventEmitter<DevSettingsEventDefinitions> |
25 |
| - implements IDevSettings { |
26 |
| - _menuItems: Map<string, () => mixed>; |
27 |
| - |
28 |
| - constructor() { |
29 |
| - super(NativeDevSettings); |
30 |
| - |
31 |
| - this._menuItems = new Map(); |
32 |
| - } |
| 28 | +if (__DEV__) { |
| 29 | + const emitter = new NativeEventEmitter<DevSettingsEventDefinitions>( |
| 30 | + NativeDevSettings, |
| 31 | + ); |
| 32 | + const menuItems = new Map(); |
33 | 33 |
|
34 |
| - addMenuItem(title: string, handler: () => mixed) { |
35 |
| - // Make sure items are not added multiple times. This can |
36 |
| - // happen when hot reloading the module that registers the |
37 |
| - // menu items. The title is used as the id which means we |
38 |
| - // don't support multiple items with the same name. |
39 |
| - const oldHandler = this._menuItems.get(title); |
40 |
| - if (oldHandler != null) { |
41 |
| - this.removeListener('didPressMenuItem', oldHandler); |
42 |
| - } else { |
43 |
| - NativeDevSettings.addMenuItem(title); |
44 |
| - } |
45 |
| - |
46 |
| - this._menuItems.set(title, handler); |
47 |
| - this.addListener('didPressMenuItem', event => { |
48 |
| - if (event.title === title) { |
49 |
| - handler(); |
| 34 | + DevSettings = { |
| 35 | + addMenuItem(title: string, handler: () => mixed): void { |
| 36 | + // Make sure items are not added multiple times. This can |
| 37 | + // happen when hot reloading the module that registers the |
| 38 | + // menu items. The title is used as the id which means we |
| 39 | + // don't support multiple items with the same name. |
| 40 | + const oldHandler = menuItems.get(title); |
| 41 | + if (oldHandler != null) { |
| 42 | + emitter.removeListener('didPressMenuItem', oldHandler); |
| 43 | + } else { |
| 44 | + NativeDevSettings.addMenuItem(title); |
50 | 45 | }
|
51 |
| - }); |
52 |
| - } |
53 |
| - |
54 |
| - reload(reason?: string) { |
55 |
| - if (typeof NativeDevSettings.reloadWithReason === 'function') { |
56 |
| - NativeDevSettings.reloadWithReason(reason ?? 'Uncategorized from JS'); |
57 |
| - } else { |
58 |
| - NativeDevSettings.reload(); |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - onFastRefresh() { |
63 |
| - if (typeof NativeDevSettings.onFastRefresh === 'function') { |
64 |
| - NativeDevSettings.onFastRefresh(); |
65 |
| - } |
66 |
| - } |
67 | 46 |
|
68 |
| - // TODO: Add other dev setting methods exposed by the native module. |
69 |
| -} |
70 |
| - |
71 |
| -// Avoid including the full `NativeDevSettings` class in prod. |
72 |
| -class NoopDevSettings implements IDevSettings { |
73 |
| - addMenuItem(title: string, handler: () => mixed) {} |
74 |
| - reload(reason?: string) {} |
75 |
| - onFastRefresh() {} |
| 47 | + menuItems.set(title, handler); |
| 48 | + emitter.addListener('didPressMenuItem', event => { |
| 49 | + if (event.title === title) { |
| 50 | + handler(); |
| 51 | + } |
| 52 | + }); |
| 53 | + }, |
| 54 | + reload(reason?: string): void { |
| 55 | + if (NativeDevSettings.reloadWithReason != null) { |
| 56 | + NativeDevSettings.reloadWithReason(reason ?? 'Uncategorized from JS'); |
| 57 | + } else { |
| 58 | + NativeDevSettings.reload(); |
| 59 | + } |
| 60 | + }, |
| 61 | + onFastRefresh(): void { |
| 62 | + NativeDevSettings.onFastRefresh?.(); |
| 63 | + }, |
| 64 | + }; |
76 | 65 | }
|
77 | 66 |
|
78 |
| -module.exports = ((__DEV__ |
79 |
| - ? new DevSettings() |
80 |
| - : new NoopDevSettings()): IDevSettings); |
| 67 | +module.exports = DevSettings; |
0 commit comments