[Fresh] Rename findHostNodesForHotUpdate to findHostInstancesForHotUpdate (#15904)
Dan Abramov committed
Jun 17, 2019 at 15:06 UTC
a3c5b1fb8bca787eaeef2f39a72920ea03da179c
3 files changed
+53
-39
packages/react-reconciler/src/ReactFiberHotReloading.js
+37
-25
@@ -292,26 +292,30 @@ function scheduleFibersWithFamiliesRecursively(
292
}
293
}
294
295
-export function findHostNodesForHotUpdate(
295
+export function findHostInstancesForHotUpdate(
296
root: FiberRoot,
297
families: Array<Family>,
298
): Set<Instance> {
299
if (__DEV__) {
300
- const hostNodes = new Set();
300
+ const hostInstances = new Set();
301
const types = new Set(families.map(family => family.current));
302
- findHostNodesForMatchingFibersRecursively(root.current, types, hostNodes);
303
- return hostNodes;
302
+ findHostInstancesForMatchingFibersRecursively(
303
+ root.current,
304
+ types,
305
+ hostInstances,
306
+ );
307
+ return hostInstances;
308
} else {
309
throw new Error(
306
- 'Did not expect findHostNodesForHotUpdate to be called in production.',
310
+ 'Did not expect findHostInstancesForHotUpdate to be called in production.',
311
);
312
}
313
}
314
311
-function findHostNodesForMatchingFibersRecursively(
315
+function findHostInstancesForMatchingFibersRecursively(
316
fiber: Fiber,
317
types: Set<any>,
314
- hostNodes: Set<Instance>,
318
+ hostInstances: Set<Instance>,
319
) {
320
if (__DEV__) {
321
const {child, sibling, tag, type} = fiber;
@@ -341,30 +345,38 @@ function findHostNodesForMatchingFibersRecursively(
345
// We have a match. This only drills down to the closest host components.
346
// There's no need to search deeper because for the purpose of giving
347
// visual feedback, "flashing" outermost parent rectangles is sufficient.
344
- findHostNodesForFiberShallowly(fiber, hostNodes);
348
+ findHostInstancesForFiberShallowly(fiber, hostInstances);
349
} else {
350
// If there's no match, maybe there will be one further down in the child tree.
351
if (child !== null) {
348
- findHostNodesForMatchingFibersRecursively(child, types, hostNodes);
352
+ findHostInstancesForMatchingFibersRecursively(
353
+ child,
354
+ types,
355
+ hostInstances,
356
+ );
357
}
358
}
359
360
if (sibling !== null) {
353
- findHostNodesForMatchingFibersRecursively(sibling, types, hostNodes);
361
+ findHostInstancesForMatchingFibersRecursively(
362
+ sibling,
363
+ types,
364
+ hostInstances,
365
+ );
366
}
367
}
368
}
369
358
-function findHostNodesForFiberShallowly(
370
+function findHostInstancesForFiberShallowly(
371
fiber: Fiber,
360
- hostNodes: Set<Instance>,
372
+ hostInstances: Set<Instance>,
373
): void {
374
if (__DEV__) {
363
- const foundHostNodes = findChildHostNodesForFiberShallowly(
375
+ const foundHostInstances = findChildHostInstancesForFiberShallowly(
376
fiber,
365
- hostNodes,
377
+ hostInstances,
378
);
367
- if (foundHostNodes) {
379
+ if (foundHostInstances) {
380
return;
381
}
382
// If we didn't find any host children, fallback to closest host parent.
@@ -372,13 +384,13 @@ function findHostNodesForFiberShallowly(
384
while (true) {
385
switch (node.tag) {
386
case HostComponent:
375
- hostNodes.add(node.stateNode);
387
+ hostInstances.add(node.stateNode);
388
return;
389
case HostPortal:
378
- hostNodes.add(node.stateNode.containerInfo);
390
+ hostInstances.add(node.stateNode.containerInfo);
391
return;
392
case HostRoot:
381
- hostNodes.add(node.stateNode.containerInfo);
393
+ hostInstances.add(node.stateNode.containerInfo);
394
return;
395
}
396
if (node.return === null) {
@@ -389,18 +401,18 @@ function findHostNodesForFiberShallowly(
401
}
402
}
403
392
-function findChildHostNodesForFiberShallowly(
404
+function findChildHostInstancesForFiberShallowly(
405
fiber: Fiber,
394
- hostNodes: Set<Instance>,
406
+ hostInstances: Set<Instance>,
407
): boolean {
408
if (__DEV__) {
409
let node: Fiber = fiber;
398
- let foundHostNodes = false;
410
+ let foundHostInstances = false;
411
while (true) {
412
if (node.tag === HostComponent) {
413
// We got a match.
402
- foundHostNodes = true;
403
- hostNodes.add(node.stateNode);
414
+ foundHostInstances = true;
415
+ hostInstances.add(node.stateNode);
416
// There may still be more, so keep searching.
417
} else if (node.child !== null) {
418
node.child.return = node;
@@ -408,11 +420,11 @@ function findChildHostNodesForFiberShallowly(
420
continue;
421
}
422
if (node === fiber) {
411
- return foundHostNodes;
423
+ return foundHostInstances;
424
}
425
while (node.sibling === null) {
426
if (node.return === null || node.return === fiber) {
415
- return foundHostNodes;
427
+ return foundHostInstances;
428
}
429
node = node.return;
430
}
packages/react-reconciler/src/ReactFiberReconciler.js
+4
-2
@@ -72,7 +72,7 @@ import {revertPassiveEffectsChange} from 'shared/ReactFeatureFlags';
72
import {requestCurrentSuspenseConfig} from './ReactFiberSuspenseConfig';
73
import {
74
scheduleHotUpdate,
75
- findHostNodesForHotUpdate,
75
+ findHostInstancesForHotUpdate,
76
} from './ReactFiberHotReloading';
77
78
type OpaqueRoot = FiberRoot;
@@ -475,7 +475,9 @@ export function injectIntoDevTools(devToolsConfig: DevToolsConfig): boolean {
475
476
return injectInternals({
477
...devToolsConfig,
478
- findHostNodesForHotUpdate: __DEV__ ? findHostNodesForHotUpdate : null,
478
+ findHostInstancesForHotUpdate: __DEV__
479
+ ? findHostInstancesForHotUpdate
480
+ : null,
481
scheduleHotUpdate: __DEV__ ? scheduleHotUpdate : null,
482
overrideHookState,
483
overrideProps,
packages/react-refresh/src/__tests__/ReactFresh-test.js
+12
-12
@@ -20,7 +20,7 @@ let act;
20
describe('ReactFresh', () => {
21
let container;
22
let lastRoot;
23
- let findHostNodesForHotUpdate;
23
+ let findHostInstancesForHotUpdate;
24
let scheduleHotUpdate;
25
26
beforeEach(() => {
@@ -28,7 +28,7 @@ describe('ReactFresh', () => {
28
supportsFiber: true,
29
inject: injected => {
30
scheduleHotUpdate = injected.scheduleHotUpdate;
31
- findHostNodesForHotUpdate = injected.findHostNodesForHotUpdate;
31
+ findHostInstancesForHotUpdate = injected.findHostInstancesForHotUpdate;
32
},
33
onCommitFiberRoot: (id, root) => {
34
lastRoot = root;
@@ -2937,7 +2937,7 @@ describe('ReactFresh', () => {
2937
}
2938
});
2939
2940
- it('can find host nodes for a family', () => {
2940
+ it('can find host instances for a family', () => {
2941
if (__DEV__) {
2942
render(() => {
2943
function Child({children}) {
@@ -3010,37 +3010,37 @@ describe('ReactFresh', () => {
3010
const childFamily = ReactFreshRuntime.getFamilyByID('Child');
3011
const emptyFamily = ReactFreshRuntime.getFamilyByID('Empty');
3012
3013
- testFindNodesForFamilies(
3013
+ testFindHostInstancesForFamilies(
3014
[parentFamily],
3015
container.querySelectorAll('.Parent'),
3016
);
3017
3018
- testFindNodesForFamilies(
3018
+ testFindHostInstancesForFamilies(
3019
[childFamily],
3020
container.querySelectorAll('.Child'),
3021
);
3022
3023
// When searching for both Parent and Child,
3024
// we'll stop visual highlighting at the Parent.
3025
- testFindNodesForFamilies(
3025
+ testFindHostInstancesForFamilies(
3026
[parentFamily, childFamily],
3027
container.querySelectorAll('.Parent'),
3028
);
3029
3030
// When we can't find host nodes, use the closest parent.
3031
- testFindNodesForFamilies(
3031
+ testFindHostInstancesForFamilies(
3032
[emptyFamily],
3033
container.querySelectorAll('.App'),
3034
);
3035
}
3036
});
3037
3038
- function testFindNodesForFamilies(families, expectedNodes) {
3039
- const foundNodes = Array.from(
3040
- findHostNodesForHotUpdate(lastRoot, families),
3038
+ function testFindHostInstancesForFamilies(families, expectedNodes) {
3039
+ const foundInstances = Array.from(
3040
+ findHostInstancesForHotUpdate(lastRoot, families),
3041
);
3042
- expect(foundNodes.length).toEqual(expectedNodes.length);
3043
- foundNodes.forEach((node, i) => {
3042
+ expect(foundInstances.length).toEqual(expectedNodes.length);
3043
+ foundInstances.forEach((node, i) => {
3044
expect(node).toBe(expectedNodes[i]);
3045
});
3046
}