Allow performWork to be called recursively

Currently we assume that performWork is never called recursively.
Ideally that is the case. We shouldn't rely on recursion anywhere
in Fiber.

However, to support the use of syncUpdates as an opt-in to forced-
synchronous updates, we need the ability to call performWork
recursively.

At the beginning of performWork, the state of the scheduler is saved;
before exiting, that state is restored, so that the scheduler can
continue where it left off. I've decided to use local variables to stash
the previous state. We could also store them in a record and push it to
a stack, but this approach has the advantage of being isolated to
performWork.
This commit is contained in:
Andrew Clark committed 2016-12-29 10:23:33 -08:00
1 parent 3926a3566f
commit 8090cf59e3
1 file changed
+37 -18
@@ -672,10 +672,29 @@ module.exports = function<T, P, I, TI, C, CX>(config : HostConfig<T, P, I, TI, C
}
function performWork(priorityLevel : PriorityLevel, deadline : Deadline | null) {
if (isPerformingWork) {
throw new Error('performWork was called recursively.');
}
// If performWork is called recursively, we need to save the previous state
// of the scheduler so it can be restored before the function exits.
// Recursion is only possible when using syncUpdates.
const previousPriorityContext = priorityContext;
const previousPriorityContextBeforeReconciliation = priorityContextBeforeReconciliation;
const previousIsPerformingWork = isPerformingWork;
const previousNextEffect = nextEffect;
const previousCommitPhaseBoundaries = commitPhaseBoundaries;
const previousFirstUncaughtError = firstUncaughtError;
const previousFatalError = fatalError;
const previousIsCommitting = isCommitting;
const previousIsUnmounting = isUnmounting;
priorityContext = NoWork;
priorityContextBeforeReconciliation = NoWork;
isPerformingWork = true;
nextEffect = null;
commitPhaseBoundaries = null;
firstUncaughtError = null;
fatalError = null;
isCommitting = false;
isUnmounting = false;
const isPerformingDeferredWork = Boolean(deadline);
let deadlineHasExpired = false;
@@ -779,22 +798,22 @@ module.exports = function<T, P, I, TI, C, CX>(config : HostConfig<T, P, I, TI, C
}
}
// We're done performing work. Time to clean up.
isPerformingWork = false;
capturedErrors = null;
failedBoundaries = null;
const errorToThrow = fatalError || firstUncaughtError;
// It's now safe to throw errors.
if (fatalError) {
let e = fatalError;
fatalError = null;
firstUncaughtError = null;
throw e;
}
if (firstUncaughtError) {
let e = firstUncaughtError;
firstUncaughtError = null;
throw e;
// We're done performing work. Restore the previous state of the scheduler.
priorityContext = previousPriorityContext;
priorityContextBeforeReconciliation = previousPriorityContextBeforeReconciliation;
isPerformingWork = previousIsPerformingWork;
nextEffect = previousNextEffect;
commitPhaseBoundaries = previousCommitPhaseBoundaries;
firstUncaughtError = previousFirstUncaughtError;
fatalError = previousFatalError;
isCommitting = previousIsCommitting;
isUnmounting = previousIsUnmounting;
// It's safe to throw any unhandled errors.
if (errorToThrow) {
throw errorToThrow;
}
}