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