UNPKG

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