Skip to content

Commit a960d18

Browse files
nateq314gaearon
authored andcommitted
eliminate unnecessary do-while loop in renderRoot() (#13087)
1 parent 5b3d17a commit a960d18

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

packages/react-reconciler/src/ReactFiberScheduler.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,54 +1011,52 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
10111011

10121012
startWorkLoopTimer(nextUnitOfWork);
10131013

1014-
do {
1015-
try {
1016-
workLoop(isYieldy);
1017-
} catch (thrownValue) {
1018-
if (enableProfilerTimer) {
1019-
// Stop "base" render timer in the event of an error.
1020-
stopBaseRenderTimerIfRunning();
1014+
try {
1015+
workLoop(isYieldy);
1016+
} catch (thrownValue) {
1017+
if (enableProfilerTimer) {
1018+
// Stop "base" render timer in the event of an error.
1019+
stopBaseRenderTimerIfRunning();
1020+
}
1021+
1022+
if (nextUnitOfWork === null) {
1023+
// This is a fatal error.
1024+
didFatal = true;
1025+
onUncaughtError(thrownValue);
1026+
} else {
1027+
if (__DEV__) {
1028+
// Reset global debug state
1029+
// We assume this is defined in DEV
1030+
(resetCurrentlyProcessingQueue: any)();
10211031
}
10221032

1023-
if (nextUnitOfWork === null) {
1024-
// This is a fatal error.
1033+
const failedUnitOfWork: Fiber = nextUnitOfWork;
1034+
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
1035+
replayUnitOfWork(failedUnitOfWork, thrownValue, isYieldy);
1036+
}
1037+
1038+
// TODO: we already know this isn't true in some cases.
1039+
// At least this shows a nicer error message until we figure out the cause.
1040+
// https://github.com/facebook/react/issues/12449#issuecomment-386727431
1041+
invariant(
1042+
nextUnitOfWork !== null,
1043+
'Failed to replay rendering after an error. This ' +
1044+
'is likely caused by a bug in React. Please file an issue ' +
1045+
'with a reproducing case to help us find it.',
1046+
);
1047+
1048+
const sourceFiber: Fiber = nextUnitOfWork;
1049+
let returnFiber = sourceFiber.return;
1050+
if (returnFiber === null) {
1051+
// This is the root. The root could capture its own errors. However,
1052+
// we don't know if it errors before or after we pushed the host
1053+
// context. This information is needed to avoid a stack mismatch.
1054+
// Because we're not sure, treat this as a fatal error. We could track
1055+
// which phase it fails in, but doesn't seem worth it. At least
1056+
// for now.
10251057
didFatal = true;
10261058
onUncaughtError(thrownValue);
10271059
} else {
1028-
if (__DEV__) {
1029-
// Reset global debug state
1030-
// We assume this is defined in DEV
1031-
(resetCurrentlyProcessingQueue: any)();
1032-
}
1033-
1034-
const failedUnitOfWork: Fiber = nextUnitOfWork;
1035-
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
1036-
replayUnitOfWork(failedUnitOfWork, thrownValue, isYieldy);
1037-
}
1038-
1039-
// TODO: we already know this isn't true in some cases.
1040-
// At least this shows a nicer error message until we figure out the cause.
1041-
// https://github.com/facebook/react/issues/12449#issuecomment-386727431
1042-
invariant(
1043-
nextUnitOfWork !== null,
1044-
'Failed to replay rendering after an error. This ' +
1045-
'is likely caused by a bug in React. Please file an issue ' +
1046-
'with a reproducing case to help us find it.',
1047-
);
1048-
1049-
const sourceFiber: Fiber = nextUnitOfWork;
1050-
let returnFiber = sourceFiber.return;
1051-
if (returnFiber === null) {
1052-
// This is the root. The root could capture its own errors. However,
1053-
// we don't know if it errors before or after we pushed the host
1054-
// context. This information is needed to avoid a stack mismatch.
1055-
// Because we're not sure, treat this as a fatal error. We could track
1056-
// which phase it fails in, but doesn't seem worth it. At least
1057-
// for now.
1058-
didFatal = true;
1059-
onUncaughtError(thrownValue);
1060-
break;
1061-
}
10621060
throwException(
10631061
root,
10641062
returnFiber,
@@ -1069,8 +1067,7 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
10691067
nextUnitOfWork = completeUnitOfWork(sourceFiber);
10701068
}
10711069
}
1072-
break;
1073-
} while (true);
1070+
}
10741071

10751072
// We're done performing work. Time to clean up.
10761073
isWorking = false;

0 commit comments

Comments
 (0)