-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathoperations.ts
More file actions
183 lines (169 loc) · 4.61 KB
/
operations.ts
File metadata and controls
183 lines (169 loc) · 4.61 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { DevNodeType, ID, Tree } from "../../view/store/types";
import { parseTable } from "./string-table";
import { MsgTypes } from "./events";
import { deepClone } from "../../view/components/profiler/flamegraph/modes/adjustNodesToRight";
import { RenderReasonMap } from "../shared/renderReasons";
import { ParsedStats, parseStats } from "../shared/stats";
/**
* This is the heart of the devtools. Here we translate incoming events
* and construct the tree data structure which all operations in the
* Devtools UI are based upon.
*
* We currently expect all operations to be in order.
*/
export function ops2Tree(oldTree: Tree, existingRoots: ID[], ops: number[]) {
const pending: Tree = new Map(oldTree);
const rootId = ops[0];
const roots: ID[] = [...existingRoots];
const removals: ID[] = [];
const reasons: RenderReasonMap = new Map();
let stats: ParsedStats | null = null;
const rendered: ID[] = [];
let i = ops[1] + 1;
const strings = parseTable(ops.slice(1, i + 1));
for (i += 1; i < ops.length; i++) {
switch (ops[i]) {
case MsgTypes.ADD_ROOT: {
const id = ops[i + 1];
roots.push(id);
i += 1;
pending.set(id, {
children: [],
depth: -1,
id,
hocs: null,
name: "__VIRTUAL__ROOT__",
parent: -1,
type: DevNodeType.Group,
key: "",
startTime: -1,
endTime: -1,
});
break;
}
case MsgTypes.ADD_VNODE: {
const id = ops[i + 1];
const parentId = ops[i + 3];
const parent = pending.get(parentId);
if (parent) {
const clone = deepClone(parent);
pending.set(parent.id, clone);
clone.children.push(id);
}
rendered.push(id);
pending.set(id, {
children: [],
depth: parent ? parent.depth + 1 : 0,
id,
hocs: null,
name: strings[ops[i + 5] - 1],
parent: parentId,
type: ops[i + 2],
key: ops[i + 6] > 0 ? strings[ops[i + 6] - 1] : "",
startTime: ops[i + 7] / 1000,
endTime: ops[i + 8] / 1000,
});
i += 8;
break;
}
case MsgTypes.UPDATE_VNODE_TIMINGS: {
const id = ops[i + 1];
pending.set(id, deepClone(pending.get(id)!));
const x = pending.get(id)!;
x.startTime = ops[i + 2] / 1000;
x.endTime = ops[i + 3] / 1000;
rendered.push(id);
i += 3;
break;
}
case MsgTypes.REMOVE_VNODE: {
const unmounts = ops[i + 1];
i += 2;
const len = i + unmounts;
for (; i < len; i++) {
const nodeId = ops[i];
removals.push(nodeId);
const node = pending.get(nodeId);
if (node) {
// Remove node from parent children array
const parent = pending.get(node.parent);
if (parent) {
const idx = parent.children.indexOf(nodeId);
if (idx > -1) {
const clone = deepClone(parent);
pending.set(parent.id, clone);
clone.children.splice(idx, 1);
}
}
// Check if node was a root
const rootIdx = roots.indexOf(node.parent);
if (rootIdx > -1) {
roots.splice(rootIdx, 1);
}
// Delete children recursively
const stack = [node.id];
let item;
while ((item = stack.pop())) {
const child = pending.get(item);
if (!child) continue;
pending.delete(child.id);
stack.push(...child.children);
}
pending.delete(nodeId);
}
}
// Subtract one because of outer loop
if (len > 0) i--;
break;
}
case MsgTypes.REORDER_CHILDREN: {
const parentId = ops[i + 1];
const count = ops[i + 2];
const parent = deepClone(pending.get(parentId)!);
parent.children = ops.slice(i + 3, i + 3 + count);
pending.set(parentId, parent);
i = i + 2 + count;
break;
}
case MsgTypes.RENDER_REASON: {
const id = ops[i + 1];
const type = ops[i + 2];
const count = ops[i + 3];
let items: string[] | null = null;
if (count > 0) {
items = ops.slice(i + 4, i + 4 + count).map(x => strings[x - 1]);
}
reasons.set(id, {
type,
items,
});
i = i + 3 + count;
break;
}
case MsgTypes.COMMIT_STATS: {
const count = ops[i + 1];
const statsOps = ops.slice(i + 1, i + 1 + count);
stats = parseStats(statsOps);
i = i + 1 + count;
break;
}
case MsgTypes.HOC_NODES: {
const vnodeId = ops[i + 1];
const vnode = pending.get(vnodeId);
const count = ops[i + 2];
if (vnode) {
const hocs = [];
for (let j = 0; j < count; j++) {
hocs.push(strings[ops[i + 3 + j] - 1]);
}
vnode.hocs = hocs;
}
i = i + 2 + count;
break;
}
default:
throw new Error("Unknown event: " + ops[i]);
}
}
return { rootId, roots, tree: pending, removals, reasons, stats, rendered };
}