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
This commit is contained in:
Tim Yung 2021-02-01 17:47:09 -08:00 коммит произвёл Facebook GitHub Bot
Родитель f5f47879b8
Коммит 70cd569e7e
1 изменённых файлов: 45 добавлений и 58 удалений

Просмотреть файл

@ -4,77 +4,64 @@
* This source code is licensed under the MIT license found in the * This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
* *
* @format
* @flow strict-local * @flow strict-local
* @format
*/ */
import NativeDevSettings from '../NativeModules/specs/NativeDevSettings'; import NativeDevSettings from '../NativeModules/specs/NativeDevSettings';
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
interface IDevSettings { let DevSettings: {
addMenuItem(title: string, handler: () => mixed): void; addMenuItem(title: string, handler: () => mixed): void,
reload(reason?: string): void; reload(reason?: string): void,
onFastRefresh(): void; onFastRefresh(): void,
} } = {
addMenuItem(title: string, handler: () => mixed): void {},
reload(reason?: string): void {},
onFastRefresh(): void {},
};
type DevSettingsEventDefinitions = { type DevSettingsEventDefinitions = {
didPressMenuItem: [{title: string}], didPressMenuItem: [{title: string}],
}; };
class DevSettings extends NativeEventEmitter<DevSettingsEventDefinitions> if (__DEV__) {
implements IDevSettings { const emitter = new NativeEventEmitter<DevSettingsEventDefinitions>(
_menuItems: Map<string, () => mixed>; NativeDevSettings,
);
const menuItems = new Map();
constructor() { DevSettings = {
super(NativeDevSettings); addMenuItem(title: string, handler: () => mixed): void {
// Make sure items are not added multiple times. This can
this._menuItems = new Map(); // happen when hot reloading the module that registers the
} // menu items. The title is used as the id which means we
// don't support multiple items with the same name.
addMenuItem(title: string, handler: () => mixed) { const oldHandler = menuItems.get(title);
// Make sure items are not added multiple times. This can if (oldHandler != null) {
// happen when hot reloading the module that registers the emitter.removeListener('didPressMenuItem', oldHandler);
// menu items. The title is used as the id which means we } else {
// don't support multiple items with the same name. NativeDevSettings.addMenuItem(title);
const oldHandler = this._menuItems.get(title);
if (oldHandler != null) {
this.removeListener('didPressMenuItem', oldHandler);
} else {
NativeDevSettings.addMenuItem(title);
}
this._menuItems.set(title, handler);
this.addListener('didPressMenuItem', event => {
if (event.title === title) {
handler();
} }
});
}
reload(reason?: string) { menuItems.set(title, handler);
if (typeof NativeDevSettings.reloadWithReason === 'function') { emitter.addListener('didPressMenuItem', event => {
NativeDevSettings.reloadWithReason(reason ?? 'Uncategorized from JS'); if (event.title === title) {
} else { handler();
NativeDevSettings.reload(); }
} });
} },
reload(reason?: string): void {
onFastRefresh() { if (NativeDevSettings.reloadWithReason != null) {
if (typeof NativeDevSettings.onFastRefresh === 'function') { NativeDevSettings.reloadWithReason(reason ?? 'Uncategorized from JS');
NativeDevSettings.onFastRefresh(); } else {
} NativeDevSettings.reload();
} }
},
// TODO: Add other dev setting methods exposed by the native module. onFastRefresh(): void {
NativeDevSettings.onFastRefresh?.();
},
};
} }
// Avoid including the full `NativeDevSettings` class in prod. module.exports = DevSettings;
class NoopDevSettings implements IDevSettings {
addMenuItem(title: string, handler: () => mixed) {}
reload(reason?: string) {}
onFastRefresh() {}
}
module.exports = ((__DEV__
? new DevSettings()
: new NoopDevSettings()): IDevSettings);