|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 | +import { ɵglobal } from '@angular/core'; |
| 10 | +import { PriorityLevel } from './schedulerPriorities'; |
| 11 | + |
| 12 | +declare class TaskController { |
| 13 | + // @ts-ignore |
| 14 | + constructor(options?: { priority?: string }): TaskController; |
| 15 | + signal: unknown; |
| 16 | + abort(): void; |
| 17 | +} |
| 18 | + |
| 19 | +type PostTaskPriorityLevel = 'user-blocking' | 'user-visible' | 'background'; |
| 20 | + |
| 21 | +type CallbackNode = { |
| 22 | + _controller: TaskController; |
| 23 | +}; |
| 24 | + |
| 25 | +// Capture local references to native APIs, in case a polyfill overrides them. |
| 26 | +const perf = window.performance; |
| 27 | +const setTimeout = window.setTimeout; |
| 28 | + |
| 29 | +// Use experimental Chrome Scheduler postTask API. |
| 30 | +const scheduler = ɵglobal.scheduler; |
| 31 | + |
| 32 | +const getCurrentTime: () => DOMHighResTimeStamp = perf.now.bind(perf); |
| 33 | + |
| 34 | +export const now = getCurrentTime; |
| 35 | + |
| 36 | +// Scheduler periodically yields in case there is other work on the main |
| 37 | +// thread, like user events. By default, it yields multiple times per frame. |
| 38 | +// It does not attempt to align with frame boundaries, since most tasks don't |
| 39 | +// need to be frame aligned; for those that do, use requestAnimationFrame. |
| 40 | +const yieldInterval = 5; |
| 41 | +let deadline = 0; |
| 42 | + |
| 43 | +let currentPriorityLevel_DEPRECATED = PriorityLevel.NormalPriority; |
| 44 | + |
| 45 | +// Always yield at the end of the frame. |
| 46 | +export function shouldYield(): boolean { |
| 47 | + return getCurrentTime() >= deadline; |
| 48 | +} |
| 49 | + |
| 50 | +export function requestPaint() { |
| 51 | + // Since we yield every frame regardless, `requestPaint` has no effect. |
| 52 | +} |
| 53 | + |
| 54 | +type SchedulerCallback<T> = (didTimeout_DEPRECATED: boolean) => |
| 55 | + | T |
| 56 | + // May return a continuation |
| 57 | + | SchedulerCallback<T>; |
| 58 | + |
| 59 | +export function scheduleCallback<T>( |
| 60 | + priorityLevel: PriorityLevel, |
| 61 | + callback: SchedulerCallback<T>, |
| 62 | + options?: { delay?: number }, |
| 63 | +): CallbackNode { |
| 64 | + let postTaskPriority; |
| 65 | + switch (priorityLevel) { |
| 66 | + case PriorityLevel.ImmediatePriority: |
| 67 | + case PriorityLevel.UserBlockingPriority: |
| 68 | + postTaskPriority = 'user-blocking'; |
| 69 | + break; |
| 70 | + case PriorityLevel.LowPriority: |
| 71 | + case PriorityLevel.NormalPriority: |
| 72 | + postTaskPriority = 'user-visible'; |
| 73 | + break; |
| 74 | + case PriorityLevel.IdlePriority: |
| 75 | + postTaskPriority = 'background'; |
| 76 | + break; |
| 77 | + default: |
| 78 | + postTaskPriority = 'user-visible'; |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + const controller = new TaskController({ priority: postTaskPriority }); |
| 83 | + const postTaskOptions = { |
| 84 | + delay: typeof options === 'object' && options !== null ? options.delay : 0, |
| 85 | + signal: controller.signal, |
| 86 | + }; |
| 87 | + |
| 88 | + const node = { |
| 89 | + _controller: controller, |
| 90 | + }; |
| 91 | + |
| 92 | + scheduler |
| 93 | + .postTask( |
| 94 | + runTask.bind(null, priorityLevel, postTaskPriority, node, callback), |
| 95 | + postTaskOptions, |
| 96 | + ) |
| 97 | + .catch(handleAbortError); |
| 98 | + |
| 99 | + return node; |
| 100 | +} |
| 101 | + |
| 102 | +function runTask<T>( |
| 103 | + priorityLevel: PriorityLevel, |
| 104 | + postTaskPriority: PostTaskPriorityLevel, |
| 105 | + node: CallbackNode, |
| 106 | + callback: SchedulerCallback<T>, |
| 107 | +) { |
| 108 | + deadline = getCurrentTime() + yieldInterval; |
| 109 | + try { |
| 110 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 111 | + const didTimeout_DEPRECATED = false; |
| 112 | + const result = callback(didTimeout_DEPRECATED); |
| 113 | + if (typeof result === 'function') { |
| 114 | + // Assume this is a continuation |
| 115 | + const continuation: SchedulerCallback<T> = (result: any) => result; |
| 116 | + const continuationOptions = { |
| 117 | + signal: node._controller.signal, |
| 118 | + }; |
| 119 | + |
| 120 | + const nextTask = runTask.bind( |
| 121 | + null, |
| 122 | + priorityLevel, |
| 123 | + postTaskPriority, |
| 124 | + node, |
| 125 | + continuation, |
| 126 | + ); |
| 127 | + |
| 128 | + if (scheduler.yield !== undefined) { |
| 129 | + scheduler |
| 130 | + .yield(continuationOptions) |
| 131 | + .then(nextTask) |
| 132 | + .catch(handleAbortError); |
| 133 | + } else { |
| 134 | + scheduler |
| 135 | + .postTask(nextTask, continuationOptions) |
| 136 | + .catch(handleAbortError); |
| 137 | + } |
| 138 | + } |
| 139 | + } catch (error) { |
| 140 | + // We're inside a `postTask` promise. If we don't handle this error, then it |
| 141 | + // will trigger an "Unhandled promise rejection" error. We don't want that, |
| 142 | + // but we do want the default error reporting behavior that normal |
| 143 | + // (non-Promise) tasks get for unhandled errors. |
| 144 | + // |
| 145 | + // So we'll re-throw the error inside a regular browser task. |
| 146 | + setTimeout(() => { |
| 147 | + throw error; |
| 148 | + }); |
| 149 | + } finally { |
| 150 | + currentPriorityLevel_DEPRECATED = PriorityLevel.NormalPriority; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function handleAbortError(error: any) { |
| 155 | + // Abort errors are an implementation detail. We don't expose the |
| 156 | + // TaskController to the user, nor do we expose the promise that is returned |
| 157 | + // from `postTask`. So we should suppress them, since there's no way for the |
| 158 | + // user to handle them. |
| 159 | +} |
| 160 | + |
| 161 | +export function cancelCallback(node: CallbackNode) { |
| 162 | + const controller = node._controller; |
| 163 | + controller.abort(); |
| 164 | +} |
| 165 | + |
| 166 | +export function runWithPriority<T>( |
| 167 | + priorityLevel: PriorityLevel, |
| 168 | + callback: () => T, |
| 169 | +): T { |
| 170 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 171 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 172 | + try { |
| 173 | + return callback(); |
| 174 | + } finally { |
| 175 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +export function getCurrentPriorityLevel(): PriorityLevel { |
| 180 | + return currentPriorityLevel_DEPRECATED; |
| 181 | +} |
| 182 | + |
| 183 | +export function next<T>(callback: () => T): T { |
| 184 | + let priorityLevel; |
| 185 | + switch (currentPriorityLevel_DEPRECATED) { |
| 186 | + case PriorityLevel.ImmediatePriority: |
| 187 | + case PriorityLevel.UserBlockingPriority: |
| 188 | + case PriorityLevel.NormalPriority: |
| 189 | + // Shift down to normal priority |
| 190 | + priorityLevel = PriorityLevel.NormalPriority; |
| 191 | + break; |
| 192 | + default: |
| 193 | + // Anything lower than normal priority should remain at the current level. |
| 194 | + priorityLevel = currentPriorityLevel_DEPRECATED; |
| 195 | + break; |
| 196 | + } |
| 197 | + |
| 198 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 199 | + currentPriorityLevel_DEPRECATED = priorityLevel; |
| 200 | + try { |
| 201 | + return callback(); |
| 202 | + } finally { |
| 203 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +export function wrapCallback<T>(callback: () => T): () => T { |
| 208 | + const parentPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 209 | + return () => { |
| 210 | + const previousPriorityLevel = currentPriorityLevel_DEPRECATED; |
| 211 | + currentPriorityLevel_DEPRECATED = parentPriorityLevel; |
| 212 | + try { |
| 213 | + return callback(); |
| 214 | + } finally { |
| 215 | + currentPriorityLevel_DEPRECATED = previousPriorityLevel; |
| 216 | + } |
| 217 | + }; |
| 218 | +} |
| 219 | + |
| 220 | +export function forceFrameRate() {} |
0 commit comments