scheduler-unstable_mock.production.js 12 KB

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