|
| 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 CallbackNode = { |
| 19 | + _controller: TaskController, |
| 20 | +}; |
| 21 | + |
| 22 | +import { |
| 23 | + ImmediatePriority, |
| 24 | + UserBlockingPriority, |
| 25 | + NormalPriority, |
| 26 | + LowPriority, |
| 27 | + IdlePriority, |
| 28 | +} from './SchedulerPriorities'; |
| 29 | + |
| 30 | +export { |
| 31 | + ImmediatePriority as unstable_ImmediatePriority, |
| 32 | + UserBlockingPriority as unstable_UserBlockingPriority, |
| 33 | + NormalPriority as unstable_NormalPriority, |
| 34 | + IdlePriority as unstable_IdlePriority, |
| 35 | + LowPriority as unstable_LowPriority, |
| 36 | +}; |
| 37 | + |
| 38 | +// Capture local references to native APIs, in case a polyfill overrides them. |
| 39 | +const perf = window.performance; |
| 40 | + |
| 41 | +// Use experimental Chrome Scheduler postTask API. |
| 42 | +const scheduler = global.scheduler; |
| 43 | + |
| 44 | +const getCurrentTime = perf.now.bind(perf); |
| 45 | + |
| 46 | +export const unstable_now = getCurrentTime; |
| 47 | + |
| 48 | +// Scheduler periodically yields in case there is other work on the main |
| 49 | +// thread, like user events. By default, it yields multiple times per frame. |
| 50 | +// It does not attempt to align with frame boundaries, since most tasks don't |
| 51 | +// need to be frame aligned; for those that do, use requestAnimationFrame. |
| 52 | +const yieldInterval = 5; |
| 53 | +let deadline = 0; |
| 54 | + |
| 55 | +let currentPriorityLevel_DEPRECATED = NormalPriority; |
| 56 | + |
| 57 | +// `isInputPending` is not available. Since we have no way of knowing if |
| 58 | +// there's pending input, always yield at the end of the frame. |
| 59 | +export function unstable_shouldYield() { |
| 60 | + return getCurrentTime() >= deadline; |
| 61 | +} |
| 62 | + |
| 63 | +export function unstable_requestPaint() { |
| 64 | + // Since we yield every frame regardless, `requestPaint` has no effect. |
| 65 | +} |
| 66 | + |
| 67 | +type SchedulerCallback<T> = ( |
| 68 | + didTimeout_DEPRECATED: boolean, |
| 69 | +) => |
| 70 | + | T |
| 71 | + // May return a continuation |
| 72 | + | SchedulerCallback<T>; |
| 73 | + |
| 74 | +export function unstable_scheduleCallback<T>( |
| 75 | + priorityLevel: PriorityLevel, |
| 76 | + callback: SchedulerCallback<T>, |
| 77 | + options?: {delay?: number}, |
| 78 | +): CallbackNode { |
| 79 | + let postTaskPriority; |
| 80 | + switch (priorityLevel) { |
| 81 | + case ImmediatePriority: |
| 82 | + case UserBlockingPriority: |
| 83 | + postTaskPriority = 'user-blocking'; |
| 84 | + break; |
| 85 | + case LowPriority: |
| 86 | + case NormalPriority: |
| 87 | + postTaskPriority = 'user-visible'; |
| 88 | + break; |
| 89 | + case IdlePriority: |
| 90 | + postTaskPriority = 'background'; |
| 91 | + break; |
| 92 | + default: |
| 93 | + postTaskPriority = 'user-visible'; |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + const controller = new TaskController(); |
| 98 | + const postTaskOptions = { |
| 99 | + priority: postTaskPriority, |
| 100 | + delay: typeof options === 'object' && options !== null ? options.delay : 0, |
| 101 | + signal: controller.signal, |
| 102 | + }; |
| 103 | + |
| 104 | + scheduler.postTask(() => { |
| 105 | + deadline = getCurrentTime() + yieldInterval; |
| 106 | + try { |
| 107 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 108 | + const didTimeout_DEPRECATED = false; |
| 109 | + const result = callback(didTimeout_DEPRECATED); |
| 110 | + if (typeof result === 'function') { |
| 111 | + // Assume this is a continuation |
| 112 | + const continuation: SchedulerCallback<T> = (result: any); |
| 113 | + unstable_scheduleCallback(priorityLevel, continuation); |
| 114 | + } |
| 115 | + } finally { |
| 116 | + currentPriorityLevel_DEPRECATED = NormalPriority; |
| 117 | + } |
| 118 | + }, postTaskOptions); |
| 119 | + |
| 120 | + return { |
| 121 | + _controller: controller, |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +export function unstable_cancelCallback(node: CallbackNode) { |
| 126 | + const controller = node._controller; |
| 127 | + controller.abort(); |
| 128 | +} |
| 129 | + |
| 130 | +export function unstable_runWithPriority<T>( |
| 131 | + priorityLevel: PriorityLevel, |
| 132 | + callback: () => T, |
| 133 | +): T { |
| 134 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 135 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 136 | + try { |
| 137 | + return callback(); |
| 138 | + } finally { |
| 139 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +export function unstable_getCurrentPriorityLevel() { |
| 144 | + return currentPriorityLevel_DEPRECATED; |
| 145 | +} |
| 146 | + |
| 147 | +export function unstable_next<T>(callback: () => T): T { |
| 148 | + let priorityLevel; |
| 149 | + switch (currentPriorityLevel_DEPRECATED) { |
| 150 | + case ImmediatePriority: |
| 151 | + case UserBlockingPriority: |
| 152 | + case NormalPriority: |
| 153 | + // Shift down to normal priority |
| 154 | + priorityLevel = NormalPriority; |
| 155 | + break; |
| 156 | + default: |
| 157 | + // Anything lower than normal priority should remain at the current level. |
| 158 | + priorityLevel = currentPriorityLevel_DEPRECATED; |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 163 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 164 | + try { |
| 165 | + return callback(); |
| 166 | + } finally { |
| 167 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +export function unstable_wrapCallback<T>(callback: () => T): () => T { |
| 172 | + const parentPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 173 | + return () => { |
| 174 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 175 | + currentPriorityLevel_DEPRECATED = parentPriorityLevel; |
| 176 | + try { |
| 177 | + return callback(); |
| 178 | + } finally { |
| 179 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 180 | + } |
| 181 | + }; |
| 182 | +} |
| 183 | + |
| 184 | +export function unstable_forceFrameRate() {} |
| 185 | + |
| 186 | +export function unstable_pauseExecution() {} |
| 187 | + |
| 188 | +export function unstable_continueExecution() {} |
| 189 | + |
| 190 | +export function unstable_getFirstCallbackNode() { |
| 191 | + return null; |
| 192 | +} |
| 193 | + |
| 194 | +// Currently no profiling build |
| 195 | +export const unstable_Profiling = null; |
0 commit comments