-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDevSettings.js
72 lines (64 loc) · 2.35 KB
/
DevSettings.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
import NativeDevSettings from '../NativeModules/specs/NativeDevSettings';
import Platform from '../Utilities/Platform';
let DevSettings: {
addMenuItem(title: string, handler: () => mixed): void,
reload(reason?: string): void,
onFastRefresh(): void,
} = {
addMenuItem(title: string, handler: () => mixed): void {},
reload(reason?: string): void {},
onFastRefresh(): void {},
};
type DevSettingsEventDefinitions = {
didPressMenuItem: [{title: string}],
};
if (__DEV__) {
const emitter = new NativeEventEmitter<DevSettingsEventDefinitions>(
// T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior
// If you want to use the native module on other platforms, please remove this condition and test its behavior
Platform.OS !== 'ios' ? null : NativeDevSettings,
);
const subscriptions = new Map<string, EventSubscription>();
DevSettings = {
addMenuItem(title: string, handler: () => mixed): void {
// Make sure items are not added multiple times. This can
// 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.
let subscription = subscriptions.get(title);
if (subscription != null) {
subscription.remove();
} else {
NativeDevSettings.addMenuItem(title);
}
subscription = emitter.addListener('didPressMenuItem', event => {
if (event.title === title) {
handler();
}
});
subscriptions.set(title, subscription);
},
reload(reason?: string): void {
if (NativeDevSettings.reloadWithReason != null) {
NativeDevSettings.reloadWithReason(reason ?? 'Uncategorized from JS');
} else {
NativeDevSettings.reload();
}
},
onFastRefresh(): void {
NativeDevSettings.onFastRefresh?.();
},
};
}
module.exports = DevSettings;