scheduler-unstable_mock.development.js 18 KB

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