scheduler.production.js 9.9 KB

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