scheduler-unstable_mock.development.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /**
  2. * @license React
  3. * scheduler-unstable_mock.development.js
  4. *
  5. * Copyright (c) Facebook, Inc. and its 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. if (process.env.NODE_ENV !== "production") {
  12. (function() {
  13. 'use strict';
  14. var enableSchedulerDebugging = false;
  15. var enableProfiling = false;
  16. function push(heap, node) {
  17. var index = heap.length;
  18. heap.push(node);
  19. siftUp(heap, node, index);
  20. }
  21. function peek(heap) {
  22. return heap.length === 0 ? null : heap[0];
  23. }
  24. function pop(heap) {
  25. if (heap.length === 0) {
  26. return null;
  27. }
  28. var first = heap[0];
  29. var last = heap.pop();
  30. if (last !== first) {
  31. heap[0] = last;
  32. siftDown(heap, last, 0);
  33. }
  34. return first;
  35. }
  36. function siftUp(heap, node, i) {
  37. var index = i;
  38. while (index > 0) {
  39. var parentIndex = index - 1 >>> 1;
  40. var parent = heap[parentIndex];
  41. if (compare(parent, node) > 0) {
  42. // The parent is larger. Swap positions.
  43. heap[parentIndex] = node;
  44. heap[index] = parent;
  45. index = parentIndex;
  46. } else {
  47. // The parent is smaller. Exit.
  48. return;
  49. }
  50. }
  51. }
  52. function siftDown(heap, node, i) {
  53. var index = i;
  54. var length = heap.length;
  55. var halfLength = length >>> 1;
  56. while (index < halfLength) {
  57. var leftIndex = (index + 1) * 2 - 1;
  58. var left = heap[leftIndex];
  59. var rightIndex = leftIndex + 1;
  60. var right = heap[rightIndex]; // If the left or right node is smaller, swap with the smaller of those.
  61. if (compare(left, node) < 0) {
  62. if (rightIndex < length && compare(right, left) < 0) {
  63. heap[index] = right;
  64. heap[rightIndex] = node;
  65. index = rightIndex;
  66. } else {
  67. heap[index] = left;
  68. heap[leftIndex] = node;
  69. index = leftIndex;
  70. }
  71. } else if (rightIndex < length && compare(right, node) < 0) {
  72. heap[index] = right;
  73. heap[rightIndex] = node;
  74. index = rightIndex;
  75. } else {
  76. // Neither child is smaller. Exit.
  77. return;
  78. }
  79. }
  80. }
  81. function compare(a, b) {
  82. // Compare sort index first, then task id.
  83. var diff = a.sortIndex - b.sortIndex;
  84. return diff !== 0 ? diff : a.id - b.id;
  85. }
  86. // TODO: Use symbols?
  87. var ImmediatePriority = 1;
  88. var UserBlockingPriority = 2;
  89. var NormalPriority = 3;
  90. var LowPriority = 4;
  91. var IdlePriority = 5;
  92. function markTaskErrored(task, ms) {
  93. }
  94. /* eslint-disable no-var */
  95. // Math.pow(2, 30) - 1
  96. // 0b111111111111111111111111111111
  97. var maxSigned31BitInt = 1073741823; // Times out immediately
  98. var IMMEDIATE_PRIORITY_TIMEOUT = -1; // Eventually times out
  99. var USER_BLOCKING_PRIORITY_TIMEOUT = 250;
  100. var NORMAL_PRIORITY_TIMEOUT = 5000;
  101. var LOW_PRIORITY_TIMEOUT = 10000; // Never times out
  102. var IDLE_PRIORITY_TIMEOUT = maxSigned31BitInt; // Tasks are stored on a min heap
  103. var taskQueue = [];
  104. var timerQueue = []; // Incrementing id counter. Used to maintain insertion order.
  105. var taskIdCounter = 1; // Pausing the scheduler is useful for debugging.
  106. var currentTask = null;
  107. var currentPriorityLevel = NormalPriority; // This is set while performing work, to prevent re-entrance.
  108. var isPerformingWork = false;
  109. var isHostCallbackScheduled = false;
  110. var isHostTimeoutScheduled = false;
  111. var currentMockTime = 0;
  112. var scheduledCallback = null;
  113. var scheduledTimeout = null;
  114. var timeoutTime = -1;
  115. var yieldedValues = null;
  116. var expectedNumberOfYields = -1;
  117. var didStop = false;
  118. var isFlushing = false;
  119. var needsPaint = false;
  120. var shouldYieldForPaint = false;
  121. var disableYieldValue = false;
  122. function setDisableYieldValue(newValue) {
  123. disableYieldValue = newValue;
  124. }
  125. function advanceTimers(currentTime) {
  126. // Check for tasks that are no longer delayed and add them to the queue.
  127. var timer = peek(timerQueue);
  128. while (timer !== null) {
  129. if (timer.callback === null) {
  130. // Timer was cancelled.
  131. pop(timerQueue);
  132. } else if (timer.startTime <= currentTime) {
  133. // Timer fired. Transfer to the task queue.
  134. pop(timerQueue);
  135. timer.sortIndex = timer.expirationTime;
  136. push(taskQueue, timer);
  137. } else {
  138. // Remaining timers are pending.
  139. return;
  140. }
  141. timer = peek(timerQueue);
  142. }
  143. }
  144. function handleTimeout(currentTime) {
  145. isHostTimeoutScheduled = false;
  146. advanceTimers(currentTime);
  147. if (!isHostCallbackScheduled) {
  148. if (peek(taskQueue) !== null) {
  149. isHostCallbackScheduled = true;
  150. requestHostCallback(flushWork);
  151. } else {
  152. var firstTimer = peek(timerQueue);
  153. if (firstTimer !== null) {
  154. requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
  155. }
  156. }
  157. }
  158. }
  159. function flushWork(hasTimeRemaining, initialTime) {
  160. isHostCallbackScheduled = false;
  161. if (isHostTimeoutScheduled) {
  162. // We scheduled a timeout but it's no longer needed. Cancel it.
  163. isHostTimeoutScheduled = false;
  164. cancelHostTimeout();
  165. }
  166. isPerformingWork = true;
  167. var previousPriorityLevel = currentPriorityLevel;
  168. try {
  169. if (enableProfiling) {
  170. try {
  171. return workLoop(hasTimeRemaining, initialTime);
  172. } catch (error) {
  173. if (currentTask !== null) {
  174. var currentTime = getCurrentTime();
  175. markTaskErrored(currentTask, currentTime);
  176. currentTask.isQueued = false;
  177. }
  178. throw error;
  179. }
  180. } else {
  181. // No catch in prod code path.
  182. return workLoop(hasTimeRemaining, initialTime);
  183. }
  184. } finally {
  185. currentTask = null;
  186. currentPriorityLevel = previousPriorityLevel;
  187. isPerformingWork = false;
  188. }
  189. }
  190. function workLoop(hasTimeRemaining, initialTime) {
  191. var currentTime = initialTime;
  192. advanceTimers(currentTime);
  193. currentTask = peek(taskQueue);
  194. while (currentTask !== null && !(enableSchedulerDebugging )) {
  195. if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost())) {
  196. // This currentTask hasn't expired, and we've reached the deadline.
  197. break;
  198. }
  199. var callback = currentTask.callback;
  200. if (typeof callback === 'function') {
  201. currentTask.callback = null;
  202. currentPriorityLevel = currentTask.priorityLevel;
  203. var didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
  204. var continuationCallback = callback(didUserCallbackTimeout);
  205. currentTime = getCurrentTime();
  206. if (typeof continuationCallback === 'function') {
  207. currentTask.callback = continuationCallback;
  208. } else {
  209. if (currentTask === peek(taskQueue)) {
  210. pop(taskQueue);
  211. }
  212. }
  213. advanceTimers(currentTime);
  214. } else {
  215. pop(taskQueue);
  216. }
  217. currentTask = peek(taskQueue);
  218. } // Return whether there's additional work
  219. if (currentTask !== null) {
  220. return true;
  221. } else {
  222. var firstTimer = peek(timerQueue);
  223. if (firstTimer !== null) {
  224. requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
  225. }
  226. return false;
  227. }
  228. }
  229. function unstable_runWithPriority(priorityLevel, eventHandler) {
  230. switch (priorityLevel) {
  231. case ImmediatePriority:
  232. case UserBlockingPriority:
  233. case NormalPriority:
  234. case LowPriority:
  235. case IdlePriority:
  236. break;
  237. default:
  238. priorityLevel = NormalPriority;
  239. }
  240. var previousPriorityLevel = currentPriorityLevel;
  241. currentPriorityLevel = priorityLevel;
  242. try {
  243. return eventHandler();
  244. } finally {
  245. currentPriorityLevel = previousPriorityLevel;
  246. }
  247. }
  248. function unstable_next(eventHandler) {
  249. var priorityLevel;
  250. switch (currentPriorityLevel) {
  251. case ImmediatePriority:
  252. case UserBlockingPriority:
  253. case NormalPriority:
  254. // Shift down to normal priority
  255. priorityLevel = NormalPriority;
  256. break;
  257. default:
  258. // Anything lower than normal priority should remain at the current level.
  259. priorityLevel = currentPriorityLevel;
  260. break;
  261. }
  262. var previousPriorityLevel = currentPriorityLevel;
  263. currentPriorityLevel = priorityLevel;
  264. try {
  265. return eventHandler();
  266. } finally {
  267. currentPriorityLevel = previousPriorityLevel;
  268. }
  269. }
  270. function unstable_wrapCallback(callback) {
  271. var parentPriorityLevel = currentPriorityLevel;
  272. return function () {
  273. // This is a fork of runWithPriority, inlined for performance.
  274. var previousPriorityLevel = currentPriorityLevel;
  275. currentPriorityLevel = parentPriorityLevel;
  276. try {
  277. return callback.apply(this, arguments);
  278. } finally {
  279. currentPriorityLevel = previousPriorityLevel;
  280. }
  281. };
  282. }
  283. function unstable_scheduleCallback(priorityLevel, callback, options) {
  284. var currentTime = getCurrentTime();
  285. var startTime;
  286. if (typeof options === 'object' && options !== null) {
  287. var delay = options.delay;
  288. if (typeof delay === 'number' && delay > 0) {
  289. startTime = currentTime + delay;
  290. } else {
  291. startTime = currentTime;
  292. }
  293. } else {
  294. startTime = currentTime;
  295. }
  296. var timeout;
  297. switch (priorityLevel) {
  298. case ImmediatePriority:
  299. timeout = IMMEDIATE_PRIORITY_TIMEOUT;
  300. break;
  301. case UserBlockingPriority:
  302. timeout = USER_BLOCKING_PRIORITY_TIMEOUT;
  303. break;
  304. case IdlePriority:
  305. timeout = IDLE_PRIORITY_TIMEOUT;
  306. break;
  307. case LowPriority:
  308. timeout = LOW_PRIORITY_TIMEOUT;
  309. break;
  310. case NormalPriority:
  311. default:
  312. timeout = NORMAL_PRIORITY_TIMEOUT;
  313. break;
  314. }
  315. var expirationTime = startTime + timeout;
  316. var newTask = {
  317. id: taskIdCounter++,
  318. callback: callback,
  319. priorityLevel: priorityLevel,
  320. startTime: startTime,
  321. expirationTime: expirationTime,
  322. sortIndex: -1
  323. };
  324. if (startTime > currentTime) {
  325. // This is a delayed task.
  326. newTask.sortIndex = startTime;
  327. push(timerQueue, newTask);
  328. if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
  329. // All tasks are delayed, and this is the task with the earliest delay.
  330. if (isHostTimeoutScheduled) {
  331. // Cancel an existing timeout.
  332. cancelHostTimeout();
  333. } else {
  334. isHostTimeoutScheduled = true;
  335. } // Schedule a timeout.
  336. requestHostTimeout(handleTimeout, startTime - currentTime);
  337. }
  338. } else {
  339. newTask.sortIndex = expirationTime;
  340. push(taskQueue, newTask);
  341. // wait until the next time we yield.
  342. if (!isHostCallbackScheduled && !isPerformingWork) {
  343. isHostCallbackScheduled = true;
  344. requestHostCallback(flushWork);
  345. }
  346. }
  347. return newTask;
  348. }
  349. function unstable_pauseExecution() {
  350. }
  351. function unstable_continueExecution() {
  352. if (!isHostCallbackScheduled && !isPerformingWork) {
  353. isHostCallbackScheduled = true;
  354. requestHostCallback(flushWork);
  355. }
  356. }
  357. function unstable_getFirstCallbackNode() {
  358. return peek(taskQueue);
  359. }
  360. function unstable_cancelCallback(task) {
  361. // remove from the queue because you can't remove arbitrary nodes from an
  362. // array based heap, only the first one.)
  363. task.callback = null;
  364. }
  365. function unstable_getCurrentPriorityLevel() {
  366. return currentPriorityLevel;
  367. }
  368. function requestHostCallback(callback) {
  369. scheduledCallback = callback;
  370. }
  371. function requestHostTimeout(callback, ms) {
  372. scheduledTimeout = callback;
  373. timeoutTime = currentMockTime + ms;
  374. }
  375. function cancelHostTimeout() {
  376. scheduledTimeout = null;
  377. timeoutTime = -1;
  378. }
  379. function shouldYieldToHost() {
  380. if (expectedNumberOfYields === 0 && yieldedValues === null || expectedNumberOfYields !== -1 && yieldedValues !== null && yieldedValues.length >= expectedNumberOfYields || shouldYieldForPaint && needsPaint) {
  381. // We yielded at least as many values as expected. Stop flushing.
  382. didStop = true;
  383. return true;
  384. }
  385. return false;
  386. }
  387. function getCurrentTime() {
  388. return currentMockTime;
  389. }
  390. function forceFrameRate() {// No-op
  391. }
  392. function reset() {
  393. if (isFlushing) {
  394. throw new Error('Cannot reset while already flushing work.');
  395. }
  396. currentMockTime = 0;
  397. scheduledCallback = null;
  398. scheduledTimeout = null;
  399. timeoutTime = -1;
  400. yieldedValues = null;
  401. expectedNumberOfYields = -1;
  402. didStop = false;
  403. isFlushing = false;
  404. needsPaint = false;
  405. } // Should only be used via an assertion helper that inspects the yielded values.
  406. function unstable_flushNumberOfYields(count) {
  407. if (isFlushing) {
  408. throw new Error('Already flushing work.');
  409. }
  410. if (scheduledCallback !== null) {
  411. var cb = scheduledCallback;
  412. expectedNumberOfYields = count;
  413. isFlushing = true;
  414. try {
  415. var hasMoreWork = true;
  416. do {
  417. hasMoreWork = cb(true, currentMockTime);
  418. } while (hasMoreWork && !didStop);
  419. if (!hasMoreWork) {
  420. scheduledCallback = null;
  421. }
  422. } finally {
  423. expectedNumberOfYields = -1;
  424. didStop = false;
  425. isFlushing = false;
  426. }
  427. }
  428. }
  429. function unstable_flushUntilNextPaint() {
  430. if (isFlushing) {
  431. throw new Error('Already flushing work.');
  432. }
  433. if (scheduledCallback !== null) {
  434. var cb = scheduledCallback;
  435. shouldYieldForPaint = true;
  436. needsPaint = false;
  437. isFlushing = true;
  438. try {
  439. var hasMoreWork = true;
  440. do {
  441. hasMoreWork = cb(true, currentMockTime);
  442. } while (hasMoreWork && !didStop);
  443. if (!hasMoreWork) {
  444. scheduledCallback = null;
  445. }
  446. } finally {
  447. shouldYieldForPaint = false;
  448. didStop = false;
  449. isFlushing = false;
  450. }
  451. }
  452. }
  453. function unstable_flushExpired() {
  454. if (isFlushing) {
  455. throw new Error('Already flushing work.');
  456. }
  457. if (scheduledCallback !== null) {
  458. isFlushing = true;
  459. try {
  460. var hasMoreWork = scheduledCallback(false, currentMockTime);
  461. if (!hasMoreWork) {
  462. scheduledCallback = null;
  463. }
  464. } finally {
  465. isFlushing = false;
  466. }
  467. }
  468. }
  469. function unstable_flushAllWithoutAsserting() {
  470. // Returns false if no work was flushed.
  471. if (isFlushing) {
  472. throw new Error('Already flushing work.');
  473. }
  474. if (scheduledCallback !== null) {
  475. var cb = scheduledCallback;
  476. isFlushing = true;
  477. try {
  478. var hasMoreWork = true;
  479. do {
  480. hasMoreWork = cb(true, currentMockTime);
  481. } while (hasMoreWork);
  482. if (!hasMoreWork) {
  483. scheduledCallback = null;
  484. }
  485. return true;
  486. } finally {
  487. isFlushing = false;
  488. }
  489. } else {
  490. return false;
  491. }
  492. }
  493. function unstable_clearYields() {
  494. if (yieldedValues === null) {
  495. return [];
  496. }
  497. var values = yieldedValues;
  498. yieldedValues = null;
  499. return values;
  500. }
  501. function unstable_flushAll() {
  502. if (yieldedValues !== null) {
  503. throw new Error('Log is not empty. Assert on the log of yielded values before ' + 'flushing additional work.');
  504. }
  505. unstable_flushAllWithoutAsserting();
  506. if (yieldedValues !== null) {
  507. throw new Error('While flushing work, something yielded a value. Use an ' + 'assertion helper to assert on the log of yielded values, e.g. ' + 'expect(Scheduler).toFlushAndYield([...])');
  508. }
  509. }
  510. function unstable_yieldValue(value) {
  511. // eslint-disable-next-line react-internal/no-production-logging
  512. if (console.log.name === 'disabledLog' || disableYieldValue) {
  513. // If console.log has been patched, we assume we're in render
  514. // replaying and we ignore any values yielding in the second pass.
  515. return;
  516. }
  517. if (yieldedValues === null) {
  518. yieldedValues = [value];
  519. } else {
  520. yieldedValues.push(value);
  521. }
  522. }
  523. function unstable_advanceTime(ms) {
  524. // eslint-disable-next-line react-internal/no-production-logging
  525. if (console.log.name === 'disabledLog' || disableYieldValue) {
  526. // If console.log has been patched, we assume we're in render
  527. // replaying and we ignore any time advancing in the second pass.
  528. return;
  529. }
  530. currentMockTime += ms;
  531. if (scheduledTimeout !== null && timeoutTime <= currentMockTime) {
  532. scheduledTimeout(currentMockTime);
  533. timeoutTime = -1;
  534. scheduledTimeout = null;
  535. }
  536. }
  537. function requestPaint() {
  538. needsPaint = true;
  539. }
  540. var unstable_Profiling = null;
  541. exports.reset = reset;
  542. exports.unstable_IdlePriority = IdlePriority;
  543. exports.unstable_ImmediatePriority = ImmediatePriority;
  544. exports.unstable_LowPriority = LowPriority;
  545. exports.unstable_NormalPriority = NormalPriority;
  546. exports.unstable_Profiling = unstable_Profiling;
  547. exports.unstable_UserBlockingPriority = UserBlockingPriority;
  548. exports.unstable_advanceTime = unstable_advanceTime;
  549. exports.unstable_cancelCallback = unstable_cancelCallback;
  550. exports.unstable_clearYields = unstable_clearYields;
  551. exports.unstable_continueExecution = unstable_continueExecution;
  552. exports.unstable_flushAll = unstable_flushAll;
  553. exports.unstable_flushAllWithoutAsserting = unstable_flushAllWithoutAsserting;
  554. exports.unstable_flushExpired = unstable_flushExpired;
  555. exports.unstable_flushNumberOfYields = unstable_flushNumberOfYields;
  556. exports.unstable_flushUntilNextPaint = unstable_flushUntilNextPaint;
  557. exports.unstable_forceFrameRate = forceFrameRate;
  558. exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
  559. exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
  560. exports.unstable_next = unstable_next;
  561. exports.unstable_now = getCurrentTime;
  562. exports.unstable_pauseExecution = unstable_pauseExecution;
  563. exports.unstable_requestPaint = requestPaint;
  564. exports.unstable_runWithPriority = unstable_runWithPriority;
  565. exports.unstable_scheduleCallback = unstable_scheduleCallback;
  566. exports.unstable_setDisableYieldValue = setDisableYieldValue;
  567. exports.unstable_shouldYield = shouldYieldToHost;
  568. exports.unstable_wrapCallback = unstable_wrapCallback;
  569. exports.unstable_yieldValue = unstable_yieldValue;
  570. })();
  571. }