-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathrenderer.ts
More file actions
342 lines (302 loc) · 8.56 KB
/
renderer.ts
File metadata and controls
342 lines (302 loc) · 8.56 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { BaseEvent, PortPageHook } from "../adapter/port";
import { Commit, flush } from "../protocol/events";
import { FunctionalComponent, ComponentConstructor, Options } from "preact";
import { ID, DevNodeType } from "../../view/store/types";
import { traverse } from "./utils";
import { FilterState } from "../adapter/filter";
import { Renderer } from "../renderer";
import { startDrawing } from "../adapter/highlightUpdates";
import { setIn, setInCopy } from "../shared/serialize";
import { createStats } from "../shared/stats";
import { ProfilerState } from "../adapter/profiler";
import {
getVNodeById,
getVNodeId,
hasVNodeId,
IdMappingState,
removeVNodeId,
} from "../shared/idMapper";
import { createCommit, shouldFilter } from "../shared/traverse";
import { PreactBindings, SharedVNode } from "../shared/bindings";
import { inspectVNode } from "./inspectVNode";
import { logVNode } from "../10/log";
import { VNodeTimings } from "./timings";
import { printCommit } from "../debug";
export interface RendererConfig {
Fragment: FunctionalComponent;
Component?: ComponentConstructor;
}
const memoReg = /^Memo\(/;
const forwardRefReg = /^ForwardRef\(/;
/**
* Get the type of a vnode. The devtools uses these constants to differentiate
* between the various forms of components.
*/
export function getDevtoolsType<T extends SharedVNode>(
vnode: T,
bindings: PreactBindings<T>,
): DevNodeType {
if (bindings.isComponent(vnode)) {
// TODO: Use getDisplayName here?
const name = vnode.type.displayName || "";
if (memoReg.test(name)) return DevNodeType.Memo;
if (forwardRefReg.test(name)) return DevNodeType.ForwardRef;
if (bindings.isSuspenseVNode(vnode)) return DevNodeType.Suspense;
if (bindings.isPortal(vnode)) return DevNodeType.Portal;
// TODO: Provider and Consumer
return vnode.type.prototype && vnode.type.prototype.render
? DevNodeType.ClassComponent
: DevNodeType.FunctionComponent;
}
return DevNodeType.Element;
}
export interface Supports {
renderReasons: boolean;
hooks: boolean;
}
export function createRenderer<T extends SharedVNode>(
port: PortPageHook,
config: RendererConfig,
options: Options,
supports: Supports,
profiler: ProfilerState,
filters: FilterState,
ids: IdMappingState<T>,
bindings: PreactBindings<T>,
timings: VNodeTimings,
): Renderer<T> {
const roots = new Map<T, ID>();
let currentUnmounts: number[] = [];
const domToVNode = new WeakMap<HTMLElement | Text, T>();
function onUnmount(vnode: T) {
if (!shouldFilter(vnode, filters, config, bindings)) {
if (hasVNodeId(ids, vnode)) {
currentUnmounts.push(getVNodeId(ids, vnode));
}
}
if (!bindings.isComponent(vnode)) {
const dom = bindings.getDom(vnode);
if (dom != null) domToVNode.delete(dom);
}
removeVNodeId(ids, vnode);
}
const inspect = (id: ID) => {
return inspectVNode(ids, config, bindings, options, id, supports.hooks);
};
return {
clear() {
roots.forEach((id, vnode) => {
onUnmount(vnode);
});
roots.clear();
},
getVNodeById: id => getVNodeById(ids, id),
getDisplayName(vnode) {
return bindings.getDisplayName(vnode, config);
},
log: (id, children) => logVNode(ids, config, id, children),
inspect,
findDomForVNode(id) {
const vnode = getVNodeById(ids, id);
if (!vnode) return null;
let first = null;
let last = null;
// Traverse tree until we find the first DOM node
let stack: any[] = [vnode];
let item;
while ((item = stack.shift()) !== undefined) {
if (item === null) continue;
if (!bindings.isComponent(item)) {
first = bindings.getDom(item);
break;
}
stack.push(...bindings.getActualChildren(item));
}
// If we traversed through every child, then there is
// no last child present.
if (first !== null) {
stack = [vnode];
while ((item = stack.pop()) !== undefined) {
if (item === null) continue;
if (!bindings.isComponent(item)) {
last = bindings.getDom(item);
break;
}
stack.push(...bindings.getActualChildren(item));
}
}
// Only set last if node is different. If both are the same
// we assume that we cannot show correct padding, margin and
// border visualizations and skip that.
return [first, first === last ? null : last];
},
findVNodeIdForDom(node) {
const vnode = domToVNode.get(node);
if (vnode) {
if (shouldFilter(vnode, filters, config, bindings)) {
let p: T | null = vnode;
let found = null;
while ((p = bindings.getVNodeParent(p)) != null) {
if (!shouldFilter(p, filters, config, bindings)) {
found = p;
break;
}
}
if (found != null) {
return getVNodeId(ids, found);
}
} else {
return getVNodeId(ids, vnode);
}
}
return -1;
},
refresh() {
this.applyFilters(filters);
},
applyFilters(nextFilters) {
/** Queue events and flush in one go */
const queue: BaseEvent<any, any>[] = [];
roots.forEach((rootId, root) => {
traverse(root, vnode => this.onUnmount(vnode), bindings);
const commit: Commit = {
operations: [],
rootId,
strings: new Map(),
unmountIds: currentUnmounts,
stats: profiler.recordStats ? createStats() : null,
};
if (commit.stats !== null) {
commit.stats.unmounts += commit.unmountIds.length;
}
const unmounts = flush(commit);
if (unmounts) {
currentUnmounts = [];
queue.push(unmounts);
}
});
filters.regex = nextFilters.regex;
filters.type = nextFilters.type;
roots.forEach(root => {
const commit = createCommit(
ids,
roots,
root,
filters,
domToVNode,
config,
profiler,
bindings,
timings,
{ start: new Map(), end: new Map() },
null,
);
const ev = flush(commit);
if (!ev) return;
queue.push(ev);
});
queue.forEach(ev => port.send(ev.type, ev.data));
},
onCommit(vnode, timingsByVNode, renderReasonPre) {
const commit = createCommit(
ids,
roots,
vnode,
filters,
domToVNode,
config,
profiler,
bindings,
timings,
timingsByVNode,
renderReasonPre,
);
timingsByVNode.start.clear();
timingsByVNode.end.clear();
if (commit.stats !== null) {
commit.stats.unmounts += currentUnmounts.length;
}
commit.unmountIds.push(...currentUnmounts);
currentUnmounts = [];
const ev = flush(commit);
if (!ev) return;
if (profiler.updateRects.size > 0) {
startDrawing(profiler.updateRects);
profiler.pendingHighlightUpdates.clear();
}
printCommit(ev.data);
port.send(ev.type as any, ev.data);
},
onUnmount,
update(id, type, path, value) {
const vnode = getVNodeById(ids, id);
if (vnode !== null) {
if (bindings.isComponent(vnode)) {
const c = bindings.getComponent(vnode);
if (c) {
if (type === "props") {
vnode.props = setInCopy(
(vnode.props as any) || {},
path.slice(),
value,
);
} else if (type === "state") {
const res = setInCopy(
(c.state as any) || {},
path.slice(),
value,
);
bindings.setNextState(c, res);
} else if (type === "context") {
// TODO: Investigate if we should disallow modifying context
// from devtools and make it readonly.
setIn((c.context as any) || {}, path.slice(), value);
}
c.forceUpdate();
}
}
}
},
updateHook(id, index, value) {
const vnode = getVNodeById(ids, id);
if (vnode !== null && bindings.isComponent(vnode)) {
const c = bindings.getComponent(vnode);
if (c) {
const s = bindings.getHookState(vnode, index);
// Only useState and useReducer hooks marked as editable so state can
// cast to more specific ReducerHookState value.
(s as [any, any])[0] = value;
c.forceUpdate();
}
}
},
suspend(id, active) {
let vnode = getVNodeById(ids, id);
while (vnode !== null) {
if (bindings.isSuspenseVNode(vnode)) {
const c = bindings.getComponent(vnode);
if (c) {
c.setState(bindings.createSuspenseState(vnode, active));
}
// Get nearest non-filtered vnode
let nearest: T | null = vnode;
while (nearest && shouldFilter(nearest, filters, config, bindings)) {
nearest = bindings.getVNodeParent(nearest);
}
if (nearest && hasVNodeId(ids, nearest)) {
const nearestId = getVNodeId(ids, nearest);
if (id !== nearestId) {
const inspectData = inspect(nearestId);
if (inspectData) {
inspectData.suspended = active;
port.send("inspect-result", inspectData);
}
}
}
break;
}
vnode = bindings.getVNodeParent(vnode);
}
},
};
}