scheduler.development.js 12 KB

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