|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {PriorityLevel} from './SchedulerPriorities'; |
| 11 | + |
| 12 | +declare class TaskController { |
| 13 | + constructor(priority?: string): TaskController; |
| 14 | + signal: mixed; |
| 15 | + abort(): void; |
| 16 | +} |
| 17 | + |
| 18 | +type PostTaskPriorityLevel = 'user-blocking' | 'user-visible' | 'background'; |
| 19 | + |
| 20 | +type CallbackNode = {| |
| 21 | + _controller: TaskController, |
| 22 | +|}; |
| 23 | + |
| 24 | +import { |
| 25 | + ImmediatePriority, |
| 26 | + UserBlockingPriority, |
| 27 | + NormalPriority, |
| 28 | + LowPriority, |
| 29 | + IdlePriority, |
| 30 | +} from './SchedulerPriorities'; |
| 31 | + |
| 32 | +export { |
| 33 | + ImmediatePriority as unstable_ImmediatePriority, |
| 34 | + UserBlockingPriority as unstable_UserBlockingPriority, |
| 35 | + NormalPriority as unstable_NormalPriority, |
| 36 | + IdlePriority as unstable_IdlePriority, |
| 37 | + LowPriority as unstable_LowPriority, |
| 38 | +}; |
| 39 | + |
| 40 | +// Capture local references to native APIs, in case a polyfill overrides them. |
| 41 | +const perf = window.performance; |
| 42 | + |
| 43 | +// Use experimental Chrome Scheduler postTask API. |
| 44 | +const scheduler = global.scheduler; |
| 45 | + |
| 46 | +const getCurrentTime = perf.now.bind(perf); |
| 47 | + |
| 48 | +export const unstable_now = getCurrentTime; |
| 49 | + |
| 50 | +// Scheduler periodically yields in case there is other work on the main |
| 51 | +// thread, like user events. By default, it yields multiple times per frame. |
| 52 | +// It does not attempt to align with frame boundaries, since most tasks don't |
| 53 | +// need to be frame aligned; for those that do, use requestAnimationFrame. |
| 54 | +const yieldInterval = 5; |
| 55 | +let deadline = 0; |
| 56 | + |
| 57 | +let currentPriorityLevel_DEPRECATED = NormalPriority; |
| 58 | + |
| 59 | +// `isInputPending` is not available. Since we have no way of knowing if |
| 60 | +// there's pending input, always yield at the end of the frame. |
| 61 | +export function unstable_shouldYield() { |
| 62 | + return getCurrentTime() >= deadline; |
| 63 | +} |
| 64 | + |
| 65 | +export function unstable_requestPaint() { |
| 66 | + // Since we yield every frame regardless, `requestPaint` has no effect. |
| 67 | +} |
| 68 | + |
| 69 | +type SchedulerCallback<T> = ( |
| 70 | + didTimeout_DEPRECATED: boolean, |
| 71 | +) => |
| 72 | + | T |
| 73 | + // May return a continuation |
| 74 | + | SchedulerCallback<T>; |
| 75 | + |
| 76 | +export function unstable_scheduleCallback<T>( |
| 77 | + priorityLevel: PriorityLevel, |
| 78 | + callback: SchedulerCallback<T>, |
| 79 | + options?: {delay?: number}, |
| 80 | +): CallbackNode { |
| 81 | + let postTaskPriority; |
| 82 | + switch (priorityLevel) { |
| 83 | + case ImmediatePriority: |
| 84 | + case UserBlockingPriority: |
| 85 | + postTaskPriority = 'user-blocking'; |
| 86 | + break; |
| 87 | + case LowPriority: |
| 88 | + case NormalPriority: |
| 89 | + postTaskPriority = 'user-visible'; |
| 90 | + break; |
| 91 | + case IdlePriority: |
| 92 | + postTaskPriority = 'background'; |
| 93 | + break; |
| 94 | + default: |
| 95 | + postTaskPriority = 'user-visible'; |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + const controller = new TaskController(); |
| 100 | + const postTaskOptions = { |
| 101 | + priority: postTaskPriority, |
| 102 | + delay: typeof options === 'object' && options !== null ? options.delay : 0, |
| 103 | + signal: controller.signal, |
| 104 | + }; |
| 105 | + |
| 106 | + const node = { |
| 107 | + _controller: controller, |
| 108 | + }; |
| 109 | + |
| 110 | + scheduler |
| 111 | + .postTask( |
| 112 | + runTask.bind(null, priorityLevel, postTaskPriority, node, callback), |
| 113 | + postTaskOptions, |
| 114 | + ) |
| 115 | + .catch(handlePostTaskError); |
| 116 | + |
| 117 | + return node; |
| 118 | +} |
| 119 | + |
| 120 | +function runTask<T>( |
| 121 | + priorityLevel: PriorityLevel, |
| 122 | + postTaskPriority: PostTaskPriorityLevel, |
| 123 | + node: CallbackNode, |
| 124 | + callback: SchedulerCallback<T>, |
| 125 | +) { |
| 126 | + deadline = getCurrentTime() + yieldInterval; |
| 127 | + try { |
| 128 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 129 | + const didTimeout_DEPRECATED = false; |
| 130 | + const result = callback(didTimeout_DEPRECATED); |
| 131 | + if (typeof result === 'function') { |
| 132 | + // Assume this is a continuation |
| 133 | + const continuation: SchedulerCallback<T> = (result: any); |
| 134 | + const continuationController = new TaskController(); |
| 135 | + const continuationOptions = { |
| 136 | + priority: postTaskPriority, |
| 137 | + signal: continuationController.signal, |
| 138 | + }; |
| 139 | + // Update the original callback node's controller, since even though we're |
| 140 | + // posting a new task, conceptually it's the same one. |
| 141 | + node._controller = continuationController; |
| 142 | + scheduler |
| 143 | + .postTask( |
| 144 | + runTask.bind( |
| 145 | + null, |
| 146 | + priorityLevel, |
| 147 | + postTaskPriority, |
| 148 | + node, |
| 149 | + continuation, |
| 150 | + ), |
| 151 | + continuationOptions, |
| 152 | + ) |
| 153 | + .catch(handlePostTaskError); |
| 154 | + } |
| 155 | + } finally { |
| 156 | + currentPriorityLevel_DEPRECATED = NormalPriority; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function handlePostTaskError(error) { |
| 161 | + // This error is either a user error thrown by a callback, or an AbortError |
| 162 | + // as a result of a cancellation. |
| 163 | + // |
| 164 | + // User errors trigger a global `error` event even if we don't rethrow them. |
| 165 | + // In fact, if we do rethrow them, they'll get reported to the console twice. |
| 166 | + // I'm not entirely sure the current `postTask` spec makes sense here. If I |
| 167 | + // catch a `postTask` call, it shouldn't trigger a global error. |
| 168 | + // |
| 169 | + // Abort errors are an implementation detail. We don't expose the |
| 170 | + // TaskController to the user, nor do we expose the promise that is returned |
| 171 | + // from `postTask`. So we shouldn't rethrow those, either, since there's no |
| 172 | + // way to handle them. (If we did return the promise to the user, then it |
| 173 | + // should be up to them to handle the AbortError.) |
| 174 | + // |
| 175 | + // In either case, we can suppress the error, barring changes to the spec |
| 176 | + // or the Scheduler API. |
| 177 | +} |
| 178 | + |
| 179 | +export function unstable_cancelCallback(node: CallbackNode) { |
| 180 | + const controller = node._controller; |
| 181 | + controller.abort(); |
| 182 | +} |
| 183 | + |
| 184 | +export function unstable_runWithPriority<T>( |
| 185 | + priorityLevel: PriorityLevel, |
| 186 | + callback: () => T, |
| 187 | +): T { |
| 188 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 189 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 190 | + try { |
| 191 | + return callback(); |
| 192 | + } finally { |
| 193 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +export function unstable_getCurrentPriorityLevel() { |
| 198 | + return currentPriorityLevel_DEPRECATED; |
| 199 | +} |
| 200 | + |
| 201 | +export function unstable_next<T>(callback: () => T): T { |
| 202 | + let priorityLevel; |
| 203 | + switch (currentPriorityLevel_DEPRECATED) { |
| 204 | + case ImmediatePriority: |
| 205 | + case UserBlockingPriority: |
| 206 | + case NormalPriority: |
| 207 | + // Shift down to normal priority |
| 208 | + priorityLevel = NormalPriority; |
| 209 | + break; |
| 210 | + default: |
| 211 | + // Anything lower than normal priority should remain at the current level. |
| 212 | + priorityLevel = currentPriorityLevel_DEPRECATED; |
| 213 | + break; |
| 214 | + } |
| 215 | + |
| 216 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 217 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 218 | + try { |
| 219 | + return callback(); |
| 220 | + } finally { |
| 221 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +export function unstable_wrapCallback<T>(callback: () => T): () => T { |
| 226 | + const parentPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 227 | + return () => { |
| 228 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 229 | + currentPriorityLevel_DEPRECATED = parentPriorityLevel; |
| 230 | + try { |
| 231 | + return callback(); |
| 232 | + } finally { |
| 233 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 234 | + } |
| 235 | + }; |
| 236 | +} |
| 237 | + |
| 238 | +export function unstable_forceFrameRate() {} |
| 239 | + |
| 240 | +export function unstable_pauseExecution() {} |
| 241 | + |
| 242 | +export function unstable_continueExecution() {} |
| 243 | + |
| 244 | +export function unstable_getFirstCallbackNode() { |
| 245 | + return null; |
| 246 | +} |
| 247 | + |
| 248 | +// Currently no profiling build |
| 249 | +export const unstable_Profiling = null; |
0 commit comments