-
Notifications
You must be signed in to change notification settings - Fork 47k
/
index.js
100 lines (84 loc) · 2.55 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* 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
*/
import Agent from './agent';
import type {DevToolsHook, RendererID, RendererInterface} from './types';
export type InitBackend = typeof initBackend;
export function initBackend(
hook: DevToolsHook,
agent: Agent,
global: Object,
): () => void {
if (hook == null) {
// DevTools didn't get injected into this page (maybe b'c of the contentType).
return () => {};
}
function registerRendererInterface(
id: RendererID,
rendererInterface: RendererInterface,
) {
agent.registerRendererInterface(id, rendererInterface);
// Now that the Store and the renderer interface are connected,
// it's time to flush the pending operation codes to the frontend.
rendererInterface.flushInitialOperations();
}
const subs = [
hook.sub(
'renderer-attached',
({
id,
rendererInterface,
}: {
id: number,
rendererInterface: RendererInterface,
}) => {
registerRendererInterface(id, rendererInterface);
},
),
hook.sub('unsupported-renderer-version', () => {
agent.onUnsupportedRenderer();
}),
hook.sub('fastRefreshScheduled', agent.onFastRefreshScheduled),
hook.sub('operations', agent.onHookOperations),
hook.sub('traceUpdates', agent.onTraceUpdates),
hook.sub('settingsInitialized', agent.onHookSettings),
// TODO Add additional subscriptions required for profiling mode
];
agent.addListener('getIfHasUnsupportedRendererVersion', () => {
if (hook.hasUnsupportedRendererAttached) {
agent.onUnsupportedRenderer();
}
});
hook.rendererInterfaces.forEach((rendererInterface, id) => {
registerRendererInterface(id, rendererInterface);
});
hook.emit('react-devtools', agent);
hook.reactDevtoolsAgent = agent;
const onAgentShutdown = () => {
subs.forEach(fn => fn());
hook.rendererInterfaces.forEach(rendererInterface => {
rendererInterface.cleanup();
});
hook.reactDevtoolsAgent = null;
};
agent.addListener('shutdown', onAgentShutdown);
subs.push(() => {
agent.removeListener('shutdown', onAgentShutdown);
});
agent.addListener('updateHookSettings', settings => {
hook.settings = settings;
});
agent.addListener('getHookSettings', () => {
if (hook.settings != null) {
agent.onHookSettings(hook.settings);
}
});
return () => {
subs.forEach(fn => fn());
};
}