-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP][Scheduler] Call postTask directly
This updates the experimental Scheduler postTask build to call postTask directly, instead of managing our own custom queue and work loop. We still use a deadline 5ms mechanism to implement `shouldYield`. The main thing that postTask is currently missing is the continuation feature — when yielding to the main thread, the yielding task is sent to the back of the queue, instead of maintaining its position. While this would be nice to have, even without it, postTask may be good enough to replace our userspace implementation. We'll run some tests to see. TODO: Need to update the tests.
- Loading branch information
Showing
11 changed files
with
262 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {PriorityLevel} from './SchedulerPriorities'; | ||
|
||
declare class TaskController { | ||
constructor(priority?: string): TaskController; | ||
signal: mixed; | ||
abort(): void; | ||
} | ||
|
||
type PostTaskPriorityLevel = 'user-blocking' | 'user-visible' | 'background'; | ||
type CallbackStatus = 'ok' | 'errored' | 'canceled'; | ||
|
||
type CallbackNode = { | ||
_controller: TaskController, | ||
_status: CallbackStatus, | ||
}; | ||
|
||
import { | ||
ImmediatePriority, | ||
UserBlockingPriority, | ||
NormalPriority, | ||
LowPriority, | ||
IdlePriority, | ||
} from './SchedulerPriorities'; | ||
|
||
export { | ||
ImmediatePriority as unstable_ImmediatePriority, | ||
UserBlockingPriority as unstable_UserBlockingPriority, | ||
NormalPriority as unstable_NormalPriority, | ||
IdlePriority as unstable_IdlePriority, | ||
LowPriority as unstable_LowPriority, | ||
}; | ||
|
||
// Capture local references to native APIs, in case a polyfill overrides them. | ||
const perf = window.performance; | ||
|
||
// Use experimental Chrome Scheduler postTask API. | ||
const scheduler = global.scheduler; | ||
|
||
const getCurrentTime = perf.now.bind(perf); | ||
|
||
export const unstable_now = getCurrentTime; | ||
|
||
// Scheduler periodically yields in case there is other work on the main | ||
// thread, like user events. By default, it yields multiple times per frame. | ||
// It does not attempt to align with frame boundaries, since most tasks don't | ||
// need to be frame aligned; for those that do, use requestAnimationFrame. | ||
const yieldInterval = 5; | ||
let deadline = 0; | ||
|
||
let currentPriorityLevel_DEPRECATED = NormalPriority; | ||
|
||
// `isInputPending` is not available. Since we have no way of knowing if | ||
// there's pending input, always yield at the end of the frame. | ||
export function unstable_shouldYield() { | ||
return getCurrentTime() >= deadline; | ||
} | ||
|
||
export function unstable_requestPaint() { | ||
// Since we yield every frame regardless, `requestPaint` has no effect. | ||
} | ||
|
||
type SchedulerCallback<T> = ( | ||
didTimeout_DEPRECATED: boolean, | ||
) => | ||
| T | ||
// May return a continuation | ||
| SchedulerCallback<T>; | ||
|
||
export function unstable_scheduleCallback<T>( | ||
priorityLevel: PriorityLevel, | ||
callback: SchedulerCallback<T>, | ||
options?: {delay?: number}, | ||
): CallbackNode { | ||
let postTaskPriority; | ||
switch (priorityLevel) { | ||
case ImmediatePriority: | ||
case UserBlockingPriority: | ||
postTaskPriority = 'user-blocking'; | ||
break; | ||
case LowPriority: | ||
case NormalPriority: | ||
postTaskPriority = 'user-visible'; | ||
break; | ||
case IdlePriority: | ||
postTaskPriority = 'background'; | ||
break; | ||
default: | ||
postTaskPriority = 'user-visible'; | ||
break; | ||
} | ||
|
||
const controller = new TaskController(); | ||
const postTaskOptions = { | ||
priority: postTaskPriority, | ||
delay: typeof options === 'object' && options !== null ? options.delay : 0, | ||
signal: controller.signal, | ||
}; | ||
|
||
const node = { | ||
_controller: controller, | ||
_status: 'ok', | ||
}; | ||
|
||
scheduler | ||
.postTask( | ||
runTask.bind(null, priorityLevel, postTaskPriority, node, callback), | ||
postTaskOptions, | ||
) | ||
.catch(handlePostTaskError.bind(null, node)); | ||
|
||
return node; | ||
} | ||
|
||
function runTask<T>( | ||
priorityLevel: PriorityLevel, | ||
postTaskPriority: PostTaskPriorityLevel, | ||
node: CallbackNode, | ||
callback: SchedulerCallback<T>, | ||
) { | ||
deadline = getCurrentTime() + yieldInterval; | ||
let result; | ||
try { | ||
currentPriorityLevel_DEPRECATED = priorityLevel; | ||
const didTimeout_DEPRECATED = false; | ||
result = callback(didTimeout_DEPRECATED); | ||
} catch (error) { | ||
node._status = 'errored'; | ||
throw error; | ||
} finally { | ||
currentPriorityLevel_DEPRECATED = NormalPriority; | ||
} | ||
if (typeof result === 'function') { | ||
// Assume this is a continuation | ||
const continuation: SchedulerCallback<T> = (result: any); | ||
const continuationController = new TaskController(); | ||
const continuationOptions = { | ||
priority: postTaskPriority, | ||
signal: continuationController.signal, | ||
}; | ||
// Update the original callback node's controller, since even though we're | ||
// posting a new task, conceptually it's the same one. | ||
node._controller = continuationController; | ||
scheduler | ||
.postTask( | ||
runTask.bind(null, priorityLevel, postTaskPriority, node, continuation), | ||
continuationOptions, | ||
) | ||
.catch(handlePostTaskError.bind(null, node)); | ||
} | ||
} | ||
|
||
function handlePostTaskError(node, error) { | ||
if (node._status === 'canceled') { | ||
// This task was canceled by unstable_cancelCallback, and there were no | ||
// other user errors. So this must be an AbortError. Suppress it. | ||
} else { | ||
// Otherwise, surface to the user. | ||
throw error; | ||
} | ||
} | ||
|
||
export function unstable_cancelCallback(node: CallbackNode) { | ||
if (node._status === 'ok') { | ||
// Unless the task already failed due to a user error, mark it as canceled. | ||
node._status = 'canceled'; | ||
} | ||
const controller = node._controller; | ||
controller.abort(); | ||
} | ||
|
||
export function unstable_runWithPriority<T>( | ||
priorityLevel: PriorityLevel, | ||
callback: () => T, | ||
): T { | ||
const previousPriorityLevel = currentPriorityLevel_DEPRECATED; | ||
currentPriorityLevel_DEPRECATED = priorityLevel; | ||
try { | ||
return callback(); | ||
} finally { | ||
currentPriorityLevel_DEPRECATED = previousPriorityLevel; | ||
} | ||
} | ||
|
||
export function unstable_getCurrentPriorityLevel() { | ||
return currentPriorityLevel_DEPRECATED; | ||
} | ||
|
||
export function unstable_next<T>(callback: () => T): T { | ||
let priorityLevel; | ||
switch (currentPriorityLevel_DEPRECATED) { | ||
case ImmediatePriority: | ||
case UserBlockingPriority: | ||
case NormalPriority: | ||
// Shift down to normal priority | ||
priorityLevel = NormalPriority; | ||
break; | ||
default: | ||
// Anything lower than normal priority should remain at the current level. | ||
priorityLevel = currentPriorityLevel_DEPRECATED; | ||
break; | ||
} | ||
|
||
const previousPriorityLevel = currentPriorityLevel_DEPRECATED; | ||
currentPriorityLevel_DEPRECATED = priorityLevel; | ||
try { | ||
return callback(); | ||
} finally { | ||
currentPriorityLevel_DEPRECATED = previousPriorityLevel; | ||
} | ||
} | ||
|
||
export function unstable_wrapCallback<T>(callback: () => T): () => T { | ||
const parentPriorityLevel = currentPriorityLevel_DEPRECATED; | ||
return () => { | ||
const previousPriorityLevel = currentPriorityLevel_DEPRECATED; | ||
currentPriorityLevel_DEPRECATED = parentPriorityLevel; | ||
try { | ||
return callback(); | ||
} finally { | ||
currentPriorityLevel_DEPRECATED = previousPriorityLevel; | ||
} | ||
}; | ||
} | ||
|
||
export function unstable_forceFrameRate() {} | ||
|
||
export function unstable_pauseExecution() {} | ||
|
||
export function unstable_continueExecution() {} | ||
|
||
export function unstable_getFirstCallbackNode() { | ||
return null; | ||
} | ||
|
||
// Currently no profiling build | ||
export const unstable_Profiling = null; |
Oops, something went wrong.