Skip to content

Commit

Permalink
Use a magic value for task expiration time
Browse files Browse the repository at this point in the history
There are a few cases related to sync mode where we need to distinguish
between work that is scheduled as task and work that is treated like
task because it expires. For example, batchedUpdates. We don't want to
perform any work until the end of the batch, regardless of how much
time has elapsed.
  • Loading branch information
acdlite committed Aug 11, 2017
1 parent 2a19015 commit dd7229a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
6 changes: 1 addition & 5 deletions src/renderers/dom/fiber/ReactDOMFiberEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,7 @@ var DOMRenderer = ReactFiberReconciler({
}
},

now() {
// TODO: Use performance.now to enable expiration
// return 0;
return now();
},
now,

canHydrateInstance(
instance: Instance | TextInstance,
Expand Down
35 changes: 19 additions & 16 deletions src/renderers/shared/fiber/ReactFiberExpirationTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ const invariant = require('fbjs/lib/invariant');
export type ExpirationTime = number;

const Done = 0;
exports.Done = Done;
const Sync = 1;
const Task = 2;
const Never = Infinity;

const MAGIC_NUMBER_OFFSET = 2;
const MAGIC_NUMBER_OFFSET = 10;

const Never = Infinity;
exports.Done = Done;
exports.Never = Infinity;

// 1 unit of expiration time represents 10ms.
Expand Down Expand Up @@ -63,11 +65,9 @@ function priorityToExpirationTime(
case NoWork:
return Done;
case SynchronousPriority:
// Return a number lower than the current time, but higher than Done.
return MAGIC_NUMBER_OFFSET - 1;
return Sync;
case TaskPriority:
// Return the current time, so that this work completes in this batch.
return currentTime;
return Task;
case HighPriority:
// Should complete within ~100ms. 120ms max.
return msToExpirationTime(ceiling(100, 20));
Expand Down Expand Up @@ -95,16 +95,19 @@ function expirationTimeToPriorityLevel(
expirationTime: ExpirationTime,
): PriorityLevel {
// First check for magic values
if (expirationTime === Done) {
return NoWork;
}
if (expirationTime === Never) {
return OffscreenPriority;
}
if (expirationTime < currentTime) {
return SynchronousPriority;
switch (expirationTime) {
case Done:
return NoWork;
case Sync:
return SynchronousPriority;
case Task:
return TaskPriority;
case Never:
return OffscreenPriority;
default:
break;
}
if (expirationTime === currentTime) {
if (expirationTime <= currentTime) {
return TaskPriority;
}
// Keep this value in sync with priorityToExpirationTime.
Expand Down
64 changes: 35 additions & 29 deletions src/renderers/shared/fiber/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(

// Read the current time from the host environment.
const currentTime = recalculateCurrentTime();
const minExpirationTime = priorityToExpirationTime(
const minExpirationTime = getExpirationTimeForPriority(
currentTime,
minPriorityLevel,
);
Expand Down Expand Up @@ -1447,30 +1447,36 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
const root: FiberRoot = (node.stateNode: any);
scheduleRoot(root, expirationTime);
if (!isPerformingWork) {
if (expirationTime < mostRecentCurrentTime) {
// This update is synchronous. Perform it now.
if (isUnbatchingUpdates) {
// We're inside unbatchedUpdates, which is inside either
// batchedUpdates or a lifecycle. We should only flush
// synchronous work, not task work.
performWork(SynchronousPriority, null);
} else {
// Flush both synchronous and task work.
performWork(TaskPriority, null);
}
} else if (expirationTime === mostRecentCurrentTime) {
invariant(
isBatchingUpdates,
'Task updates can only be scheduled as a nested update or ' +
'inside batchedUpdates. This error is likely caused by a ' +
'bug in React. Please file an issue.',
);
} else {
// This update is async. Schedule a callback.
if (!isCallbackScheduled) {
scheduleDeferredCallback(performDeferredWork);
isCallbackScheduled = true;
}
const priorityLevel = expirationTimeToPriorityLevel(
mostRecentCurrentTime,
expirationTime,
);
switch (priorityLevel) {
case SynchronousPriority:
if (isUnbatchingUpdates) {
// We're inside unbatchedUpdates, which is inside either
// batchedUpdates or a lifecycle. We should only flush
// synchronous work, not task work.
performWork(SynchronousPriority, null);
} else {
// Flush both synchronous and task work.
performWork(TaskPriority, null);
}
break;
case TaskPriority:
invariant(
isBatchingUpdates,
'Task updates can only be scheduled as a nested update or ' +
'inside batchedUpdates. This error is likely caused by a ' +
'bug in React. Please file an issue.',
);
break;
default:
// This update is async. Schedule a callback.
if (!isCallbackScheduled) {
scheduleDeferredCallback(performDeferredWork);
isCallbackScheduled = true;
}
}
}
} else {
Expand Down Expand Up @@ -1533,11 +1539,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
}

function scheduleErrorRecovery(fiber: Fiber) {
scheduleUpdateImpl(
fiber,
priorityToExpirationTime(mostRecentCurrentTime, TaskPriority),
true,
const taskTime = getExpirationTimeForPriority(
mostRecentCurrentTime,
TaskPriority,
);
scheduleUpdateImpl(fiber, taskTime, true);
}

function recalculateCurrentTime(): ExpirationTime {
Expand Down

0 comments on commit dd7229a

Please sign in to comment.