/
index.js
67 lines (58 loc) · 1.63 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
// @flow
import type { DevToolsHook, ReactRenderer, RendererInterface } from './types';
import Agent from './agent';
import { attach } from './renderer';
export function initBackend(
hook: DevToolsHook,
agent: Agent,
global: Object
): void {
const subs = [
hook.sub(
'renderer-attached',
({
id,
renderer,
rendererInterface,
}: {
id: number,
renderer: ReactRenderer,
rendererInterface: RendererInterface,
}) => {
agent.setRendererInterface(id, rendererInterface);
rendererInterface.walkTree();
}
),
hook.sub('operations', agent.onHookOperations),
// TODO Add additional subscriptions required for profiling mode
];
const attachRenderer = (id: number, renderer: ReactRenderer) => {
const rendererInterface = attach(hook, id, renderer, global);
hook.rendererInterfaces.set(id, rendererInterface);
hook.emit('renderer-attached', {
id,
renderer,
rendererInterface,
});
};
// Connect renderers that have already injected themselves.
hook.renderers.forEach((renderer, id) => {
attachRenderer(id, renderer);
});
// Connect any new renderers that injected themselves.
hook.on(
'renderer',
({ id, renderer }: { id: number, renderer: ReactRenderer }) => {
attachRenderer(id, renderer);
}
);
hook.emit('react-devtools', agent);
hook.reactDevtoolsAgent = agent;
agent.addListener('shutdown', () => {
subs.forEach(fn => fn());
hook.rendererInterfaces.forEach(rendererInterface => {
rendererInterface.cleanup();
});
hook.reactDevtoolsAgent = null;
});
}