scheduler-unstable_mock.development.js 18 KB

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