-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
renderer.js
253 lines (218 loc) · 6.73 KB
/
renderer.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
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
/* istanbul ignore file */
import { getData, getChildren, getInstance, hasDataChanged, isRoot } from './custom';
/**
* Custom renderer tailored for Preact. It converts updated vnode trees
* to events the devtools can understand.
* @class Renderer
*/
export class Renderer {
constructor(hook, rid) {
/** @type {string} */
this.rid = rid;
this.hook = hook;
/** @type {Array<import('../internal').DevtoolsEvent>} */
this.pending = [];
/**
* Store the instance of a vnode. This will be later used to decide if a
* vnode needs to be mounted or updated. For components the instance refers
* to the actuall class instance whereas for dom nodes it refers to the
* underlying dom element.
* @type {WeakMap<import('../internal').Component | import('../internal').PreactElement | HTMLElement | Text, import('../internal').VNode>}
*/
this.inst2vnode = new WeakMap();
this.connected = false;
}
/**
* Mark the connection status as ready so that we can proceed
* to actually flush events.
*/
markConnected() {
this.connected = true;
this.flushPendingEvents();
}
/**
* Flush all queued events
*/
flushPendingEvents() {
if (!this.connected) return;
let events = this.pending;
this.pending = [];
for (let i = 0; i < events.length; i++) {
let event = events[i];
this.hook.emit(event.type, event);
}
}
/**
* Recursively mount a vnode tree. Note that the devtools expects the tree to
* be mounted from the bottom up, otherwise the order will be messed up.
* Therefore we mount children prior to mounting the vnode itself.
* @param {import('../internal').VNode} vnode
*/
mount(vnode) {
this.inst2vnode.set(getInstance(vnode), vnode);
let data = getData(vnode);
/** @type {Array<import('../internal').DevtoolsEvent>} */
let work = [{
internalInstance: vnode,
data,
renderer: this.rid,
type: 'mount'
}];
// Children must be mounted first
if (Array.isArray(data.children)) {
let stack = data.children.slice();
let item;
while ((item = stack.pop())!=null) {
let children = getChildren(item);
stack.push(...children);
this.inst2vnode.set(getInstance(item), item);
let data = getData(item);
work.push({
internalInstance: item,
data,
renderer: this.rid,
type: 'mount'
});
}
}
for (let i = work.length; --i>=0;) {
this.pending.push(work[i]);
}
// Special event if we have a root
if (isRoot(vnode)) {
this.pending.push({
internalInstance: vnode,
data,
renderer: this.rid,
type: 'root'
});
}
}
/**
* Update a vnode tree
* @param {import('../internal').VNode} vnode
*/
update(vnode) {
let data = getData(vnode);
// Children must be updated first
if (Array.isArray(data.children)) {
for (let i = 0; i < data.children.length; i++) {
let child = data.children[i];
let inst = getInstance(child);
let prevChild = this.inst2vnode.get(inst);
if (prevChild==null) this.mount(child);
else this.update(child);
// Mutate child to keep referential equality intact
data.children[i] = this.inst2vnode.get(inst);
}
}
let prev = this.inst2vnode.get(data.publicInstance);
// The `updateProfileTimes` event is a faster version of `updated` and
// is processed much quicker inside the devtools extension.
if (!hasDataChanged(prev, vnode)) {
// Always assume profiling data has changed. When we skip an event here
// the devtools element picker will somehow break.
this.pending.push({
// This property is only used as an id inside the devtools. The
// relevant data will be read from `.data` instead which is a
// normalized structure that every react release adheres to. This
// makes backwards-compatibility easier instead of relying on internal
// vnode/fiber shape.
internalInstance: prev,
data,
renderer: this.rid,
type: 'updateProfileTimes'
});
return;
}
this.pending.push({
// This property is only used as an id inside the devtools. The
// relevant data will be read from `.data` instead which is a
// normalized structure that every react release adheres to. This
// makes backwards-compatibility easier instead of relying on internal
// vnode/fiber shape.
internalInstance: prev,
data,
renderer: this.rid,
type: 'update'
});
}
/**
* Pass a rendered tree to the devtools. At this point elements have already
* unmounted, so we don't need to check for removals and only update vs mount
* instead.
* @param {import('../internal').VNode} vnode
*/
handleCommitFiberRoot(vnode) {
let inst = getInstance(vnode);
if (this.inst2vnode.has(inst)) this.update(vnode);
else this.mount(vnode);
// The devtools checks via the existence of this property if the devtools
// profiler should be enabled or not. If it is missing from the first root
// node the "Profiler" tab won't show up.
/** @type {import('../internal').VNode} */
let root = null;
if (isRoot(vnode)) {
/** @type {*} */
(vnode).treeBaseDuration = 0;
root = vnode;
}
else {
// "rootCommitted" always needs the actual root node for the profiler
// to be able to collect timings. The `_parent` property will
// point to a vnode for a root node.
root = vnode;
while (root._parent!=null) {
root = root._parent;
}
}
this.pending.push({
internalInstance: root,
renderer: this.rid,
data: getData(root),
type: 'rootCommitted'
});
this.flushPendingEvents();
return vnode;
}
/**
* Unmount a vnode recursively. Contrary to mounting or updating unmounting needs
* to push the events in parent-first order. Because `options.unmount` is
* already fired in parent-first order we don't need to traverse anything here.
* @param {import('../internal').VNode} vnode
*/
handleCommitFiberUnmount(vnode) {
let inst = getInstance(vnode);
this.inst2vnode.delete(inst);
// Special case when unmounting a root (most prominently caused by webpack's
// `hot-module-reloading`). If this happens we need to unmount the virtual
// `Fragment` we're wrapping around each root just for the devtools.
this.pending.push({
internalInstance: vnode,
renderer: this.rid,
type: 'unmount'
});
}
/**
* Get the dom element by a vnode
* @param {import('../internal').VNode} vnode
* @returns {import('../internal').PreactElement | Text}
*/
getNativeFromReactElement(vnode) {
return vnode._dom;
}
/**
* Get a vnode by a dom element
* @param {import('../internal').PreactElement | Text} dom
* @returns {import('../internal').VNode | null}
*/
getReactElementFromNative(dom) {
return this.inst2vnode.get(dom) || null;
}
// Unused, but devtools expects it to be there
/* istanbul ignore next */
walkTree() {}
// Unused, but devtools expects it to be there
/* istanbul ignore next */
cleanup() {}
}