scheduler-unstable_mock.development.js 13 KB

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