mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
827e912776
## Summary
these were removed in https://github.com/facebook/react/pull/26617. adds
them back so we can conduct another experiment.
## How did you test this change?
`yarn test-www`
DiffTrain build for [c81f4e02ba](https://github.com/facebook/react/commit/c81f4e02bab9d03e1583047d041fdb5e4eb19415)
886 lines
28 KiB
JavaScript
886 lines
28 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @noflow
|
|
* @nolint
|
|
* @preventMunge
|
|
* @preserve-invariant-messages
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
if (__DEV__) {
|
|
(function () {
|
|
"use strict";
|
|
|
|
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
|
|
if (
|
|
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== "undefined" &&
|
|
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
|
|
"function"
|
|
) {
|
|
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
|
|
}
|
|
// $FlowFixMe[cannot-resolve-module]
|
|
var dynamicFeatureFlags = require("SchedulerFeatureFlags");
|
|
|
|
var enableProfilingFeatureFlag = dynamicFeatureFlags.enableProfiling;
|
|
var userBlockingPriorityTimeout =
|
|
dynamicFeatureFlags.userBlockingPriorityTimeout,
|
|
normalPriorityTimeout = dynamicFeatureFlags.normalPriorityTimeout,
|
|
lowPriorityTimeout = dynamicFeatureFlags.lowPriorityTimeout,
|
|
enableIsInputPending = dynamicFeatureFlags.enableIsInputPending,
|
|
enableIsInputPendingContinuous =
|
|
dynamicFeatureFlags.enableIsInputPendingContinuous,
|
|
frameYieldMs = dynamicFeatureFlags.frameYieldMs,
|
|
continuousYieldMs = dynamicFeatureFlags.continuousYieldMs,
|
|
maxYieldMs = dynamicFeatureFlags.maxYieldMs;
|
|
var enableProfiling = enableProfilingFeatureFlag;
|
|
|
|
function push(heap, node) {
|
|
var index = heap.length;
|
|
heap.push(node);
|
|
siftUp(heap, node, index);
|
|
}
|
|
function peek(heap) {
|
|
return heap.length === 0 ? null : heap[0];
|
|
}
|
|
function pop(heap) {
|
|
if (heap.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
var first = heap[0];
|
|
var last = heap.pop();
|
|
|
|
if (last !== first) {
|
|
heap[0] = last;
|
|
siftDown(heap, last, 0);
|
|
}
|
|
|
|
return first;
|
|
}
|
|
|
|
function siftUp(heap, node, i) {
|
|
var index = i;
|
|
|
|
while (index > 0) {
|
|
var parentIndex = (index - 1) >>> 1;
|
|
var parent = heap[parentIndex];
|
|
|
|
if (compare(parent, node) > 0) {
|
|
// The parent is larger. Swap positions.
|
|
heap[parentIndex] = node;
|
|
heap[index] = parent;
|
|
index = parentIndex;
|
|
} else {
|
|
// The parent is smaller. Exit.
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
function siftDown(heap, node, i) {
|
|
var index = i;
|
|
var length = heap.length;
|
|
var halfLength = length >>> 1;
|
|
|
|
while (index < halfLength) {
|
|
var leftIndex = (index + 1) * 2 - 1;
|
|
var left = heap[leftIndex];
|
|
var rightIndex = leftIndex + 1;
|
|
var right = heap[rightIndex]; // If the left or right node is smaller, swap with the smaller of those.
|
|
|
|
if (compare(left, node) < 0) {
|
|
if (rightIndex < length && compare(right, left) < 0) {
|
|
heap[index] = right;
|
|
heap[rightIndex] = node;
|
|
index = rightIndex;
|
|
} else {
|
|
heap[index] = left;
|
|
heap[leftIndex] = node;
|
|
index = leftIndex;
|
|
}
|
|
} else if (rightIndex < length && compare(right, node) < 0) {
|
|
heap[index] = right;
|
|
heap[rightIndex] = node;
|
|
index = rightIndex;
|
|
} else {
|
|
// Neither child is smaller. Exit.
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
function compare(a, b) {
|
|
// Compare sort index first, then task id.
|
|
var diff = a.sortIndex - b.sortIndex;
|
|
return diff !== 0 ? diff : a.id - b.id;
|
|
}
|
|
|
|
// TODO: Use symbols?
|
|
var ImmediatePriority = 1;
|
|
var UserBlockingPriority = 2;
|
|
var NormalPriority = 3;
|
|
var LowPriority = 4;
|
|
var IdlePriority = 5;
|
|
|
|
var runIdCounter = 0;
|
|
var mainThreadIdCounter = 0; // Bytes per element is 4
|
|
|
|
var INITIAL_EVENT_LOG_SIZE = 131072;
|
|
var MAX_EVENT_LOG_SIZE = 524288; // Equivalent to 2 megabytes
|
|
|
|
var eventLogSize = 0;
|
|
var eventLogBuffer = null;
|
|
var eventLog = null;
|
|
var eventLogIndex = 0;
|
|
var TaskStartEvent = 1;
|
|
var TaskCompleteEvent = 2;
|
|
var TaskErrorEvent = 3;
|
|
var TaskCancelEvent = 4;
|
|
var TaskRunEvent = 5;
|
|
var TaskYieldEvent = 6;
|
|
var SchedulerSuspendEvent = 7;
|
|
var SchedulerResumeEvent = 8;
|
|
|
|
function logEvent(entries) {
|
|
if (eventLog !== null) {
|
|
var offset = eventLogIndex;
|
|
eventLogIndex += entries.length;
|
|
|
|
if (eventLogIndex + 1 > eventLogSize) {
|
|
eventLogSize *= 2;
|
|
|
|
if (eventLogSize > MAX_EVENT_LOG_SIZE) {
|
|
// Using console['error'] to evade Babel and ESLint
|
|
console["error"](
|
|
"Scheduler Profiling: Event log exceeded maximum size. Don't " +
|
|
"forget to call `stopLoggingProfilingEvents()`."
|
|
);
|
|
stopLoggingProfilingEvents();
|
|
return;
|
|
}
|
|
|
|
var newEventLog = new Int32Array(eventLogSize * 4); // $FlowFixMe[incompatible-call] found when upgrading Flow
|
|
|
|
newEventLog.set(eventLog);
|
|
eventLogBuffer = newEventLog.buffer;
|
|
eventLog = newEventLog;
|
|
}
|
|
|
|
eventLog.set(entries, offset);
|
|
}
|
|
}
|
|
|
|
function startLoggingProfilingEvents() {
|
|
eventLogSize = INITIAL_EVENT_LOG_SIZE;
|
|
eventLogBuffer = new ArrayBuffer(eventLogSize * 4);
|
|
eventLog = new Int32Array(eventLogBuffer);
|
|
eventLogIndex = 0;
|
|
}
|
|
function stopLoggingProfilingEvents() {
|
|
var buffer = eventLogBuffer;
|
|
eventLogSize = 0;
|
|
eventLogBuffer = null;
|
|
eventLog = null;
|
|
eventLogIndex = 0;
|
|
return buffer;
|
|
}
|
|
function markTaskStart(task, ms) {
|
|
if (enableProfiling) {
|
|
if (eventLog !== null) {
|
|
// performance.now returns a float, representing milliseconds. When the
|
|
// event is logged, it's coerced to an int. Convert to microseconds to
|
|
// maintain extra degrees of precision.
|
|
logEvent([TaskStartEvent, ms * 1000, task.id, task.priorityLevel]);
|
|
}
|
|
}
|
|
}
|
|
function markTaskCompleted(task, ms) {
|
|
if (enableProfiling) {
|
|
if (eventLog !== null) {
|
|
logEvent([TaskCompleteEvent, ms * 1000, task.id]);
|
|
}
|
|
}
|
|
}
|
|
function markTaskCanceled(task, ms) {
|
|
if (enableProfiling) {
|
|
if (eventLog !== null) {
|
|
logEvent([TaskCancelEvent, ms * 1000, task.id]);
|
|
}
|
|
}
|
|
}
|
|
function markTaskErrored(task, ms) {
|
|
if (enableProfiling) {
|
|
if (eventLog !== null) {
|
|
logEvent([TaskErrorEvent, ms * 1000, task.id]);
|
|
}
|
|
}
|
|
}
|
|
function markTaskRun(task, ms) {
|
|
if (enableProfiling) {
|
|
runIdCounter++;
|
|
|
|
if (eventLog !== null) {
|
|
logEvent([TaskRunEvent, ms * 1000, task.id, runIdCounter]);
|
|
}
|
|
}
|
|
}
|
|
function markTaskYield(task, ms) {
|
|
if (enableProfiling) {
|
|
if (eventLog !== null) {
|
|
logEvent([TaskYieldEvent, ms * 1000, task.id, runIdCounter]);
|
|
}
|
|
}
|
|
}
|
|
function markSchedulerSuspended(ms) {
|
|
if (enableProfiling) {
|
|
mainThreadIdCounter++;
|
|
|
|
if (eventLog !== null) {
|
|
logEvent([SchedulerSuspendEvent, ms * 1000, mainThreadIdCounter]);
|
|
}
|
|
}
|
|
}
|
|
function markSchedulerUnsuspended(ms) {
|
|
if (enableProfiling) {
|
|
if (eventLog !== null) {
|
|
logEvent([SchedulerResumeEvent, ms * 1000, mainThreadIdCounter]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* eslint-disable no-var */
|
|
exports.unstable_now = void 0;
|
|
var hasPerformanceNow = // $FlowFixMe[method-unbinding]
|
|
typeof performance === "object" && typeof performance.now === "function";
|
|
|
|
if (hasPerformanceNow) {
|
|
var localPerformance = performance;
|
|
|
|
exports.unstable_now = function () {
|
|
return localPerformance.now();
|
|
};
|
|
} else {
|
|
var localDate = Date;
|
|
var initialTime = localDate.now();
|
|
|
|
exports.unstable_now = function () {
|
|
return localDate.now() - initialTime;
|
|
};
|
|
} // Max 31 bit integer. The max integer size in V8 for 32-bit systems.
|
|
// Math.pow(2, 30) - 1
|
|
// 0b111111111111111111111111111111
|
|
|
|
var maxSigned31BitInt = 1073741823; // Tasks are stored on a min heap
|
|
|
|
var taskQueue = [];
|
|
var timerQueue = []; // Incrementing id counter. Used to maintain insertion order.
|
|
|
|
var taskIdCounter = 1; // Pausing the scheduler is useful for debugging.
|
|
|
|
var isSchedulerPaused = false;
|
|
var currentTask = null;
|
|
var currentPriorityLevel = NormalPriority; // This is set while performing work, to prevent re-entrance.
|
|
|
|
var isPerformingWork = false;
|
|
var isHostCallbackScheduled = false;
|
|
var isHostTimeoutScheduled = false; // Capture local references to native APIs, in case a polyfill overrides them.
|
|
|
|
var localSetTimeout = typeof setTimeout === "function" ? setTimeout : null;
|
|
var localClearTimeout =
|
|
typeof clearTimeout === "function" ? clearTimeout : null;
|
|
var localSetImmediate =
|
|
typeof setImmediate !== "undefined" ? setImmediate : null; // IE and Node.js + jsdom
|
|
|
|
var isInputPending =
|
|
typeof navigator !== "undefined" && // $FlowFixMe[prop-missing]
|
|
navigator.scheduling !== undefined && // $FlowFixMe[incompatible-type]
|
|
navigator.scheduling.isInputPending !== undefined
|
|
? navigator.scheduling.isInputPending.bind(navigator.scheduling)
|
|
: null;
|
|
var continuousOptions = {
|
|
includeContinuous: enableIsInputPendingContinuous
|
|
};
|
|
|
|
function advanceTimers(currentTime) {
|
|
// Check for tasks that are no longer delayed and add them to the queue.
|
|
var timer = peek(timerQueue);
|
|
|
|
while (timer !== null) {
|
|
if (timer.callback === null) {
|
|
// Timer was cancelled.
|
|
pop(timerQueue);
|
|
} else if (timer.startTime <= currentTime) {
|
|
// Timer fired. Transfer to the task queue.
|
|
pop(timerQueue);
|
|
timer.sortIndex = timer.expirationTime;
|
|
push(taskQueue, timer);
|
|
|
|
if (enableProfiling) {
|
|
markTaskStart(timer, currentTime);
|
|
timer.isQueued = true;
|
|
}
|
|
} else {
|
|
// Remaining timers are pending.
|
|
return;
|
|
}
|
|
|
|
timer = peek(timerQueue);
|
|
}
|
|
}
|
|
|
|
function handleTimeout(currentTime) {
|
|
isHostTimeoutScheduled = false;
|
|
advanceTimers(currentTime);
|
|
|
|
if (!isHostCallbackScheduled) {
|
|
if (peek(taskQueue) !== null) {
|
|
isHostCallbackScheduled = true;
|
|
requestHostCallback();
|
|
} else {
|
|
var firstTimer = peek(timerQueue);
|
|
|
|
if (firstTimer !== null) {
|
|
requestHostTimeout(
|
|
handleTimeout,
|
|
firstTimer.startTime - currentTime
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function flushWork(initialTime) {
|
|
if (enableProfiling) {
|
|
markSchedulerUnsuspended(initialTime);
|
|
} // We'll need a host callback the next time work is scheduled.
|
|
|
|
isHostCallbackScheduled = false;
|
|
|
|
if (isHostTimeoutScheduled) {
|
|
// We scheduled a timeout but it's no longer needed. Cancel it.
|
|
isHostTimeoutScheduled = false;
|
|
cancelHostTimeout();
|
|
}
|
|
|
|
isPerformingWork = true;
|
|
var previousPriorityLevel = currentPriorityLevel;
|
|
|
|
try {
|
|
if (enableProfiling) {
|
|
try {
|
|
return workLoop(initialTime);
|
|
} catch (error) {
|
|
if (currentTask !== null) {
|
|
var currentTime = exports.unstable_now(); // $FlowFixMe[incompatible-call] found when upgrading Flow
|
|
|
|
markTaskErrored(currentTask, currentTime); // $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
currentTask.isQueued = false;
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
} else {
|
|
// No catch in prod code path.
|
|
return workLoop(initialTime);
|
|
}
|
|
} finally {
|
|
currentTask = null;
|
|
currentPriorityLevel = previousPriorityLevel;
|
|
isPerformingWork = false;
|
|
|
|
if (enableProfiling) {
|
|
var _currentTime = exports.unstable_now();
|
|
|
|
markSchedulerSuspended(_currentTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
function workLoop(initialTime) {
|
|
var currentTime = initialTime;
|
|
advanceTimers(currentTime);
|
|
currentTask = peek(taskQueue);
|
|
|
|
while (currentTask !== null && !isSchedulerPaused) {
|
|
if (currentTask.expirationTime > currentTime && shouldYieldToHost()) {
|
|
// This currentTask hasn't expired, and we've reached the deadline.
|
|
break;
|
|
} // $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
var callback = currentTask.callback;
|
|
|
|
if (typeof callback === "function") {
|
|
// $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
currentTask.callback = null; // $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
currentPriorityLevel = currentTask.priorityLevel; // $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
var didUserCallbackTimeout =
|
|
currentTask.expirationTime <= currentTime;
|
|
|
|
if (enableProfiling) {
|
|
// $FlowFixMe[incompatible-call] found when upgrading Flow
|
|
markTaskRun(currentTask, currentTime);
|
|
}
|
|
|
|
var continuationCallback = callback(didUserCallbackTimeout);
|
|
currentTime = exports.unstable_now();
|
|
|
|
if (typeof continuationCallback === "function") {
|
|
// If a continuation is returned, immediately yield to the main thread
|
|
// regardless of how much time is left in the current time slice.
|
|
// $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
currentTask.callback = continuationCallback;
|
|
|
|
if (enableProfiling) {
|
|
// $FlowFixMe[incompatible-call] found when upgrading Flow
|
|
markTaskYield(currentTask, currentTime);
|
|
}
|
|
|
|
advanceTimers(currentTime);
|
|
return true;
|
|
} else {
|
|
if (enableProfiling) {
|
|
// $FlowFixMe[incompatible-call] found when upgrading Flow
|
|
markTaskCompleted(currentTask, currentTime); // $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
currentTask.isQueued = false;
|
|
}
|
|
|
|
if (currentTask === peek(taskQueue)) {
|
|
pop(taskQueue);
|
|
}
|
|
|
|
advanceTimers(currentTime);
|
|
}
|
|
} else {
|
|
pop(taskQueue);
|
|
}
|
|
|
|
currentTask = peek(taskQueue);
|
|
} // Return whether there's additional work
|
|
|
|
if (currentTask !== null) {
|
|
return true;
|
|
} else {
|
|
var firstTimer = peek(timerQueue);
|
|
|
|
if (firstTimer !== null) {
|
|
requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function unstable_runWithPriority(priorityLevel, eventHandler) {
|
|
switch (priorityLevel) {
|
|
case ImmediatePriority:
|
|
case UserBlockingPriority:
|
|
case NormalPriority:
|
|
case LowPriority:
|
|
case IdlePriority:
|
|
break;
|
|
|
|
default:
|
|
priorityLevel = NormalPriority;
|
|
}
|
|
|
|
var previousPriorityLevel = currentPriorityLevel;
|
|
currentPriorityLevel = priorityLevel;
|
|
|
|
try {
|
|
return eventHandler();
|
|
} finally {
|
|
currentPriorityLevel = previousPriorityLevel;
|
|
}
|
|
}
|
|
|
|
function unstable_next(eventHandler) {
|
|
var priorityLevel;
|
|
|
|
switch (currentPriorityLevel) {
|
|
case ImmediatePriority:
|
|
case UserBlockingPriority:
|
|
case NormalPriority:
|
|
// Shift down to normal priority
|
|
priorityLevel = NormalPriority;
|
|
break;
|
|
|
|
default:
|
|
// Anything lower than normal priority should remain at the current level.
|
|
priorityLevel = currentPriorityLevel;
|
|
break;
|
|
}
|
|
|
|
var previousPriorityLevel = currentPriorityLevel;
|
|
currentPriorityLevel = priorityLevel;
|
|
|
|
try {
|
|
return eventHandler();
|
|
} finally {
|
|
currentPriorityLevel = previousPriorityLevel;
|
|
}
|
|
}
|
|
|
|
function unstable_wrapCallback(callback) {
|
|
var parentPriorityLevel = currentPriorityLevel; // $FlowFixMe[incompatible-return]
|
|
// $FlowFixMe[missing-this-annot]
|
|
|
|
return function () {
|
|
// This is a fork of runWithPriority, inlined for performance.
|
|
var previousPriorityLevel = currentPriorityLevel;
|
|
currentPriorityLevel = parentPriorityLevel;
|
|
|
|
try {
|
|
return callback.apply(this, arguments);
|
|
} finally {
|
|
currentPriorityLevel = previousPriorityLevel;
|
|
}
|
|
};
|
|
}
|
|
|
|
function unstable_scheduleCallback(priorityLevel, callback, options) {
|
|
var currentTime = exports.unstable_now();
|
|
var startTime;
|
|
|
|
if (typeof options === "object" && options !== null) {
|
|
var delay = options.delay;
|
|
|
|
if (typeof delay === "number" && delay > 0) {
|
|
startTime = currentTime + delay;
|
|
} else {
|
|
startTime = currentTime;
|
|
}
|
|
} else {
|
|
startTime = currentTime;
|
|
}
|
|
|
|
var timeout;
|
|
|
|
switch (priorityLevel) {
|
|
case ImmediatePriority:
|
|
// Times out immediately
|
|
timeout = -1;
|
|
break;
|
|
|
|
case UserBlockingPriority:
|
|
// Eventually times out
|
|
timeout = userBlockingPriorityTimeout;
|
|
break;
|
|
|
|
case IdlePriority:
|
|
// Never times out
|
|
timeout = maxSigned31BitInt;
|
|
break;
|
|
|
|
case LowPriority:
|
|
// Eventually times out
|
|
timeout = lowPriorityTimeout;
|
|
break;
|
|
|
|
case NormalPriority:
|
|
default:
|
|
// Eventually times out
|
|
timeout = normalPriorityTimeout;
|
|
break;
|
|
}
|
|
|
|
var expirationTime = startTime + timeout;
|
|
var newTask = {
|
|
id: taskIdCounter++,
|
|
callback: callback,
|
|
priorityLevel: priorityLevel,
|
|
startTime: startTime,
|
|
expirationTime: expirationTime,
|
|
sortIndex: -1
|
|
};
|
|
|
|
if (enableProfiling) {
|
|
newTask.isQueued = false;
|
|
}
|
|
|
|
if (startTime > currentTime) {
|
|
// This is a delayed task.
|
|
newTask.sortIndex = startTime;
|
|
push(timerQueue, newTask);
|
|
|
|
if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
|
|
// All tasks are delayed, and this is the task with the earliest delay.
|
|
if (isHostTimeoutScheduled) {
|
|
// Cancel an existing timeout.
|
|
cancelHostTimeout();
|
|
} else {
|
|
isHostTimeoutScheduled = true;
|
|
} // Schedule a timeout.
|
|
|
|
requestHostTimeout(handleTimeout, startTime - currentTime);
|
|
}
|
|
} else {
|
|
newTask.sortIndex = expirationTime;
|
|
push(taskQueue, newTask);
|
|
|
|
if (enableProfiling) {
|
|
markTaskStart(newTask, currentTime);
|
|
newTask.isQueued = true;
|
|
} // Schedule a host callback, if needed. If we're already performing work,
|
|
// wait until the next time we yield.
|
|
|
|
if (!isHostCallbackScheduled && !isPerformingWork) {
|
|
isHostCallbackScheduled = true;
|
|
requestHostCallback();
|
|
}
|
|
}
|
|
|
|
return newTask;
|
|
}
|
|
|
|
function unstable_pauseExecution() {
|
|
isSchedulerPaused = true;
|
|
}
|
|
|
|
function unstable_continueExecution() {
|
|
isSchedulerPaused = false;
|
|
|
|
if (!isHostCallbackScheduled && !isPerformingWork) {
|
|
isHostCallbackScheduled = true;
|
|
requestHostCallback();
|
|
}
|
|
}
|
|
|
|
function unstable_getFirstCallbackNode() {
|
|
return peek(taskQueue);
|
|
}
|
|
|
|
function unstable_cancelCallback(task) {
|
|
if (enableProfiling) {
|
|
if (task.isQueued) {
|
|
var currentTime = exports.unstable_now();
|
|
markTaskCanceled(task, currentTime);
|
|
task.isQueued = false;
|
|
}
|
|
} // Null out the callback to indicate the task has been canceled. (Can't
|
|
// remove from the queue because you can't remove arbitrary nodes from an
|
|
// array based heap, only the first one.)
|
|
|
|
task.callback = null;
|
|
}
|
|
|
|
function unstable_getCurrentPriorityLevel() {
|
|
return currentPriorityLevel;
|
|
}
|
|
|
|
var isMessageLoopRunning = false;
|
|
var taskTimeoutID = -1; // Scheduler periodically yields in case there is other work on the main
|
|
// thread, like user events. By default, it yields multiple times per frame.
|
|
// It does not attempt to align with frame boundaries, since most tasks don't
|
|
// need to be frame aligned; for those that do, use requestAnimationFrame.
|
|
|
|
var frameInterval = frameYieldMs;
|
|
var continuousInputInterval = continuousYieldMs;
|
|
var maxInterval = maxYieldMs;
|
|
var startTime = -1;
|
|
var needsPaint = false;
|
|
|
|
function shouldYieldToHost() {
|
|
var timeElapsed = exports.unstable_now() - startTime;
|
|
|
|
if (timeElapsed < frameInterval) {
|
|
// The main thread has only been blocked for a really short amount of time;
|
|
// smaller than a single frame. Don't yield yet.
|
|
return false;
|
|
} // The main thread has been blocked for a non-negligible amount of time. We
|
|
// may want to yield control of the main thread, so the browser can perform
|
|
// high priority tasks. The main ones are painting and user input. If there's
|
|
// a pending paint or a pending input, then we should yield. But if there's
|
|
// neither, then we can yield less often while remaining responsive. We'll
|
|
// eventually yield regardless, since there could be a pending paint that
|
|
// wasn't accompanied by a call to `requestPaint`, or other main thread tasks
|
|
// like network events.
|
|
|
|
if (enableIsInputPending) {
|
|
if (needsPaint) {
|
|
// There's a pending paint (signaled by `requestPaint`). Yield now.
|
|
return true;
|
|
}
|
|
|
|
if (timeElapsed < continuousInputInterval) {
|
|
// We haven't blocked the thread for that long. Only yield if there's a
|
|
// pending discrete input (e.g. click). It's OK if there's pending
|
|
// continuous input (e.g. mouseover).
|
|
if (isInputPending !== null) {
|
|
return isInputPending();
|
|
}
|
|
} else if (timeElapsed < maxInterval) {
|
|
// Yield if there's either a pending discrete or continuous input.
|
|
if (isInputPending !== null) {
|
|
return isInputPending(continuousOptions);
|
|
}
|
|
} else {
|
|
// We've blocked the thread for a long time. Even if there's no pending
|
|
// input, there may be some other scheduled work that we don't know about,
|
|
// like a network event. Yield now.
|
|
return true;
|
|
}
|
|
} // `isInputPending` isn't available. Yield now.
|
|
|
|
return true;
|
|
}
|
|
|
|
function requestPaint() {
|
|
if (
|
|
enableIsInputPending &&
|
|
navigator !== undefined && // $FlowFixMe[prop-missing]
|
|
navigator.scheduling !== undefined && // $FlowFixMe[incompatible-type]
|
|
navigator.scheduling.isInputPending !== undefined
|
|
) {
|
|
needsPaint = true;
|
|
} // Since we yield every frame regardless, `requestPaint` has no effect.
|
|
}
|
|
|
|
function forceFrameRate(fps) {
|
|
if (fps < 0 || fps > 125) {
|
|
// Using console['error'] to evade Babel and ESLint
|
|
console["error"](
|
|
"forceFrameRate takes a positive int between 0 and 125, " +
|
|
"forcing frame rates higher than 125 fps is not supported"
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (fps > 0) {
|
|
frameInterval = Math.floor(1000 / fps);
|
|
} else {
|
|
// reset the framerate
|
|
frameInterval = frameYieldMs;
|
|
}
|
|
}
|
|
|
|
var performWorkUntilDeadline = function () {
|
|
if (isMessageLoopRunning) {
|
|
var currentTime = exports.unstable_now(); // Keep track of the start time so we can measure how long the main thread
|
|
// has been blocked.
|
|
|
|
startTime = currentTime; // If a scheduler task throws, exit the current browser task so the
|
|
// error can be observed.
|
|
//
|
|
// Intentionally not using a try-catch, since that makes some debugging
|
|
// techniques harder. Instead, if `flushWork` errors, then `hasMoreWork` will
|
|
// remain true, and we'll continue the work loop.
|
|
|
|
var hasMoreWork = true;
|
|
|
|
try {
|
|
hasMoreWork = flushWork(currentTime);
|
|
} finally {
|
|
if (hasMoreWork) {
|
|
// If there's more work, schedule the next message event at the end
|
|
// of the preceding one.
|
|
schedulePerformWorkUntilDeadline();
|
|
} else {
|
|
isMessageLoopRunning = false;
|
|
}
|
|
}
|
|
} // Yielding to the browser will give it a chance to paint, so we can
|
|
// reset this.
|
|
|
|
needsPaint = false;
|
|
};
|
|
|
|
var schedulePerformWorkUntilDeadline;
|
|
|
|
if (typeof localSetImmediate === "function") {
|
|
// Node.js and old IE.
|
|
// There's a few reasons for why we prefer setImmediate.
|
|
//
|
|
// Unlike MessageChannel, it doesn't prevent a Node.js process from exiting.
|
|
// (Even though this is a DOM fork of the Scheduler, you could get here
|
|
// with a mix of Node.js 15+, which has a MessageChannel, and jsdom.)
|
|
// https://github.com/facebook/react/issues/20756
|
|
//
|
|
// But also, it runs earlier which is the semantic we want.
|
|
// If other browsers ever implement it, it's better to use it.
|
|
// Although both of these would be inferior to native scheduling.
|
|
schedulePerformWorkUntilDeadline = function () {
|
|
localSetImmediate(performWorkUntilDeadline);
|
|
};
|
|
} else if (typeof MessageChannel !== "undefined") {
|
|
// DOM and Worker environments.
|
|
// We prefer MessageChannel because of the 4ms setTimeout clamping.
|
|
var channel = new MessageChannel();
|
|
var port = channel.port2;
|
|
channel.port1.onmessage = performWorkUntilDeadline;
|
|
|
|
schedulePerformWorkUntilDeadline = function () {
|
|
port.postMessage(null);
|
|
};
|
|
} else {
|
|
// We should only fallback here in non-browser environments.
|
|
schedulePerformWorkUntilDeadline = function () {
|
|
// $FlowFixMe[not-a-function] nullable value
|
|
localSetTimeout(performWorkUntilDeadline, 0);
|
|
};
|
|
}
|
|
|
|
function requestHostCallback() {
|
|
if (!isMessageLoopRunning) {
|
|
isMessageLoopRunning = true;
|
|
schedulePerformWorkUntilDeadline();
|
|
}
|
|
}
|
|
|
|
function requestHostTimeout(callback, ms) {
|
|
// $FlowFixMe[not-a-function] nullable value
|
|
taskTimeoutID = localSetTimeout(function () {
|
|
callback(exports.unstable_now());
|
|
}, ms);
|
|
}
|
|
|
|
function cancelHostTimeout() {
|
|
// $FlowFixMe[not-a-function] nullable value
|
|
localClearTimeout(taskTimeoutID);
|
|
taskTimeoutID = -1;
|
|
}
|
|
var unstable_Profiling = enableProfiling
|
|
? {
|
|
startLoggingProfilingEvents: startLoggingProfilingEvents,
|
|
stopLoggingProfilingEvents: stopLoggingProfilingEvents
|
|
}
|
|
: null;
|
|
|
|
exports.unstable_IdlePriority = IdlePriority;
|
|
exports.unstable_ImmediatePriority = ImmediatePriority;
|
|
exports.unstable_LowPriority = LowPriority;
|
|
exports.unstable_NormalPriority = NormalPriority;
|
|
exports.unstable_Profiling = unstable_Profiling;
|
|
exports.unstable_UserBlockingPriority = UserBlockingPriority;
|
|
exports.unstable_cancelCallback = unstable_cancelCallback;
|
|
exports.unstable_continueExecution = unstable_continueExecution;
|
|
exports.unstable_forceFrameRate = forceFrameRate;
|
|
exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
|
|
exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
|
|
exports.unstable_next = unstable_next;
|
|
exports.unstable_pauseExecution = unstable_pauseExecution;
|
|
exports.unstable_requestPaint = requestPaint;
|
|
exports.unstable_runWithPriority = unstable_runWithPriority;
|
|
exports.unstable_scheduleCallback = unstable_scheduleCallback;
|
|
exports.unstable_shouldYield = shouldYieldToHost;
|
|
exports.unstable_wrapCallback = unstable_wrapCallback;
|
|
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
|
|
if (
|
|
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== "undefined" &&
|
|
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
|
|
"function"
|
|
) {
|
|
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
|
|
}
|
|
})();
|
|
}
|