scheduler.development.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /** @license React v0.19.1
  2. * scheduler.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 = true;
  15. var requestHostCallback;
  16. var requestHostTimeout;
  17. var cancelHostTimeout;
  18. var shouldYieldToHost;
  19. var requestPaint;
  20. if ( // If Scheduler runs in a non-DOM environment, it falls back to a naive
  21. // implementation using setTimeout.
  22. typeof window === 'undefined' || // Check if MessageChannel is supported, too.
  23. typeof MessageChannel !== 'function') {
  24. // If this accidentally gets imported in a non-browser environment, e.g. JavaScriptCore,
  25. // fallback to a naive implementation.
  26. var _callback = null;
  27. var _timeoutID = null;
  28. var _flushCallback = function () {
  29. if (_callback !== null) {
  30. try {
  31. var currentTime = exports.unstable_now();
  32. var hasRemainingTime = true;
  33. _callback(hasRemainingTime, currentTime);
  34. _callback = null;
  35. } catch (e) {
  36. setTimeout(_flushCallback, 0);
  37. throw e;
  38. }
  39. }
  40. };
  41. var initialTime = Date.now();
  42. exports.unstable_now = function () {
  43. return Date.now() - initialTime;
  44. };
  45. requestHostCallback = function (cb) {
  46. if (_callback !== null) {
  47. // Protect against re-entrancy.
  48. setTimeout(requestHostCallback, 0, cb);
  49. } else {
  50. _callback = cb;
  51. setTimeout(_flushCallback, 0);
  52. }
  53. };
  54. requestHostTimeout = function (cb, ms) {
  55. _timeoutID = setTimeout(cb, ms);
  56. };
  57. cancelHostTimeout = function () {
  58. clearTimeout(_timeoutID);
  59. };
  60. shouldYieldToHost = function () {
  61. return false;
  62. };
  63. requestPaint = exports.unstable_forceFrameRate = function () {};
  64. } else {
  65. // Capture local references to native APIs, in case a polyfill overrides them.
  66. var performance = window.performance;
  67. var _Date = window.Date;
  68. var _setTimeout = window.setTimeout;
  69. var _clearTimeout = window.clearTimeout;
  70. if (typeof console !== 'undefined') {
  71. // TODO: Scheduler no longer requires these methods to be polyfilled. But
  72. // maybe we want to continue warning if they don't exist, to preserve the
  73. // option to rely on it in the future?
  74. var requestAnimationFrame = window.requestAnimationFrame;
  75. var cancelAnimationFrame = window.cancelAnimationFrame; // TODO: Remove fb.me link
  76. if (typeof requestAnimationFrame !== 'function') {
  77. // Using console['error'] to evade Babel and ESLint
  78. console['error']("This browser doesn't support requestAnimationFrame. " + 'Make sure that you load a ' + 'polyfill in older browsers. https://fb.me/react-polyfills');
  79. }
  80. if (typeof cancelAnimationFrame !== 'function') {
  81. // Using console['error'] to evade Babel and ESLint
  82. console['error']("This browser doesn't support cancelAnimationFrame. " + 'Make sure that you load a ' + 'polyfill in older browsers. https://fb.me/react-polyfills');
  83. }
  84. }
  85. if (typeof performance === 'object' && typeof performance.now === 'function') {
  86. exports.unstable_now = function () {
  87. return performance.now();
  88. };
  89. } else {
  90. var _initialTime = _Date.now();
  91. exports.unstable_now = function () {
  92. return _Date.now() - _initialTime;
  93. };
  94. }
  95. var isMessageLoopRunning = false;
  96. var scheduledHostCallback = null;
  97. var taskTimeoutID = -1; // Scheduler periodically yields in case there is other work on the main
  98. // thread, like user events. By default, it yields multiple times per frame.
  99. // It does not attempt to align with frame boundaries, since most tasks don't
  100. // need to be frame aligned; for those that do, use requestAnimationFrame.
  101. var yieldInterval = 5;
  102. var deadline = 0; // TODO: Make this configurable
  103. {
  104. // `isInputPending` is not available. Since we have no way of knowing if
  105. // there's pending input, always yield at the end of the frame.
  106. shouldYieldToHost = function () {
  107. return exports.unstable_now() >= deadline;
  108. }; // Since we yield every frame regardless, `requestPaint` has no effect.
  109. requestPaint = function () {};
  110. }
  111. exports.unstable_forceFrameRate = function (fps) {
  112. if (fps < 0 || fps > 125) {
  113. // Using console['error'] to evade Babel and ESLint
  114. console['error']('forceFrameRate takes a positive int between 0 and 125, ' + 'forcing framerates higher than 125 fps is not unsupported');
  115. return;
  116. }
  117. if (fps > 0) {
  118. yieldInterval = Math.floor(1000 / fps);
  119. } else {
  120. // reset the framerate
  121. yieldInterval = 5;
  122. }
  123. };
  124. var performWorkUntilDeadline = function () {
  125. if (scheduledHostCallback !== null) {
  126. var currentTime = exports.unstable_now(); // Yield after `yieldInterval` ms, regardless of where we are in the vsync
  127. // cycle. This means there's always time remaining at the beginning of
  128. // the message event.
  129. deadline = currentTime + yieldInterval;
  130. var hasTimeRemaining = true;
  131. try {
  132. var hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime);
  133. if (!hasMoreWork) {
  134. isMessageLoopRunning = false;
  135. scheduledHostCallback = null;
  136. } else {
  137. // If there's more work, schedule the next message event at the end
  138. // of the preceding one.
  139. port.postMessage(null);
  140. }
  141. } catch (error) {
  142. // If a scheduler task throws, exit the current browser task so the
  143. // error can be observed.
  144. port.postMessage(null);
  145. throw error;
  146. }
  147. } else {
  148. isMessageLoopRunning = false;
  149. } // Yielding to the browser will give it a chance to paint, so we can
  150. };
  151. var channel = new MessageChannel();
  152. var port = channel.port2;
  153. channel.port1.onmessage = performWorkUntilDeadline;
  154. requestHostCallback = function (callback) {
  155. scheduledHostCallback = callback;
  156. if (!isMessageLoopRunning) {
  157. isMessageLoopRunning = true;
  158. port.postMessage(null);
  159. }
  160. };
  161. requestHostTimeout = function (callback, ms) {
  162. taskTimeoutID = _setTimeout(function () {
  163. callback(exports.unstable_now());
  164. }, ms);
  165. };
  166. cancelHostTimeout = function () {
  167. _clearTimeout(taskTimeoutID);
  168. taskTimeoutID = -1;
  169. };
  170. }
  171. function push(heap, node) {
  172. var index = heap.length;
  173. heap.push(node);
  174. siftUp(heap, node, index);
  175. }
  176. function peek(heap) {
  177. var first = heap[0];
  178. return first === undefined ? null : first;
  179. }
  180. function pop(heap) {
  181. var first = heap[0];
  182. if (first !== undefined) {
  183. var last = heap.pop();
  184. if (last !== first) {
  185. heap[0] = last;
  186. siftDown(heap, last, 0);
  187. }
  188. return first;
  189. } else {
  190. return null;
  191. }
  192. }
  193. function siftUp(heap, node, i) {
  194. var index = i;
  195. while (true) {
  196. var parentIndex = index - 1 >>> 1;
  197. var parent = heap[parentIndex];
  198. if (parent !== undefined && compare(parent, node) > 0) {
  199. // The parent is larger. Swap positions.
  200. heap[parentIndex] = node;
  201. heap[index] = parent;
  202. index = parentIndex;
  203. } else {
  204. // The parent is smaller. Exit.
  205. return;
  206. }
  207. }
  208. }
  209. function siftDown(heap, node, i) {
  210. var index = i;
  211. var length = heap.length;
  212. while (index < length) {
  213. var leftIndex = (index + 1) * 2 - 1;
  214. var left = heap[leftIndex];
  215. var rightIndex = leftIndex + 1;
  216. var right = heap[rightIndex]; // If the left or right node is smaller, swap with the smaller of those.
  217. if (left !== undefined && compare(left, node) < 0) {
  218. if (right !== undefined && compare(right, left) < 0) {
  219. heap[index] = right;
  220. heap[rightIndex] = node;
  221. index = rightIndex;
  222. } else {
  223. heap[index] = left;
  224. heap[leftIndex] = node;
  225. index = leftIndex;
  226. }
  227. } else if (right !== undefined && compare(right, node) < 0) {
  228. heap[index] = right;
  229. heap[rightIndex] = node;
  230. index = rightIndex;
  231. } else {
  232. // Neither child is smaller. Exit.
  233. return;
  234. }
  235. }
  236. }
  237. function compare(a, b) {
  238. // Compare sort index first, then task id.
  239. var diff = a.sortIndex - b.sortIndex;
  240. return diff !== 0 ? diff : a.id - b.id;
  241. }
  242. // TODO: Use symbols?
  243. var NoPriority = 0;
  244. var ImmediatePriority = 1;
  245. var UserBlockingPriority = 2;
  246. var NormalPriority = 3;
  247. var LowPriority = 4;
  248. var IdlePriority = 5;
  249. var runIdCounter = 0;
  250. var mainThreadIdCounter = 0;
  251. var profilingStateSize = 4;
  252. var sharedProfilingBuffer = // $FlowFixMe Flow doesn't know about SharedArrayBuffer
  253. typeof SharedArrayBuffer === 'function' ? new SharedArrayBuffer(profilingStateSize * Int32Array.BYTES_PER_ELEMENT) : // $FlowFixMe Flow doesn't know about ArrayBuffer
  254. typeof ArrayBuffer === 'function' ? new ArrayBuffer(profilingStateSize * Int32Array.BYTES_PER_ELEMENT) : null // Don't crash the init path on IE9
  255. ;
  256. var profilingState = sharedProfilingBuffer !== null ? new Int32Array(sharedProfilingBuffer) : []; // We can't read this but it helps save bytes for null checks
  257. var PRIORITY = 0;
  258. var CURRENT_TASK_ID = 1;
  259. var CURRENT_RUN_ID = 2;
  260. var QUEUE_SIZE = 3;
  261. {
  262. profilingState[PRIORITY] = NoPriority; // This is maintained with a counter, because the size of the priority queue
  263. // array might include canceled tasks.
  264. profilingState[QUEUE_SIZE] = 0;
  265. profilingState[CURRENT_TASK_ID] = 0;
  266. } // Bytes per element is 4
  267. var INITIAL_EVENT_LOG_SIZE = 131072;
  268. var MAX_EVENT_LOG_SIZE = 524288; // Equivalent to 2 megabytes
  269. var eventLogSize = 0;
  270. var eventLogBuffer = null;
  271. var eventLog = null;
  272. var eventLogIndex = 0;
  273. var TaskStartEvent = 1;
  274. var TaskCompleteEvent = 2;
  275. var TaskErrorEvent = 3;
  276. var TaskCancelEvent = 4;
  277. var TaskRunEvent = 5;
  278. var TaskYieldEvent = 6;
  279. var SchedulerSuspendEvent = 7;
  280. var SchedulerResumeEvent = 8;
  281. function logEvent(entries) {
  282. if (eventLog !== null) {
  283. var offset = eventLogIndex;
  284. eventLogIndex += entries.length;
  285. if (eventLogIndex + 1 > eventLogSize) {
  286. eventLogSize *= 2;
  287. if (eventLogSize > MAX_EVENT_LOG_SIZE) {
  288. // Using console['error'] to evade Babel and ESLint
  289. console['error']("Scheduler Profiling: Event log exceeded maximum size. Don't " + 'forget to call `stopLoggingProfilingEvents()`.');
  290. stopLoggingProfilingEvents();
  291. return;
  292. }
  293. var newEventLog = new Int32Array(eventLogSize * 4);
  294. newEventLog.set(eventLog);
  295. eventLogBuffer = newEventLog.buffer;
  296. eventLog = newEventLog;
  297. }
  298. eventLog.set(entries, offset);
  299. }
  300. }
  301. function startLoggingProfilingEvents() {
  302. eventLogSize = INITIAL_EVENT_LOG_SIZE;
  303. eventLogBuffer = new ArrayBuffer(eventLogSize * 4);
  304. eventLog = new Int32Array(eventLogBuffer);
  305. eventLogIndex = 0;
  306. }
  307. function stopLoggingProfilingEvents() {
  308. var buffer = eventLogBuffer;
  309. eventLogSize = 0;
  310. eventLogBuffer = null;
  311. eventLog = null;
  312. eventLogIndex = 0;
  313. return buffer;
  314. }
  315. function markTaskStart(task, ms) {
  316. {
  317. profilingState[QUEUE_SIZE]++;
  318. if (eventLog !== null) {
  319. // performance.now returns a float, representing milliseconds. When the
  320. // event is logged, it's coerced to an int. Convert to microseconds to
  321. // maintain extra degrees of precision.
  322. logEvent([TaskStartEvent, ms * 1000, task.id, task.priorityLevel]);
  323. }
  324. }
  325. }
  326. function markTaskCompleted(task, ms) {
  327. {
  328. profilingState[PRIORITY] = NoPriority;
  329. profilingState[CURRENT_TASK_ID] = 0;
  330. profilingState[QUEUE_SIZE]--;
  331. if (eventLog !== null) {
  332. logEvent([TaskCompleteEvent, ms * 1000, task.id]);
  333. }
  334. }
  335. }
  336. function markTaskCanceled(task, ms) {
  337. {
  338. profilingState[QUEUE_SIZE]--;
  339. if (eventLog !== null) {
  340. logEvent([TaskCancelEvent, ms * 1000, task.id]);
  341. }
  342. }
  343. }
  344. function markTaskErrored(task, ms) {
  345. {
  346. profilingState[PRIORITY] = NoPriority;
  347. profilingState[CURRENT_TASK_ID] = 0;
  348. profilingState[QUEUE_SIZE]--;
  349. if (eventLog !== null) {
  350. logEvent([TaskErrorEvent, ms * 1000, task.id]);
  351. }
  352. }
  353. }
  354. function markTaskRun(task, ms) {
  355. {
  356. runIdCounter++;
  357. profilingState[PRIORITY] = task.priorityLevel;
  358. profilingState[CURRENT_TASK_ID] = task.id;
  359. profilingState[CURRENT_RUN_ID] = runIdCounter;
  360. if (eventLog !== null) {
  361. logEvent([TaskRunEvent, ms * 1000, task.id, runIdCounter]);
  362. }
  363. }
  364. }
  365. function markTaskYield(task, ms) {
  366. {
  367. profilingState[PRIORITY] = NoPriority;
  368. profilingState[CURRENT_TASK_ID] = 0;
  369. profilingState[CURRENT_RUN_ID] = 0;
  370. if (eventLog !== null) {
  371. logEvent([TaskYieldEvent, ms * 1000, task.id, runIdCounter]);
  372. }
  373. }
  374. }
  375. function markSchedulerSuspended(ms) {
  376. {
  377. mainThreadIdCounter++;
  378. if (eventLog !== null) {
  379. logEvent([SchedulerSuspendEvent, ms * 1000, mainThreadIdCounter]);
  380. }
  381. }
  382. }
  383. function markSchedulerUnsuspended(ms) {
  384. {
  385. if (eventLog !== null) {
  386. logEvent([SchedulerResumeEvent, ms * 1000, mainThreadIdCounter]);
  387. }
  388. }
  389. }
  390. /* eslint-disable no-var */
  391. // Math.pow(2, 30) - 1
  392. // 0b111111111111111111111111111111
  393. var maxSigned31BitInt = 1073741823; // Times out immediately
  394. var IMMEDIATE_PRIORITY_TIMEOUT = -1; // Eventually times out
  395. var USER_BLOCKING_PRIORITY = 250;
  396. var NORMAL_PRIORITY_TIMEOUT = 5000;
  397. var LOW_PRIORITY_TIMEOUT = 10000; // Never times out
  398. var IDLE_PRIORITY = maxSigned31BitInt; // Tasks are stored on a min heap
  399. var taskQueue = [];
  400. var timerQueue = []; // Incrementing id counter. Used to maintain insertion order.
  401. var taskIdCounter = 1; // Pausing the scheduler is useful for debugging.
  402. var currentTask = null;
  403. var currentPriorityLevel = NormalPriority; // This is set while performing work, to prevent re-entrancy.
  404. var isPerformingWork = false;
  405. var isHostCallbackScheduled = false;
  406. var isHostTimeoutScheduled = false;
  407. function advanceTimers(currentTime) {
  408. // Check for tasks that are no longer delayed and add them to the queue.
  409. var timer = peek(timerQueue);
  410. while (timer !== null) {
  411. if (timer.callback === null) {
  412. // Timer was cancelled.
  413. pop(timerQueue);
  414. } else if (timer.startTime <= currentTime) {
  415. // Timer fired. Transfer to the task queue.
  416. pop(timerQueue);
  417. timer.sortIndex = timer.expirationTime;
  418. push(taskQueue, timer);
  419. {
  420. markTaskStart(timer, currentTime);
  421. timer.isQueued = true;
  422. }
  423. } else {
  424. // Remaining timers are pending.
  425. return;
  426. }
  427. timer = peek(timerQueue);
  428. }
  429. }
  430. function handleTimeout(currentTime) {
  431. isHostTimeoutScheduled = false;
  432. advanceTimers(currentTime);
  433. if (!isHostCallbackScheduled) {
  434. if (peek(taskQueue) !== null) {
  435. isHostCallbackScheduled = true;
  436. requestHostCallback(flushWork);
  437. } else {
  438. var firstTimer = peek(timerQueue);
  439. if (firstTimer !== null) {
  440. requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
  441. }
  442. }
  443. }
  444. }
  445. function flushWork(hasTimeRemaining, initialTime) {
  446. {
  447. markSchedulerUnsuspended(initialTime);
  448. } // We'll need a host callback the next time work is scheduled.
  449. isHostCallbackScheduled = false;
  450. if (isHostTimeoutScheduled) {
  451. // We scheduled a timeout but it's no longer needed. Cancel it.
  452. isHostTimeoutScheduled = false;
  453. cancelHostTimeout();
  454. }
  455. isPerformingWork = true;
  456. var previousPriorityLevel = currentPriorityLevel;
  457. try {
  458. if (enableProfiling) {
  459. try {
  460. return workLoop(hasTimeRemaining, initialTime);
  461. } catch (error) {
  462. if (currentTask !== null) {
  463. var currentTime = exports.unstable_now();
  464. markTaskErrored(currentTask, currentTime);
  465. currentTask.isQueued = false;
  466. }
  467. throw error;
  468. }
  469. } else {
  470. // No catch in prod codepath.
  471. return workLoop(hasTimeRemaining, initialTime);
  472. }
  473. } finally {
  474. currentTask = null;
  475. currentPriorityLevel = previousPriorityLevel;
  476. isPerformingWork = false;
  477. {
  478. var _currentTime = exports.unstable_now();
  479. markSchedulerSuspended(_currentTime);
  480. }
  481. }
  482. }
  483. function workLoop(hasTimeRemaining, initialTime) {
  484. var currentTime = initialTime;
  485. advanceTimers(currentTime);
  486. currentTask = peek(taskQueue);
  487. while (currentTask !== null && !(enableSchedulerDebugging )) {
  488. if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost())) {
  489. // This currentTask hasn't expired, and we've reached the deadline.
  490. break;
  491. }
  492. var callback = currentTask.callback;
  493. if (callback !== null) {
  494. currentTask.callback = null;
  495. currentPriorityLevel = currentTask.priorityLevel;
  496. var didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
  497. markTaskRun(currentTask, currentTime);
  498. var continuationCallback = callback(didUserCallbackTimeout);
  499. currentTime = exports.unstable_now();
  500. if (typeof continuationCallback === 'function') {
  501. currentTask.callback = continuationCallback;
  502. markTaskYield(currentTask, currentTime);
  503. } else {
  504. {
  505. markTaskCompleted(currentTask, currentTime);
  506. currentTask.isQueued = false;
  507. }
  508. if (currentTask === peek(taskQueue)) {
  509. pop(taskQueue);
  510. }
  511. }
  512. advanceTimers(currentTime);
  513. } else {
  514. pop(taskQueue);
  515. }
  516. currentTask = peek(taskQueue);
  517. } // Return whether there's additional work
  518. if (currentTask !== null) {
  519. return true;
  520. } else {
  521. var firstTimer = peek(timerQueue);
  522. if (firstTimer !== null) {
  523. requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
  524. }
  525. return false;
  526. }
  527. }
  528. function unstable_runWithPriority(priorityLevel, eventHandler) {
  529. switch (priorityLevel) {
  530. case ImmediatePriority:
  531. case UserBlockingPriority:
  532. case NormalPriority:
  533. case LowPriority:
  534. case IdlePriority:
  535. break;
  536. default:
  537. priorityLevel = NormalPriority;
  538. }
  539. var previousPriorityLevel = currentPriorityLevel;
  540. currentPriorityLevel = priorityLevel;
  541. try {
  542. return eventHandler();
  543. } finally {
  544. currentPriorityLevel = previousPriorityLevel;
  545. }
  546. }
  547. function unstable_next(eventHandler) {
  548. var priorityLevel;
  549. switch (currentPriorityLevel) {
  550. case ImmediatePriority:
  551. case UserBlockingPriority:
  552. case NormalPriority:
  553. // Shift down to normal priority
  554. priorityLevel = NormalPriority;
  555. break;
  556. default:
  557. // Anything lower than normal priority should remain at the current level.
  558. priorityLevel = currentPriorityLevel;
  559. break;
  560. }
  561. var previousPriorityLevel = currentPriorityLevel;
  562. currentPriorityLevel = priorityLevel;
  563. try {
  564. return eventHandler();
  565. } finally {
  566. currentPriorityLevel = previousPriorityLevel;
  567. }
  568. }
  569. function unstable_wrapCallback(callback) {
  570. var parentPriorityLevel = currentPriorityLevel;
  571. return function () {
  572. // This is a fork of runWithPriority, inlined for performance.
  573. var previousPriorityLevel = currentPriorityLevel;
  574. currentPriorityLevel = parentPriorityLevel;
  575. try {
  576. return callback.apply(this, arguments);
  577. } finally {
  578. currentPriorityLevel = previousPriorityLevel;
  579. }
  580. };
  581. }
  582. function timeoutForPriorityLevel(priorityLevel) {
  583. switch (priorityLevel) {
  584. case ImmediatePriority:
  585. return IMMEDIATE_PRIORITY_TIMEOUT;
  586. case UserBlockingPriority:
  587. return USER_BLOCKING_PRIORITY;
  588. case IdlePriority:
  589. return IDLE_PRIORITY;
  590. case LowPriority:
  591. return LOW_PRIORITY_TIMEOUT;
  592. case NormalPriority:
  593. default:
  594. return NORMAL_PRIORITY_TIMEOUT;
  595. }
  596. }
  597. function unstable_scheduleCallback(priorityLevel, callback, options) {
  598. var currentTime = exports.unstable_now();
  599. var startTime;
  600. var timeout;
  601. if (typeof options === 'object' && options !== null) {
  602. var delay = options.delay;
  603. if (typeof delay === 'number' && delay > 0) {
  604. startTime = currentTime + delay;
  605. } else {
  606. startTime = currentTime;
  607. }
  608. timeout = typeof options.timeout === 'number' ? options.timeout : timeoutForPriorityLevel(priorityLevel);
  609. } else {
  610. timeout = timeoutForPriorityLevel(priorityLevel);
  611. startTime = currentTime;
  612. }
  613. var expirationTime = startTime + timeout;
  614. var newTask = {
  615. id: taskIdCounter++,
  616. callback: callback,
  617. priorityLevel: priorityLevel,
  618. startTime: startTime,
  619. expirationTime: expirationTime,
  620. sortIndex: -1
  621. };
  622. {
  623. newTask.isQueued = false;
  624. }
  625. if (startTime > currentTime) {
  626. // This is a delayed task.
  627. newTask.sortIndex = startTime;
  628. push(timerQueue, newTask);
  629. if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
  630. // All tasks are delayed, and this is the task with the earliest delay.
  631. if (isHostTimeoutScheduled) {
  632. // Cancel an existing timeout.
  633. cancelHostTimeout();
  634. } else {
  635. isHostTimeoutScheduled = true;
  636. } // Schedule a timeout.
  637. requestHostTimeout(handleTimeout, startTime - currentTime);
  638. }
  639. } else {
  640. newTask.sortIndex = expirationTime;
  641. push(taskQueue, newTask);
  642. {
  643. markTaskStart(newTask, currentTime);
  644. newTask.isQueued = true;
  645. } // Schedule a host callback, if needed. If we're already performing work,
  646. // wait until the next time we yield.
  647. if (!isHostCallbackScheduled && !isPerformingWork) {
  648. isHostCallbackScheduled = true;
  649. requestHostCallback(flushWork);
  650. }
  651. }
  652. return newTask;
  653. }
  654. function unstable_pauseExecution() {
  655. }
  656. function unstable_continueExecution() {
  657. if (!isHostCallbackScheduled && !isPerformingWork) {
  658. isHostCallbackScheduled = true;
  659. requestHostCallback(flushWork);
  660. }
  661. }
  662. function unstable_getFirstCallbackNode() {
  663. return peek(taskQueue);
  664. }
  665. function unstable_cancelCallback(task) {
  666. {
  667. if (task.isQueued) {
  668. var currentTime = exports.unstable_now();
  669. markTaskCanceled(task, currentTime);
  670. task.isQueued = false;
  671. }
  672. } // Null out the callback to indicate the task has been canceled. (Can't
  673. // remove from the queue because you can't remove arbitrary nodes from an
  674. // array based heap, only the first one.)
  675. task.callback = null;
  676. }
  677. function unstable_getCurrentPriorityLevel() {
  678. return currentPriorityLevel;
  679. }
  680. function unstable_shouldYield() {
  681. var currentTime = exports.unstable_now();
  682. advanceTimers(currentTime);
  683. var firstTask = peek(taskQueue);
  684. return firstTask !== currentTask && currentTask !== null && firstTask !== null && firstTask.callback !== null && firstTask.startTime <= currentTime && firstTask.expirationTime < currentTask.expirationTime || shouldYieldToHost();
  685. }
  686. var unstable_requestPaint = requestPaint;
  687. var unstable_Profiling = {
  688. startLoggingProfilingEvents: startLoggingProfilingEvents,
  689. stopLoggingProfilingEvents: stopLoggingProfilingEvents,
  690. sharedProfilingBuffer: sharedProfilingBuffer
  691. } ;
  692. exports.unstable_IdlePriority = IdlePriority;
  693. exports.unstable_ImmediatePriority = ImmediatePriority;
  694. exports.unstable_LowPriority = LowPriority;
  695. exports.unstable_NormalPriority = NormalPriority;
  696. exports.unstable_Profiling = unstable_Profiling;
  697. exports.unstable_UserBlockingPriority = UserBlockingPriority;
  698. exports.unstable_cancelCallback = unstable_cancelCallback;
  699. exports.unstable_continueExecution = unstable_continueExecution;
  700. exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
  701. exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
  702. exports.unstable_next = unstable_next;
  703. exports.unstable_pauseExecution = unstable_pauseExecution;
  704. exports.unstable_requestPaint = unstable_requestPaint;
  705. exports.unstable_runWithPriority = unstable_runWithPriority;
  706. exports.unstable_scheduleCallback = unstable_scheduleCallback;
  707. exports.unstable_shouldYield = unstable_shouldYield;
  708. exports.unstable_wrapCallback = unstable_wrapCallback;
  709. })();
  710. }