| 1 | /**
|
|---|
| 2 | * @license React
|
|---|
| 3 | * scheduler.development.js
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) Meta Platforms, Inc. and affiliates.
|
|---|
| 6 | *
|
|---|
| 7 | * This source code is licensed under the MIT license found in the
|
|---|
| 8 | * LICENSE file in the root directory of this source tree.
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | "use strict";
|
|---|
| 12 | "production" !== process.env.NODE_ENV &&
|
|---|
| 13 | (function () {
|
|---|
| 14 | function performWorkUntilDeadline() {
|
|---|
| 15 | needsPaint = !1;
|
|---|
| 16 | if (isMessageLoopRunning) {
|
|---|
| 17 | var currentTime = exports.unstable_now();
|
|---|
| 18 | startTime = currentTime;
|
|---|
| 19 | var hasMoreWork = !0;
|
|---|
| 20 | try {
|
|---|
| 21 | a: {
|
|---|
| 22 | isHostCallbackScheduled = !1;
|
|---|
| 23 | isHostTimeoutScheduled &&
|
|---|
| 24 | ((isHostTimeoutScheduled = !1),
|
|---|
| 25 | localClearTimeout(taskTimeoutID),
|
|---|
| 26 | (taskTimeoutID = -1));
|
|---|
| 27 | isPerformingWork = !0;
|
|---|
| 28 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 29 | try {
|
|---|
| 30 | b: {
|
|---|
| 31 | advanceTimers(currentTime);
|
|---|
| 32 | for (
|
|---|
| 33 | currentTask = peek(taskQueue);
|
|---|
| 34 | null !== currentTask &&
|
|---|
| 35 | !(
|
|---|
| 36 | currentTask.expirationTime > currentTime &&
|
|---|
| 37 | shouldYieldToHost()
|
|---|
| 38 | );
|
|---|
| 39 |
|
|---|
| 40 | ) {
|
|---|
| 41 | var callback = currentTask.callback;
|
|---|
| 42 | if ("function" === typeof callback) {
|
|---|
| 43 | currentTask.callback = null;
|
|---|
| 44 | currentPriorityLevel = currentTask.priorityLevel;
|
|---|
| 45 | var continuationCallback = callback(
|
|---|
| 46 | currentTask.expirationTime <= currentTime
|
|---|
| 47 | );
|
|---|
| 48 | currentTime = exports.unstable_now();
|
|---|
| 49 | if ("function" === typeof continuationCallback) {
|
|---|
| 50 | currentTask.callback = continuationCallback;
|
|---|
| 51 | advanceTimers(currentTime);
|
|---|
| 52 | hasMoreWork = !0;
|
|---|
| 53 | break b;
|
|---|
| 54 | }
|
|---|
| 55 | currentTask === peek(taskQueue) && pop(taskQueue);
|
|---|
| 56 | advanceTimers(currentTime);
|
|---|
| 57 | } else pop(taskQueue);
|
|---|
| 58 | currentTask = peek(taskQueue);
|
|---|
| 59 | }
|
|---|
| 60 | if (null !== currentTask) hasMoreWork = !0;
|
|---|
| 61 | else {
|
|---|
| 62 | var firstTimer = peek(timerQueue);
|
|---|
| 63 | null !== firstTimer &&
|
|---|
| 64 | requestHostTimeout(
|
|---|
| 65 | handleTimeout,
|
|---|
| 66 | firstTimer.startTime - currentTime
|
|---|
| 67 | );
|
|---|
| 68 | hasMoreWork = !1;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 | break a;
|
|---|
| 72 | } finally {
|
|---|
| 73 | (currentTask = null),
|
|---|
| 74 | (currentPriorityLevel = previousPriorityLevel),
|
|---|
| 75 | (isPerformingWork = !1);
|
|---|
| 76 | }
|
|---|
| 77 | hasMoreWork = void 0;
|
|---|
| 78 | }
|
|---|
| 79 | } finally {
|
|---|
| 80 | hasMoreWork
|
|---|
| 81 | ? schedulePerformWorkUntilDeadline()
|
|---|
| 82 | : (isMessageLoopRunning = !1);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | function push(heap, node) {
|
|---|
| 87 | var index = heap.length;
|
|---|
| 88 | heap.push(node);
|
|---|
| 89 | a: for (; 0 < index; ) {
|
|---|
| 90 | var parentIndex = (index - 1) >>> 1,
|
|---|
| 91 | parent = heap[parentIndex];
|
|---|
| 92 | if (0 < compare(parent, node))
|
|---|
| 93 | (heap[parentIndex] = node),
|
|---|
| 94 | (heap[index] = parent),
|
|---|
| 95 | (index = parentIndex);
|
|---|
| 96 | else break a;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | function peek(heap) {
|
|---|
| 100 | return 0 === heap.length ? null : heap[0];
|
|---|
| 101 | }
|
|---|
| 102 | function pop(heap) {
|
|---|
| 103 | if (0 === heap.length) return null;
|
|---|
| 104 | var first = heap[0],
|
|---|
| 105 | last = heap.pop();
|
|---|
| 106 | if (last !== first) {
|
|---|
| 107 | heap[0] = last;
|
|---|
| 108 | a: for (
|
|---|
| 109 | var index = 0, length = heap.length, halfLength = length >>> 1;
|
|---|
| 110 | index < halfLength;
|
|---|
| 111 |
|
|---|
| 112 | ) {
|
|---|
| 113 | var leftIndex = 2 * (index + 1) - 1,
|
|---|
| 114 | left = heap[leftIndex],
|
|---|
| 115 | rightIndex = leftIndex + 1,
|
|---|
| 116 | right = heap[rightIndex];
|
|---|
| 117 | if (0 > compare(left, last))
|
|---|
| 118 | rightIndex < length && 0 > compare(right, left)
|
|---|
| 119 | ? ((heap[index] = right),
|
|---|
| 120 | (heap[rightIndex] = last),
|
|---|
| 121 | (index = rightIndex))
|
|---|
| 122 | : ((heap[index] = left),
|
|---|
| 123 | (heap[leftIndex] = last),
|
|---|
| 124 | (index = leftIndex));
|
|---|
| 125 | else if (rightIndex < length && 0 > compare(right, last))
|
|---|
| 126 | (heap[index] = right),
|
|---|
| 127 | (heap[rightIndex] = last),
|
|---|
| 128 | (index = rightIndex);
|
|---|
| 129 | else break a;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | return first;
|
|---|
| 133 | }
|
|---|
| 134 | function compare(a, b) {
|
|---|
| 135 | var diff = a.sortIndex - b.sortIndex;
|
|---|
| 136 | return 0 !== diff ? diff : a.id - b.id;
|
|---|
| 137 | }
|
|---|
| 138 | function advanceTimers(currentTime) {
|
|---|
| 139 | for (var timer = peek(timerQueue); null !== timer; ) {
|
|---|
| 140 | if (null === timer.callback) pop(timerQueue);
|
|---|
| 141 | else if (timer.startTime <= currentTime)
|
|---|
| 142 | pop(timerQueue),
|
|---|
| 143 | (timer.sortIndex = timer.expirationTime),
|
|---|
| 144 | push(taskQueue, timer);
|
|---|
| 145 | else break;
|
|---|
| 146 | timer = peek(timerQueue);
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | function handleTimeout(currentTime) {
|
|---|
| 150 | isHostTimeoutScheduled = !1;
|
|---|
| 151 | advanceTimers(currentTime);
|
|---|
| 152 | if (!isHostCallbackScheduled)
|
|---|
| 153 | if (null !== peek(taskQueue))
|
|---|
| 154 | (isHostCallbackScheduled = !0),
|
|---|
| 155 | isMessageLoopRunning ||
|
|---|
| 156 | ((isMessageLoopRunning = !0), schedulePerformWorkUntilDeadline());
|
|---|
| 157 | else {
|
|---|
| 158 | var firstTimer = peek(timerQueue);
|
|---|
| 159 | null !== firstTimer &&
|
|---|
| 160 | requestHostTimeout(
|
|---|
| 161 | handleTimeout,
|
|---|
| 162 | firstTimer.startTime - currentTime
|
|---|
| 163 | );
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | function shouldYieldToHost() {
|
|---|
| 167 | return needsPaint
|
|---|
| 168 | ? !0
|
|---|
| 169 | : exports.unstable_now() - startTime < frameInterval
|
|---|
| 170 | ? !1
|
|---|
| 171 | : !0;
|
|---|
| 172 | }
|
|---|
| 173 | function requestHostTimeout(callback, ms) {
|
|---|
| 174 | taskTimeoutID = localSetTimeout(function () {
|
|---|
| 175 | callback(exports.unstable_now());
|
|---|
| 176 | }, ms);
|
|---|
| 177 | }
|
|---|
| 178 | "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
|
|---|
| 179 | "function" ===
|
|---|
| 180 | typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart &&
|
|---|
| 181 | __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
|
|---|
| 182 | exports.unstable_now = void 0;
|
|---|
| 183 | if (
|
|---|
| 184 | "object" === typeof performance &&
|
|---|
| 185 | "function" === typeof performance.now
|
|---|
| 186 | ) {
|
|---|
| 187 | var localPerformance = performance;
|
|---|
| 188 | exports.unstable_now = function () {
|
|---|
| 189 | return localPerformance.now();
|
|---|
| 190 | };
|
|---|
| 191 | } else {
|
|---|
| 192 | var localDate = Date,
|
|---|
| 193 | initialTime = localDate.now();
|
|---|
| 194 | exports.unstable_now = function () {
|
|---|
| 195 | return localDate.now() - initialTime;
|
|---|
| 196 | };
|
|---|
| 197 | }
|
|---|
| 198 | var taskQueue = [],
|
|---|
| 199 | timerQueue = [],
|
|---|
| 200 | taskIdCounter = 1,
|
|---|
| 201 | currentTask = null,
|
|---|
| 202 | currentPriorityLevel = 3,
|
|---|
| 203 | isPerformingWork = !1,
|
|---|
| 204 | isHostCallbackScheduled = !1,
|
|---|
| 205 | isHostTimeoutScheduled = !1,
|
|---|
| 206 | needsPaint = !1,
|
|---|
| 207 | localSetTimeout = "function" === typeof setTimeout ? setTimeout : null,
|
|---|
| 208 | localClearTimeout =
|
|---|
| 209 | "function" === typeof clearTimeout ? clearTimeout : null,
|
|---|
| 210 | localSetImmediate =
|
|---|
| 211 | "undefined" !== typeof setImmediate ? setImmediate : null,
|
|---|
| 212 | isMessageLoopRunning = !1,
|
|---|
| 213 | taskTimeoutID = -1,
|
|---|
| 214 | frameInterval = 5,
|
|---|
| 215 | startTime = -1;
|
|---|
| 216 | if ("function" === typeof localSetImmediate)
|
|---|
| 217 | var schedulePerformWorkUntilDeadline = function () {
|
|---|
| 218 | localSetImmediate(performWorkUntilDeadline);
|
|---|
| 219 | };
|
|---|
| 220 | else if ("undefined" !== typeof MessageChannel) {
|
|---|
| 221 | var channel = new MessageChannel(),
|
|---|
| 222 | port = channel.port2;
|
|---|
| 223 | channel.port1.onmessage = performWorkUntilDeadline;
|
|---|
| 224 | schedulePerformWorkUntilDeadline = function () {
|
|---|
| 225 | port.postMessage(null);
|
|---|
| 226 | };
|
|---|
| 227 | } else
|
|---|
| 228 | schedulePerformWorkUntilDeadline = function () {
|
|---|
| 229 | localSetTimeout(performWorkUntilDeadline, 0);
|
|---|
| 230 | };
|
|---|
| 231 | exports.unstable_IdlePriority = 5;
|
|---|
| 232 | exports.unstable_ImmediatePriority = 1;
|
|---|
| 233 | exports.unstable_LowPriority = 4;
|
|---|
| 234 | exports.unstable_NormalPriority = 3;
|
|---|
| 235 | exports.unstable_Profiling = null;
|
|---|
| 236 | exports.unstable_UserBlockingPriority = 2;
|
|---|
| 237 | exports.unstable_cancelCallback = function (task) {
|
|---|
| 238 | task.callback = null;
|
|---|
| 239 | };
|
|---|
| 240 | exports.unstable_forceFrameRate = function (fps) {
|
|---|
| 241 | 0 > fps || 125 < fps
|
|---|
| 242 | ? console.error(
|
|---|
| 243 | "forceFrameRate takes a positive int between 0 and 125, forcing frame rates higher than 125 fps is not supported"
|
|---|
| 244 | )
|
|---|
| 245 | : (frameInterval = 0 < fps ? Math.floor(1e3 / fps) : 5);
|
|---|
| 246 | };
|
|---|
| 247 | exports.unstable_getCurrentPriorityLevel = function () {
|
|---|
| 248 | return currentPriorityLevel;
|
|---|
| 249 | };
|
|---|
| 250 | exports.unstable_next = function (eventHandler) {
|
|---|
| 251 | switch (currentPriorityLevel) {
|
|---|
| 252 | case 1:
|
|---|
| 253 | case 2:
|
|---|
| 254 | case 3:
|
|---|
| 255 | var priorityLevel = 3;
|
|---|
| 256 | break;
|
|---|
| 257 | default:
|
|---|
| 258 | priorityLevel = currentPriorityLevel;
|
|---|
| 259 | }
|
|---|
| 260 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 261 | currentPriorityLevel = priorityLevel;
|
|---|
| 262 | try {
|
|---|
| 263 | return eventHandler();
|
|---|
| 264 | } finally {
|
|---|
| 265 | currentPriorityLevel = previousPriorityLevel;
|
|---|
| 266 | }
|
|---|
| 267 | };
|
|---|
| 268 | exports.unstable_requestPaint = function () {
|
|---|
| 269 | needsPaint = !0;
|
|---|
| 270 | };
|
|---|
| 271 | exports.unstable_runWithPriority = function (priorityLevel, eventHandler) {
|
|---|
| 272 | switch (priorityLevel) {
|
|---|
| 273 | case 1:
|
|---|
| 274 | case 2:
|
|---|
| 275 | case 3:
|
|---|
| 276 | case 4:
|
|---|
| 277 | case 5:
|
|---|
| 278 | break;
|
|---|
| 279 | default:
|
|---|
| 280 | priorityLevel = 3;
|
|---|
| 281 | }
|
|---|
| 282 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 283 | currentPriorityLevel = priorityLevel;
|
|---|
| 284 | try {
|
|---|
| 285 | return eventHandler();
|
|---|
| 286 | } finally {
|
|---|
| 287 | currentPriorityLevel = previousPriorityLevel;
|
|---|
| 288 | }
|
|---|
| 289 | };
|
|---|
| 290 | exports.unstable_scheduleCallback = function (
|
|---|
| 291 | priorityLevel,
|
|---|
| 292 | callback,
|
|---|
| 293 | options
|
|---|
| 294 | ) {
|
|---|
| 295 | var currentTime = exports.unstable_now();
|
|---|
| 296 | "object" === typeof options && null !== options
|
|---|
| 297 | ? ((options = options.delay),
|
|---|
| 298 | (options =
|
|---|
| 299 | "number" === typeof options && 0 < options
|
|---|
| 300 | ? currentTime + options
|
|---|
| 301 | : currentTime))
|
|---|
| 302 | : (options = currentTime);
|
|---|
| 303 | switch (priorityLevel) {
|
|---|
| 304 | case 1:
|
|---|
| 305 | var timeout = -1;
|
|---|
| 306 | break;
|
|---|
| 307 | case 2:
|
|---|
| 308 | timeout = 250;
|
|---|
| 309 | break;
|
|---|
| 310 | case 5:
|
|---|
| 311 | timeout = 1073741823;
|
|---|
| 312 | break;
|
|---|
| 313 | case 4:
|
|---|
| 314 | timeout = 1e4;
|
|---|
| 315 | break;
|
|---|
| 316 | default:
|
|---|
| 317 | timeout = 5e3;
|
|---|
| 318 | }
|
|---|
| 319 | timeout = options + timeout;
|
|---|
| 320 | priorityLevel = {
|
|---|
| 321 | id: taskIdCounter++,
|
|---|
| 322 | callback: callback,
|
|---|
| 323 | priorityLevel: priorityLevel,
|
|---|
| 324 | startTime: options,
|
|---|
| 325 | expirationTime: timeout,
|
|---|
| 326 | sortIndex: -1
|
|---|
| 327 | };
|
|---|
| 328 | options > currentTime
|
|---|
| 329 | ? ((priorityLevel.sortIndex = options),
|
|---|
| 330 | push(timerQueue, priorityLevel),
|
|---|
| 331 | null === peek(taskQueue) &&
|
|---|
| 332 | priorityLevel === peek(timerQueue) &&
|
|---|
| 333 | (isHostTimeoutScheduled
|
|---|
| 334 | ? (localClearTimeout(taskTimeoutID), (taskTimeoutID = -1))
|
|---|
| 335 | : (isHostTimeoutScheduled = !0),
|
|---|
| 336 | requestHostTimeout(handleTimeout, options - currentTime)))
|
|---|
| 337 | : ((priorityLevel.sortIndex = timeout),
|
|---|
| 338 | push(taskQueue, priorityLevel),
|
|---|
| 339 | isHostCallbackScheduled ||
|
|---|
| 340 | isPerformingWork ||
|
|---|
| 341 | ((isHostCallbackScheduled = !0),
|
|---|
| 342 | isMessageLoopRunning ||
|
|---|
| 343 | ((isMessageLoopRunning = !0),
|
|---|
| 344 | schedulePerformWorkUntilDeadline())));
|
|---|
| 345 | return priorityLevel;
|
|---|
| 346 | };
|
|---|
| 347 | exports.unstable_shouldYield = shouldYieldToHost;
|
|---|
| 348 | exports.unstable_wrapCallback = function (callback) {
|
|---|
| 349 | var parentPriorityLevel = currentPriorityLevel;
|
|---|
| 350 | return function () {
|
|---|
| 351 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 352 | currentPriorityLevel = parentPriorityLevel;
|
|---|
| 353 | try {
|
|---|
| 354 | return callback.apply(this, arguments);
|
|---|
| 355 | } finally {
|
|---|
| 356 | currentPriorityLevel = previousPriorityLevel;
|
|---|
| 357 | }
|
|---|
| 358 | };
|
|---|
| 359 | };
|
|---|
| 360 | "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
|
|---|
| 361 | "function" ===
|
|---|
| 362 | typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
|
|---|
| 363 | __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
|
|---|
| 364 | })();
|
|---|