Skip to content

Commit 0602449

Browse files
author
Brian Vaughn
committed
Fixed yielding/incremental bug
1 parent f0562c7 commit 0602449

File tree

4 files changed

+219
-85
lines changed

4 files changed

+219
-85
lines changed

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import {
6868
NoWork,
6969
computeAsyncExpirationNoBucket,
7070
} from './ReactFiberExpirationTime';
71-
import {onCommitUnmount} from './ReactFiberDevToolsHook';
71+
import {isDevToolsPresent, onCommitUnmount} from './ReactFiberDevToolsHook';
7272
import {startPhaseTimer, stopPhaseTimer} from './ReactDebugFiberPerf';
7373
import {getStackByFiberInDevAndProd} from './ReactCurrentFiber';
7474
import {logCapturedError} from './ReactFiberErrorLogger';
@@ -1341,8 +1341,10 @@ function commitSuspenseComponent(
13411341
retry = Schedule_tracing_wrap(retry);
13421342
}
13431343
if (enableUpdaterTracking) {
1344-
// If we have pending work still, restore the original updaters
1345-
restorePendingUpdaters(finishedRoot, committedExpirationTime);
1344+
if (isDevToolsPresent) {
1345+
// If we have pending work still, restore the original updaters
1346+
restorePendingUpdaters(finishedRoot, committedExpirationTime);
1347+
}
13461348
}
13471349
retryCache.add(thenable);
13481350
thenable.then(retry, retry);

packages/react-reconciler/src/ReactFiberScheduler.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ import {
167167
hasCaughtError,
168168
clearCaughtError,
169169
} from 'shared/ReactErrorUtils';
170-
import {onCommitRoot} from './ReactFiberDevToolsHook';
170+
import {isDevToolsPresent, onCommitRoot} from './ReactFiberDevToolsHook';
171171

172172
const ceil = Math.ceil;
173173

@@ -339,13 +339,15 @@ export function scheduleUpdateOnFiber(
339339
}
340340

341341
if (enableUpdaterTracking) {
342-
const pendingUpdatersMap = root.pendingUpdatersMap;
343-
let updaters = pendingUpdatersMap.get(expirationTime);
344-
if (updaters == null) {
345-
updaters = new Set();
346-
pendingUpdatersMap.set(expirationTime, updaters);
342+
if (isDevToolsPresent) {
343+
const pendingUpdatersMap = root.pendingUpdatersMap;
344+
let updaters = pendingUpdatersMap.get(expirationTime);
345+
if (updaters == null) {
346+
updaters = new Set();
347+
pendingUpdatersMap.set(expirationTime, updaters);
348+
}
349+
updaters.add(fiber);
347350
}
348-
updaters.add(fiber);
349351
}
350352

351353
root.pingTime = NoWork;
@@ -813,6 +815,17 @@ function renderRoot(
813815
Interaction,
814816
>);
815817
}
818+
819+
if (enableUpdaterTracking) {
820+
if (isDevToolsPresent) {
821+
// If we didn't finish the current work, restore pending updaters for the next pass.
822+
if (root.memoizedUpdaters.size > 0) {
823+
restorePendingUpdaters(root, expirationTime);
824+
root.memoizedUpdaters.clear();
825+
}
826+
}
827+
}
828+
816829
return renderRoot.bind(null, root, currentTime);
817830
}
818831
}
@@ -878,6 +891,17 @@ function renderRoot(
878891
if (expirationTime !== Sync) {
879892
startRequestCallbackTimer();
880893
}
894+
895+
if (enableUpdaterTracking) {
896+
if (isDevToolsPresent) {
897+
// If we didn't finish the current work, restore pending updaters for the next pass.
898+
if (root.memoizedUpdaters.size > 0) {
899+
restorePendingUpdaters(root, expirationTime);
900+
root.memoizedUpdaters.clear();
901+
}
902+
}
903+
}
904+
881905
return renderRoot.bind(null, root, expirationTime);
882906
}
883907
}
@@ -1305,8 +1329,10 @@ function commitRootImpl(root) {
13051329
root.lastPendingTime = firstPendingTimeBeforeCommit;
13061330

13071331
if (enableUpdaterTracking) {
1308-
if (firstPendingTimeBeforeCommit !== NoWork) {
1309-
restorePendingUpdaters(root, root.lastPendingTime);
1332+
if (isDevToolsPresent) {
1333+
if (firstPendingTimeBeforeCommit !== NoWork) {
1334+
restorePendingUpdaters(root, root.lastPendingTime);
1335+
}
13101336
}
13111337
}
13121338
}
@@ -1515,6 +1541,12 @@ function commitRootImpl(root) {
15151541

15161542
onCommitRoot(finishedWork.stateNode);
15171543

1544+
if (enableUpdaterTracking) {
1545+
if (isDevToolsPresent) {
1546+
root.memoizedUpdaters.clear();
1547+
}
1548+
}
1549+
15181550
if (remainingExpirationTime === Sync) {
15191551
// Count the number of times the root synchronously re-renders without
15201552
// finishing. If there are too many, it indicates an infinite update loop.
@@ -2191,7 +2223,7 @@ export function restorePendingUpdaters(
21912223
root: FiberRoot,
21922224
expirationTime: ExpirationTime,
21932225
): void {
2194-
if (!enableUpdaterTracking) {
2226+
if (!enableUpdaterTracking || !isDevToolsPresent) {
21952227
return;
21962228
}
21972229
const pendingUpdatersMap = root.pendingUpdatersMap;
@@ -2323,20 +2355,21 @@ function startWorkOnPendingInteraction(root, expirationTime) {
23232355
// This is called when new work is started on a root.
23242356

23252357
if (enableUpdaterTracking) {
2326-
const memoizedUpdaters: Set<Fiber> = new Set();
2327-
const pendingUpdatersMap = root.pendingUpdatersMap;
2328-
pendingUpdatersMap.forEach((updaters, scheduledExpirationTime) => {
2329-
if (scheduledExpirationTime >= expirationTime) {
2330-
pendingUpdatersMap.delete(scheduledExpirationTime);
2331-
updaters.forEach(fiber => memoizedUpdaters.add(fiber));
2332-
}
2333-
});
2334-
2335-
// Store the current set of interactions on the FiberRoot for a few reasons:
2336-
// We can re-use it in hot functions like renderRoot() without having to
2337-
// recalculate it. This also provides DevTools with a way to access it when
2338-
// the onCommitRoot() hook is called.
2339-
root.memoizedUpdaters = memoizedUpdaters;
2358+
if (isDevToolsPresent) {
2359+
// Store the current set of interactions on the FiberRoot for a few reasons:
2360+
// We can re-use it in hot functions like renderRoot() without having to
2361+
// recalculate it. This also provides DevTools with a way to access it when
2362+
// the onCommitRoot() hook is called.
2363+
const pendingUpdatersMap = root.pendingUpdatersMap;
2364+
pendingUpdatersMap.forEach((updaters, scheduledExpirationTime) => {
2365+
if (scheduledExpirationTime >= expirationTime) {
2366+
pendingUpdatersMap.delete(scheduledExpirationTime);
2367+
updaters.forEach(fiber => {
2368+
root.memoizedUpdaters.add(fiber);
2369+
});
2370+
}
2371+
});
2372+
}
23402373
}
23412374

23422375
if (enableSchedulerTracing) {

packages/react-reconciler/src/ReactFiberUnwindWork.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
checkForWrongSuspensePriorityInDEV,
8080
restorePendingUpdaters,
8181
} from './ReactFiberScheduler';
82+
import {isDevToolsPresent} from './ReactFiberDevToolsHook';
8283

8384
import invariant from 'shared/invariant';
8485

@@ -190,8 +191,10 @@ function attachPingListener(
190191
ping = Schedule_tracing_wrap(ping);
191192
}
192193
if (enableUpdaterTracking) {
193-
// If we have pending work still, restore the original updaters
194-
restorePendingUpdaters(root, renderExpirationTime);
194+
if (isDevToolsPresent) {
195+
// If we have pending work still, restore the original updaters
196+
restorePendingUpdaters(root, renderExpirationTime);
197+
}
195198
}
196199
thenable.then(ping, ping);
197200
}
@@ -210,8 +213,10 @@ function throwException(
210213
sourceFiber.firstEffect = sourceFiber.lastEffect = null;
211214

212215
if (enableUpdaterTracking) {
213-
// If we have pending work still, restore the original updaters
214-
restorePendingUpdaters(root, renderExpirationTime);
216+
if (isDevToolsPresent) {
217+
// If we have pending work still, restore the original updaters
218+
restorePendingUpdaters(root, renderExpirationTime);
219+
}
215220
}
216221

217222
if (

0 commit comments

Comments
 (0)