scheduler-unstable_post_task.development.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /** @license React v0.20.2
  2. * scheduler-unstable_post_task.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. // TODO: Use symbols?
  14. var ImmediatePriority = 1;
  15. var UserBlockingPriority = 2;
  16. var NormalPriority = 3;
  17. var LowPriority = 4;
  18. var IdlePriority = 5;
  19. var perf = window.performance;
  20. var setTimeout = window.setTimeout; // Use experimental Chrome Scheduler postTask API.
  21. var scheduler = global.scheduler;
  22. var getCurrentTime = perf.now.bind(perf);
  23. var unstable_now = getCurrentTime; // Scheduler periodically yields in case there is other work on the main
  24. // thread, like user events. By default, it yields multiple times per frame.
  25. // It does not attempt to align with frame boundaries, since most tasks don't
  26. // need to be frame aligned; for those that do, use requestAnimationFrame.
  27. var yieldInterval = 5;
  28. var deadline = 0;
  29. var currentPriorityLevel_DEPRECATED = NormalPriority; // `isInputPending` is not available. Since we have no way of knowing if
  30. // there's pending input, always yield at the end of the frame.
  31. function unstable_shouldYield() {
  32. return getCurrentTime() >= deadline;
  33. }
  34. function unstable_requestPaint() {// Since we yield every frame regardless, `requestPaint` has no effect.
  35. }
  36. function unstable_scheduleCallback(priorityLevel, callback, options) {
  37. var postTaskPriority;
  38. switch (priorityLevel) {
  39. case ImmediatePriority:
  40. case UserBlockingPriority:
  41. postTaskPriority = 'user-blocking';
  42. break;
  43. case LowPriority:
  44. case NormalPriority:
  45. postTaskPriority = 'user-visible';
  46. break;
  47. case IdlePriority:
  48. postTaskPriority = 'background';
  49. break;
  50. default:
  51. postTaskPriority = 'user-visible';
  52. break;
  53. }
  54. var controller = new TaskController();
  55. var postTaskOptions = {
  56. priority: postTaskPriority,
  57. delay: typeof options === 'object' && options !== null ? options.delay : 0,
  58. signal: controller.signal
  59. };
  60. var node = {
  61. _controller: controller
  62. };
  63. scheduler.postTask(runTask.bind(null, priorityLevel, postTaskPriority, node, callback), postTaskOptions).catch(handleAbortError);
  64. return node;
  65. }
  66. function runTask(priorityLevel, postTaskPriority, node, callback) {
  67. deadline = getCurrentTime() + yieldInterval;
  68. try {
  69. currentPriorityLevel_DEPRECATED = priorityLevel;
  70. var _didTimeout_DEPRECATED = false;
  71. var result = callback(_didTimeout_DEPRECATED);
  72. if (typeof result === 'function') {
  73. // Assume this is a continuation
  74. var continuation = result;
  75. var continuationController = new TaskController();
  76. var continuationOptions = {
  77. priority: postTaskPriority,
  78. signal: continuationController.signal
  79. }; // Update the original callback node's controller, since even though we're
  80. // posting a new task, conceptually it's the same one.
  81. node._controller = continuationController;
  82. scheduler.postTask(runTask.bind(null, priorityLevel, postTaskPriority, node, continuation), continuationOptions).catch(handleAbortError);
  83. }
  84. } catch (error) {
  85. // We're inside a `postTask` promise. If we don't handle this error, then it
  86. // will trigger an "Unhandled promise rejection" error. We don't want that,
  87. // but we do want the default error reporting behavior that normal
  88. // (non-Promise) tasks get for unhandled errors.
  89. //
  90. // So we'll re-throw the error inside a regular browser task.
  91. setTimeout(function () {
  92. throw error;
  93. });
  94. } finally {
  95. currentPriorityLevel_DEPRECATED = NormalPriority;
  96. }
  97. }
  98. function handleAbortError(error) {// Abort errors are an implementation detail. We don't expose the
  99. // TaskController to the user, nor do we expose the promise that is returned
  100. // from `postTask`. So we should suppress them, since there's no way for the
  101. // user to handle them.
  102. }
  103. function unstable_cancelCallback(node) {
  104. var controller = node._controller;
  105. controller.abort();
  106. }
  107. function unstable_runWithPriority(priorityLevel, callback) {
  108. var previousPriorityLevel = currentPriorityLevel_DEPRECATED;
  109. currentPriorityLevel_DEPRECATED = priorityLevel;
  110. try {
  111. return callback();
  112. } finally {
  113. currentPriorityLevel_DEPRECATED = previousPriorityLevel;
  114. }
  115. }
  116. function unstable_getCurrentPriorityLevel() {
  117. return currentPriorityLevel_DEPRECATED;
  118. }
  119. function unstable_next(callback) {
  120. var priorityLevel;
  121. switch (currentPriorityLevel_DEPRECATED) {
  122. case ImmediatePriority:
  123. case UserBlockingPriority:
  124. case NormalPriority:
  125. // Shift down to normal priority
  126. priorityLevel = NormalPriority;
  127. break;
  128. default:
  129. // Anything lower than normal priority should remain at the current level.
  130. priorityLevel = currentPriorityLevel_DEPRECATED;
  131. break;
  132. }
  133. var previousPriorityLevel = currentPriorityLevel_DEPRECATED;
  134. currentPriorityLevel_DEPRECATED = priorityLevel;
  135. try {
  136. return callback();
  137. } finally {
  138. currentPriorityLevel_DEPRECATED = previousPriorityLevel;
  139. }
  140. }
  141. function unstable_wrapCallback(callback) {
  142. var parentPriorityLevel = currentPriorityLevel_DEPRECATED;
  143. return function () {
  144. var previousPriorityLevel = currentPriorityLevel_DEPRECATED;
  145. currentPriorityLevel_DEPRECATED = parentPriorityLevel;
  146. try {
  147. return callback();
  148. } finally {
  149. currentPriorityLevel_DEPRECATED = previousPriorityLevel;
  150. }
  151. };
  152. }
  153. function unstable_forceFrameRate() {}
  154. function unstable_pauseExecution() {}
  155. function unstable_continueExecution() {}
  156. function unstable_getFirstCallbackNode() {
  157. return null;
  158. } // Currently no profiling build
  159. var unstable_Profiling = null;
  160. exports.unstable_IdlePriority = IdlePriority;
  161. exports.unstable_ImmediatePriority = ImmediatePriority;
  162. exports.unstable_LowPriority = LowPriority;
  163. exports.unstable_NormalPriority = NormalPriority;
  164. exports.unstable_Profiling = unstable_Profiling;
  165. exports.unstable_UserBlockingPriority = UserBlockingPriority;
  166. exports.unstable_cancelCallback = unstable_cancelCallback;
  167. exports.unstable_continueExecution = unstable_continueExecution;
  168. exports.unstable_forceFrameRate = unstable_forceFrameRate;
  169. exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
  170. exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
  171. exports.unstable_next = unstable_next;
  172. exports.unstable_now = unstable_now;
  173. exports.unstable_pauseExecution = unstable_pauseExecution;
  174. exports.unstable_requestPaint = unstable_requestPaint;
  175. exports.unstable_runWithPriority = unstable_runWithPriority;
  176. exports.unstable_scheduleCallback = unstable_scheduleCallback;
  177. exports.unstable_shouldYield = unstable_shouldYield;
  178. exports.unstable_wrapCallback = unstable_wrapCallback;
  179. })();
  180. }