Skip to content

Commit 70cd569

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Simplify DevSettings Implementation
Summary: Simplifies the implementation of `DevSettings`. The fact that it uses `NativeEventEmitter` is an implementation detail and does not need to be exposed via inheritance. This also enables more code to be removed from production builds (because the development implementation is now statically enclosed by `__DEV__`). Changelog: [General][Changed] - `DevSettings` no longer inherits from `NativeEventEmitter` Reviewed By: RSNara Differential Revision: D26140148 fbshipit-source-id: 99fa9d0c6ce8e365f89936aa12a4720f7a04b984
1 parent f5f4787 commit 70cd569

File tree

1 file changed

+45
-58
lines changed

1 file changed

+45
-58
lines changed

Libraries/Utilities/DevSettings.js

+45-58
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,64 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict-local
8+
* @format
99
*/
1010

1111
import NativeDevSettings from '../NativeModules/specs/NativeDevSettings';
1212
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
1313

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+
};
1923

2024
type DevSettingsEventDefinitions = {
2125
didPressMenuItem: [{title: string}],
2226
};
2327

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();
3333

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);
5045
}
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-
}
6746

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+
};
7665
}
7766

78-
module.exports = ((__DEV__
79-
? new DevSettings()
80-
: new NoopDevSettings()): IDevSettings);
67+
module.exports = DevSettings;

0 commit comments

Comments
 (0)