| 1 | /**
|
|---|
| 2 | * @license React
|
|---|
| 3 | * scheduler-unstable_mock.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 | var taskQueue = [],
|
|---|
| 61 | timerQueue = [],
|
|---|
| 62 | taskIdCounter = 1,
|
|---|
| 63 | currentTask = null,
|
|---|
| 64 | currentPriorityLevel = 3,
|
|---|
| 65 | isPerformingWork = !1,
|
|---|
| 66 | isHostCallbackScheduled = !1,
|
|---|
| 67 | isHostTimeoutScheduled = !1,
|
|---|
| 68 | currentMockTime = 0,
|
|---|
| 69 | scheduledCallback = null,
|
|---|
| 70 | scheduledTimeout = null,
|
|---|
| 71 | timeoutTime = -1,
|
|---|
| 72 | yieldedValues = null,
|
|---|
| 73 | expectedNumberOfYields = -1,
|
|---|
| 74 | didStop = !1,
|
|---|
| 75 | isFlushing = !1,
|
|---|
| 76 | needsPaint = !1,
|
|---|
| 77 | shouldYieldForPaint = !1,
|
|---|
| 78 | disableYieldValue = !1;
|
|---|
| 79 | function advanceTimers(currentTime) {
|
|---|
| 80 | for (var timer = peek(timerQueue); null !== timer; ) {
|
|---|
| 81 | if (null === timer.callback) pop(timerQueue);
|
|---|
| 82 | else if (timer.startTime <= currentTime)
|
|---|
| 83 | pop(timerQueue),
|
|---|
| 84 | (timer.sortIndex = timer.expirationTime),
|
|---|
| 85 | push(taskQueue, timer);
|
|---|
| 86 | else break;
|
|---|
| 87 | timer = peek(timerQueue);
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | function handleTimeout(currentTime) {
|
|---|
| 91 | isHostTimeoutScheduled = !1;
|
|---|
| 92 | advanceTimers(currentTime);
|
|---|
| 93 | if (!isHostCallbackScheduled)
|
|---|
| 94 | if (null !== peek(taskQueue))
|
|---|
| 95 | (isHostCallbackScheduled = !0), (scheduledCallback = flushWork);
|
|---|
| 96 | else {
|
|---|
| 97 | var firstTimer = peek(timerQueue);
|
|---|
| 98 | null !== firstTimer &&
|
|---|
| 99 | ((currentTime = firstTimer.startTime - currentTime),
|
|---|
| 100 | (scheduledTimeout = handleTimeout),
|
|---|
| 101 | (timeoutTime = currentMockTime + currentTime));
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | function flushWork(hasTimeRemaining, initialTime) {
|
|---|
| 105 | isHostCallbackScheduled = !1;
|
|---|
| 106 | isHostTimeoutScheduled &&
|
|---|
| 107 | ((isHostTimeoutScheduled = !1),
|
|---|
| 108 | (scheduledTimeout = null),
|
|---|
| 109 | (timeoutTime = -1));
|
|---|
| 110 | isPerformingWork = !0;
|
|---|
| 111 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 112 | try {
|
|---|
| 113 | a: {
|
|---|
| 114 | advanceTimers(initialTime);
|
|---|
| 115 | for (
|
|---|
| 116 | currentTask = peek(taskQueue);
|
|---|
| 117 | null !== currentTask &&
|
|---|
| 118 | (!(currentTask.expirationTime > initialTime) ||
|
|---|
| 119 | (hasTimeRemaining && !shouldYieldToHost()));
|
|---|
| 120 |
|
|---|
| 121 | ) {
|
|---|
| 122 | var callback = currentTask.callback;
|
|---|
| 123 | if ("function" === typeof callback) {
|
|---|
| 124 | currentTask.callback = null;
|
|---|
| 125 | currentPriorityLevel = currentTask.priorityLevel;
|
|---|
| 126 | var continuationCallback = callback(
|
|---|
| 127 | currentTask.expirationTime <= initialTime
|
|---|
| 128 | );
|
|---|
| 129 | initialTime = currentMockTime;
|
|---|
| 130 | if ("function" === typeof continuationCallback) {
|
|---|
| 131 | if (
|
|---|
| 132 | ((currentTask.callback = continuationCallback),
|
|---|
| 133 | advanceTimers(initialTime),
|
|---|
| 134 | shouldYieldForPaint)
|
|---|
| 135 | ) {
|
|---|
| 136 | var JSCompiler_inline_result = (needsPaint = !0);
|
|---|
| 137 | break a;
|
|---|
| 138 | }
|
|---|
| 139 | } else
|
|---|
| 140 | currentTask === peek(taskQueue) && pop(taskQueue),
|
|---|
| 141 | advanceTimers(initialTime);
|
|---|
| 142 | } else pop(taskQueue);
|
|---|
| 143 | currentTask = peek(taskQueue);
|
|---|
| 144 | }
|
|---|
| 145 | if (null !== currentTask) JSCompiler_inline_result = !0;
|
|---|
| 146 | else {
|
|---|
| 147 | var firstTimer = peek(timerQueue);
|
|---|
| 148 | if (null !== firstTimer) {
|
|---|
| 149 | var ms = firstTimer.startTime - initialTime;
|
|---|
| 150 | scheduledTimeout = handleTimeout;
|
|---|
| 151 | timeoutTime = currentMockTime + ms;
|
|---|
| 152 | }
|
|---|
| 153 | JSCompiler_inline_result = !1;
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | return JSCompiler_inline_result;
|
|---|
| 157 | } finally {
|
|---|
| 158 | (currentTask = null),
|
|---|
| 159 | (currentPriorityLevel = previousPriorityLevel),
|
|---|
| 160 | (isPerformingWork = !1);
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | function shouldYieldToHost() {
|
|---|
| 164 | return (0 === expectedNumberOfYields && null === yieldedValues) ||
|
|---|
| 165 | (-1 !== expectedNumberOfYields &&
|
|---|
| 166 | null !== yieldedValues &&
|
|---|
| 167 | yieldedValues.length >= expectedNumberOfYields) ||
|
|---|
| 168 | (shouldYieldForPaint && needsPaint)
|
|---|
| 169 | ? (didStop = !0)
|
|---|
| 170 | : !1;
|
|---|
| 171 | }
|
|---|
| 172 | function unstable_flushAllWithoutAsserting() {
|
|---|
| 173 | if (isFlushing) throw Error("Already flushing work.");
|
|---|
| 174 | if (null !== scheduledCallback) {
|
|---|
| 175 | var cb = scheduledCallback;
|
|---|
| 176 | isFlushing = !0;
|
|---|
| 177 | try {
|
|---|
| 178 | var hasMoreWork = !0;
|
|---|
| 179 | do hasMoreWork = cb(!0, currentMockTime);
|
|---|
| 180 | while (hasMoreWork);
|
|---|
| 181 | hasMoreWork || (scheduledCallback = null);
|
|---|
| 182 | return !0;
|
|---|
| 183 | } finally {
|
|---|
| 184 | isFlushing = !1;
|
|---|
| 185 | }
|
|---|
| 186 | } else return !1;
|
|---|
| 187 | }
|
|---|
| 188 | exports.log = function (value) {
|
|---|
| 189 | "disabledLog" === console.log.name ||
|
|---|
| 190 | disableYieldValue ||
|
|---|
| 191 | (null === yieldedValues
|
|---|
| 192 | ? (yieldedValues = [value])
|
|---|
| 193 | : yieldedValues.push(value));
|
|---|
| 194 | };
|
|---|
| 195 | exports.reset = function () {
|
|---|
| 196 | if (isFlushing) throw Error("Cannot reset while already flushing work.");
|
|---|
| 197 | currentMockTime = 0;
|
|---|
| 198 | scheduledTimeout = scheduledCallback = null;
|
|---|
| 199 | timeoutTime = -1;
|
|---|
| 200 | yieldedValues = null;
|
|---|
| 201 | expectedNumberOfYields = -1;
|
|---|
| 202 | needsPaint = isFlushing = didStop = !1;
|
|---|
| 203 | };
|
|---|
| 204 | exports.unstable_IdlePriority = 5;
|
|---|
| 205 | exports.unstable_ImmediatePriority = 1;
|
|---|
| 206 | exports.unstable_LowPriority = 4;
|
|---|
| 207 | exports.unstable_NormalPriority = 3;
|
|---|
| 208 | exports.unstable_Profiling = null;
|
|---|
| 209 | exports.unstable_UserBlockingPriority = 2;
|
|---|
| 210 | exports.unstable_advanceTime = function (ms) {
|
|---|
| 211 | "disabledLog" === console.log.name ||
|
|---|
| 212 | disableYieldValue ||
|
|---|
| 213 | ((currentMockTime += ms),
|
|---|
| 214 | null !== scheduledTimeout &&
|
|---|
| 215 | timeoutTime <= currentMockTime &&
|
|---|
| 216 | (scheduledTimeout(currentMockTime),
|
|---|
| 217 | (timeoutTime = -1),
|
|---|
| 218 | (scheduledTimeout = null)));
|
|---|
| 219 | };
|
|---|
| 220 | exports.unstable_cancelCallback = function (task) {
|
|---|
| 221 | task.callback = null;
|
|---|
| 222 | };
|
|---|
| 223 | exports.unstable_clearLog = function () {
|
|---|
| 224 | if (null === yieldedValues) return [];
|
|---|
| 225 | var values = yieldedValues;
|
|---|
| 226 | yieldedValues = null;
|
|---|
| 227 | return values;
|
|---|
| 228 | };
|
|---|
| 229 | exports.unstable_flushAll = function () {
|
|---|
| 230 | if (null !== yieldedValues)
|
|---|
| 231 | throw Error(
|
|---|
| 232 | "Log is not empty. Assert on the log of yielded values before flushing additional work."
|
|---|
| 233 | );
|
|---|
| 234 | unstable_flushAllWithoutAsserting();
|
|---|
| 235 | if (null !== yieldedValues)
|
|---|
| 236 | throw Error(
|
|---|
| 237 | "While flushing work, something yielded a value. Use an assertion helper to assert on the log of yielded values, e.g. expect(Scheduler).toFlushAndYield([...])"
|
|---|
| 238 | );
|
|---|
| 239 | };
|
|---|
| 240 | exports.unstable_flushAllWithoutAsserting = unstable_flushAllWithoutAsserting;
|
|---|
| 241 | exports.unstable_flushExpired = function () {
|
|---|
| 242 | if (isFlushing) throw Error("Already flushing work.");
|
|---|
| 243 | if (null !== scheduledCallback) {
|
|---|
| 244 | isFlushing = !0;
|
|---|
| 245 | try {
|
|---|
| 246 | scheduledCallback(!1, currentMockTime) || (scheduledCallback = null);
|
|---|
| 247 | } finally {
|
|---|
| 248 | isFlushing = !1;
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | };
|
|---|
| 252 | exports.unstable_flushNumberOfYields = function (count) {
|
|---|
| 253 | if (isFlushing) throw Error("Already flushing work.");
|
|---|
| 254 | if (null !== scheduledCallback) {
|
|---|
| 255 | var cb = scheduledCallback;
|
|---|
| 256 | expectedNumberOfYields = count;
|
|---|
| 257 | isFlushing = !0;
|
|---|
| 258 | try {
|
|---|
| 259 | count = !0;
|
|---|
| 260 | do count = cb(!0, currentMockTime);
|
|---|
| 261 | while (count && !didStop);
|
|---|
| 262 | count || (scheduledCallback = null);
|
|---|
| 263 | } finally {
|
|---|
| 264 | (expectedNumberOfYields = -1), (isFlushing = didStop = !1);
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 | };
|
|---|
| 268 | exports.unstable_flushUntilNextPaint = function () {
|
|---|
| 269 | if (isFlushing) throw Error("Already flushing work.");
|
|---|
| 270 | if (null !== scheduledCallback) {
|
|---|
| 271 | var cb = scheduledCallback;
|
|---|
| 272 | shouldYieldForPaint = !0;
|
|---|
| 273 | needsPaint = !1;
|
|---|
| 274 | isFlushing = !0;
|
|---|
| 275 | try {
|
|---|
| 276 | var hasMoreWork = !0;
|
|---|
| 277 | do hasMoreWork = cb(!0, currentMockTime);
|
|---|
| 278 | while (hasMoreWork && !didStop);
|
|---|
| 279 | hasMoreWork || (scheduledCallback = null);
|
|---|
| 280 | } finally {
|
|---|
| 281 | isFlushing = didStop = shouldYieldForPaint = !1;
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 | return !1;
|
|---|
| 285 | };
|
|---|
| 286 | exports.unstable_forceFrameRate = function () {};
|
|---|
| 287 | exports.unstable_getCurrentPriorityLevel = function () {
|
|---|
| 288 | return currentPriorityLevel;
|
|---|
| 289 | };
|
|---|
| 290 | exports.unstable_hasPendingWork = function () {
|
|---|
| 291 | return null !== scheduledCallback;
|
|---|
| 292 | };
|
|---|
| 293 | exports.unstable_next = function (eventHandler) {
|
|---|
| 294 | switch (currentPriorityLevel) {
|
|---|
| 295 | case 1:
|
|---|
| 296 | case 2:
|
|---|
| 297 | case 3:
|
|---|
| 298 | var priorityLevel = 3;
|
|---|
| 299 | break;
|
|---|
| 300 | default:
|
|---|
| 301 | priorityLevel = currentPriorityLevel;
|
|---|
| 302 | }
|
|---|
| 303 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 304 | currentPriorityLevel = priorityLevel;
|
|---|
| 305 | try {
|
|---|
| 306 | return eventHandler();
|
|---|
| 307 | } finally {
|
|---|
| 308 | currentPriorityLevel = previousPriorityLevel;
|
|---|
| 309 | }
|
|---|
| 310 | };
|
|---|
| 311 | exports.unstable_now = function () {
|
|---|
| 312 | return currentMockTime;
|
|---|
| 313 | };
|
|---|
| 314 | exports.unstable_requestPaint = function () {
|
|---|
| 315 | needsPaint = !0;
|
|---|
| 316 | };
|
|---|
| 317 | exports.unstable_runWithPriority = function (priorityLevel, eventHandler) {
|
|---|
| 318 | switch (priorityLevel) {
|
|---|
| 319 | case 1:
|
|---|
| 320 | case 2:
|
|---|
| 321 | case 3:
|
|---|
| 322 | case 4:
|
|---|
| 323 | case 5:
|
|---|
| 324 | break;
|
|---|
| 325 | default:
|
|---|
| 326 | priorityLevel = 3;
|
|---|
| 327 | }
|
|---|
| 328 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 329 | currentPriorityLevel = priorityLevel;
|
|---|
| 330 | try {
|
|---|
| 331 | return eventHandler();
|
|---|
| 332 | } finally {
|
|---|
| 333 | currentPriorityLevel = previousPriorityLevel;
|
|---|
| 334 | }
|
|---|
| 335 | };
|
|---|
| 336 | exports.unstable_scheduleCallback = function (
|
|---|
| 337 | priorityLevel,
|
|---|
| 338 | callback,
|
|---|
| 339 | options
|
|---|
| 340 | ) {
|
|---|
| 341 | var currentTime = currentMockTime;
|
|---|
| 342 | "object" === typeof options && null !== options
|
|---|
| 343 | ? ((options = options.delay),
|
|---|
| 344 | (options =
|
|---|
| 345 | "number" === typeof options && 0 < options
|
|---|
| 346 | ? currentTime + options
|
|---|
| 347 | : currentTime))
|
|---|
| 348 | : (options = currentTime);
|
|---|
| 349 | switch (priorityLevel) {
|
|---|
| 350 | case 1:
|
|---|
| 351 | var timeout = -1;
|
|---|
| 352 | break;
|
|---|
| 353 | case 2:
|
|---|
| 354 | timeout = 250;
|
|---|
| 355 | break;
|
|---|
| 356 | case 5:
|
|---|
| 357 | timeout = 1073741823;
|
|---|
| 358 | break;
|
|---|
| 359 | case 4:
|
|---|
| 360 | timeout = 1e4;
|
|---|
| 361 | break;
|
|---|
| 362 | default:
|
|---|
| 363 | timeout = 5e3;
|
|---|
| 364 | }
|
|---|
| 365 | timeout = options + timeout;
|
|---|
| 366 | priorityLevel = {
|
|---|
| 367 | id: taskIdCounter++,
|
|---|
| 368 | callback: callback,
|
|---|
| 369 | priorityLevel: priorityLevel,
|
|---|
| 370 | startTime: options,
|
|---|
| 371 | expirationTime: timeout,
|
|---|
| 372 | sortIndex: -1
|
|---|
| 373 | };
|
|---|
| 374 | options > currentTime
|
|---|
| 375 | ? ((priorityLevel.sortIndex = options),
|
|---|
| 376 | push(timerQueue, priorityLevel),
|
|---|
| 377 | null === peek(taskQueue) &&
|
|---|
| 378 | priorityLevel === peek(timerQueue) &&
|
|---|
| 379 | (isHostTimeoutScheduled
|
|---|
| 380 | ? ((scheduledTimeout = null), (timeoutTime = -1))
|
|---|
| 381 | : (isHostTimeoutScheduled = !0),
|
|---|
| 382 | (scheduledTimeout = handleTimeout),
|
|---|
| 383 | (timeoutTime = currentMockTime + (options - currentTime))))
|
|---|
| 384 | : ((priorityLevel.sortIndex = timeout),
|
|---|
| 385 | push(taskQueue, priorityLevel),
|
|---|
| 386 | isHostCallbackScheduled ||
|
|---|
| 387 | isPerformingWork ||
|
|---|
| 388 | ((isHostCallbackScheduled = !0), (scheduledCallback = flushWork)));
|
|---|
| 389 | return priorityLevel;
|
|---|
| 390 | };
|
|---|
| 391 | exports.unstable_setDisableYieldValue = function (newValue) {
|
|---|
| 392 | disableYieldValue = newValue;
|
|---|
| 393 | };
|
|---|
| 394 | exports.unstable_shouldYield = shouldYieldToHost;
|
|---|
| 395 | exports.unstable_wrapCallback = function (callback) {
|
|---|
| 396 | var parentPriorityLevel = currentPriorityLevel;
|
|---|
| 397 | return function () {
|
|---|
| 398 | var previousPriorityLevel = currentPriorityLevel;
|
|---|
| 399 | currentPriorityLevel = parentPriorityLevel;
|
|---|
| 400 | try {
|
|---|
| 401 | return callback.apply(this, arguments);
|
|---|
| 402 | } finally {
|
|---|
| 403 | currentPriorityLevel = previousPriorityLevel;
|
|---|
| 404 | }
|
|---|
| 405 | };
|
|---|
| 406 | };
|
|---|