Update the commit tree to assume operations do not repeat
Dan committed
Apr 5, 2019 at 23:42 UTC
7a03c5a8857840353de367709b728e0d32fff108
1 file changed
+60
-41
src/devtools/views/Profiler/CommitTreeBuilder.js
+60
-41
@@ -184,6 +184,14 @@ function updateTree(
184
185
i = i + 3;
186
187
+ if (nodes.has(id)) {
188
+ throw new Error(
189
+ 'Commit tree already contains fiber ' +
190
+ id +
191
+ '. This is a bug in React DevTools.'
192
+ );
193
+ }
194
+
195
if (type === ElementTypeRoot) {
196
i++; // supportsProfiling flag
197
@@ -191,22 +199,16 @@ function updateTree(
199
debug('Add', `new root fiber ${id}`);
200
}
201
194
- if (nodes.has(id)) {
195
- // The renderer's tree walking approach sometimes mounts the same Fiber twice with Suspense and Lazy.
196
- // For now, we avoid adding it to the tree twice by checking if it's already been mounted.
197
- // Maybe in the future we'll revisit this.
198
- } else {
199
- const node: Node = {
200
- children: [],
201
- displayName: null,
202
- id,
203
- key: null,
204
- parentID: 0,
205
- treeBaseDuration: 0, // This will be updated by a subsequent operation
206
- };
207
-
208
- nodes.set(id, node);
209
- }
202
+ const node: Node = {
203
+ children: [],
204
+ displayName: null,
205
+ id,
206
+ key: null,
207
+ parentID: 0,
208
+ treeBaseDuration: 0, // This will be updated by a subsequent operation
209
+ };
210
+
211
+ nodes.set(id, node);
212
} else {
213
parentID = ((operations[i]: any): number);
214
i++;
@@ -231,32 +233,26 @@ function updateTree(
233
: utfDecodeString((operations.slice(i, i + keyLength): any));
234
i += +keyLength;
235
234
- if (nodes.has(id)) {
235
- // The renderer's tree walking approach sometimes mounts the same Fiber twice with Suspense and Lazy.
236
- // For now, we avoid adding it to the tree twice by checking if it's already been mounted.
237
- // Maybe in the future we'll revisit this.
238
- } else {
239
- if (__DEBUG__) {
240
- debug(
241
- 'Add',
242
- `fiber ${id} (${displayName || 'null'}) as child of ${parentID}`
243
- );
244
- }
245
-
246
- parentNode = getClonedNode(parentID);
247
- parentNode.children = parentNode.children.concat(id);
248
-
249
- const node: Node = {
250
- children: [],
251
- displayName,
252
- id,
253
- key,
254
- parentID,
255
- treeBaseDuration: 0, // This will be updated by a subsequent operation
256
- };
257
-
258
- nodes.set(id, node);
236
+ if (__DEBUG__) {
237
+ debug(
238
+ 'Add',
239
+ `fiber ${id} (${displayName || 'null'}) as child of ${parentID}`
240
+ );
241
}
242
+
243
+ parentNode = getClonedNode(parentID);
244
+ parentNode.children = parentNode.children.concat(id);
245
+
246
+ const node: Node = {
247
+ children: [],
248
+ displayName,
249
+ id,
250
+ key,
251
+ parentID,
252
+ treeBaseDuration: 0, // This will be updated by a subsequent operation
253
+ };
254
+
255
+ nodes.set(id, node);
256
}
257
break;
258
case TREE_OPERATION_RECURSIVE_REMOVE_CHILDREN:
@@ -264,9 +260,24 @@ function updateTree(
260
261
i = i + 2;
262
263
+ if (!nodes.has(id)) {
264
+ throw new Error(
265
+ 'Commit tree does not contain fiber ' +
266
+ id +
267
+ '. This is a bug in React DevTools.'
268
+ );
269
+ }
270
+
271
node = getClonedNode(id);
272
273
const recursivelyRemove = childID => {
274
+ if (!nodes.has(id)) {
275
+ throw new Error(
276
+ 'Commit tree does not contain fiber ' +
277
+ id +
278
+ '. This is a bug in React DevTools.'
279
+ );
280
+ }
281
const child = getClonedNode(childID);
282
nodes.delete(childID);
283
child.children.forEach(recursivelyRemove);
@@ -280,6 +291,14 @@ function updateTree(
291
292
i = i + 2;
293
294
+ if (!nodes.has(id)) {
295
+ throw new Error(
296
+ 'Commit tree does not contain fiber ' +
297
+ id +
298
+ '. This is a bug in React DevTools.'
299
+ );
300
+ }
301
+
302
node = getClonedNode(id);
303
parentID = node.parentID;
304