scheduler-unstable_mock.development.js 16 KB

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