Repository navigation
Expand file tree
/
Copy pathreactTreeSource.ts
More file actions
51 lines (50 loc) · 2.1 KB
/
Copy pathreactTreeSource.ts
File metadata and controls
51 lines (50 loc) · 2.1 KB
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
import { MAX_ROOT_SCAN } from '../constants';
/**
* Page-side, in the Components tree (`TREE_JS`): React's part. Its top is the roots' components (the hook's
* roots, else the containers React marks, the first `MAX_ROOT_SCAN` elements looked through); a node's
* children are the nearest component fibers under it, and it leads to its first element.
*/
export const REACT_TREE_JS = `
const reactRoots = () => {
const roots = new Set();
const hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
if (hook && hook.renderers && typeof hook.getFiberRoots === 'function') {
for (const id of hook.renderers.keys()) for (const root of hook.getFiberRoots(id)) roots.add(root);
}
const elements = roots.size ? [] : document.querySelectorAll('*');
for (let i = 0; i < elements.length && i < ${MAX_ROOT_SCAN}; i++) {
const container = elements[i];
const key = Object.keys(container).find((k) => k.startsWith('__reactContainer$'));
const legacy = container._reactRootContainer && container._reactRootContainer._internalRoot;
if (key && container[key] && container[key].stateNode) roots.add(container[key].stateNode);
else if (legacy) roots.add(legacy);
}
return [...roots].filter((root) => root.current);
};
const reactKids = (fiber) => {
const out = [];
const walk = (parent) => {
for (let child = parent.child; child; child = child.sibling) {
if (isComponent(child)) out.push(child);
else walk(child);
}
};
walk(fiber);
return out;
};
const reactElement = (fiber) => {
if (fiber.stateNode instanceof Element) return fiber.stateNode;
for (let child = fiber.child; child; child = child.sibling) {
const found = reactElement(child);
if (found) return found;
}
return null;
};
const REACT_TREE = {
top: () => reactRoots().flatMap((root) => reactKids(root.current)),
kids: reactKids,
element: reactElement,
node: (f, fn) => ({ name: componentName(f.type), key: f.key == null ? null : String(f.key), fn: fn(renderFunction(f.type)) }),
same: (a, b) => a === b || a === b.alternate,
};
`;