-
Notifications
You must be signed in to change notification settings - Fork 46.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Scheduler indirection #21107
Remove Scheduler indirection #21107
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ import type { | |
import type {Fiber, Dispatcher, HookType} from './ReactInternalTypes'; | ||
import type {Lanes, Lane} from './ReactFiberLane.old'; | ||
import type {HookFlags} from './ReactHookEffectTags'; | ||
import type {ReactPriorityLevel} from './ReactInternalTypes'; | ||
import type {FiberRoot} from './ReactInternalTypes'; | ||
import type {OpaqueIDType} from './ReactFiberHostConfig'; | ||
import type {Cache} from './ReactFiberCacheComponent.old'; | ||
|
@@ -120,7 +119,6 @@ type Update<S, A> = {| | |
eagerReducer: ((S, A) => S) | null, | ||
eagerState: S | null, | ||
next: Update<S, A>, | ||
priority?: ReactPriorityLevel, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used to use this for the Suspense priority warning. That got removed, but if we were to add the warning back, we'd use the update lanes. |
||
|}; | ||
|
||
export type UpdateQueue<S, A> = {| | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -7,10 +7,10 @@ | |||||||
* @flow | ||||||||
*/ | ||||||||
|
||||||||
import type {ReactPriorityLevel} from './ReactInternalTypes'; | ||||||||
|
||||||||
// Intentionally not named imports because Rollup would use dynamic dispatch for | ||||||||
// CommonJS interop named imports. | ||||||||
// This module only exists as an ESM wrapper around the external CommonJS | ||||||||
// Scheduler dependency. Notice that we're intentionally not using named imports | ||||||||
// because Rollup would use dynamic dispatch for CommonJS interop named imports. | ||||||||
// When we switch to ESM, we can delete this module. | ||||||||
import * as Scheduler from 'scheduler'; | ||||||||
import {__interactionsRef} from 'scheduler/tracing'; | ||||||||
import {enableSchedulerTracing} from 'shared/ReactFeatureFlags'; | ||||||||
|
@@ -21,19 +21,18 @@ import { | |||||||
setCurrentUpdatePriority, | ||||||||
} from './ReactEventPriorities.new'; | ||||||||
|
||||||||
const { | ||||||||
unstable_scheduleCallback: Scheduler_scheduleCallback, | ||||||||
unstable_cancelCallback: Scheduler_cancelCallback, | ||||||||
unstable_shouldYield: Scheduler_shouldYield, | ||||||||
unstable_requestPaint: Scheduler_requestPaint, | ||||||||
unstable_now: Scheduler_now, | ||||||||
unstable_getCurrentPriorityLevel: Scheduler_getCurrentPriorityLevel, | ||||||||
unstable_ImmediatePriority: Scheduler_ImmediatePriority, | ||||||||
unstable_UserBlockingPriority: Scheduler_UserBlockingPriority, | ||||||||
unstable_NormalPriority: Scheduler_NormalPriority, | ||||||||
unstable_LowPriority: Scheduler_LowPriority, | ||||||||
unstable_IdlePriority: Scheduler_IdlePriority, | ||||||||
} = Scheduler; | ||||||||
export const scheduleCallback = Scheduler.unstable_scheduleCallback; | ||||||||
export const cancelCallback = Scheduler.unstable_cancelCallback; | ||||||||
export const shouldYield = Scheduler.unstable_shouldYield; | ||||||||
export const requestPaint = Scheduler.unstable_requestPaint; | ||||||||
export const now = Scheduler.unstable_now; | ||||||||
export const getCurrentPriorityLevel = | ||||||||
Scheduler.unstable_getCurrentPriorityLevel; | ||||||||
export const ImmediatePriority = Scheduler.unstable_ImmediatePriority; | ||||||||
export const UserBlockingPriority = Scheduler.unstable_UserBlockingPriority; | ||||||||
export const NormalPriority = Scheduler.unstable_NormalPriority; | ||||||||
export const LowPriority = Scheduler.unstable_LowPriority; | ||||||||
export const IdlePriority = Scheduler.unstable_IdlePriority; | ||||||||
|
||||||||
if (enableSchedulerTracing) { | ||||||||
// Provide explicit error message when production+profiling bundle of e.g. | ||||||||
|
@@ -51,80 +50,9 @@ if (enableSchedulerTracing) { | |||||||
|
||||||||
export type SchedulerCallback = (isSync: boolean) => SchedulerCallback | null; | ||||||||
|
||||||||
type SchedulerCallbackOptions = {timeout?: number, ...}; | ||||||||
|
||||||||
// Except for NoPriority, these correspond to Scheduler priorities. We use | ||||||||
// ascending numbers so we can compare them like numbers. They start at 90 to | ||||||||
// avoid clashing with Scheduler's priorities. | ||||||||
export const ImmediatePriority: ReactPriorityLevel = 99; | ||||||||
export const UserBlockingPriority: ReactPriorityLevel = 98; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Goodbye, 99, 98, and friends. How many times have I seen you while debugging, trying to remember which level it is. Hope to never see you again! 🕺 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello to 0, 1, 2, 3, 4, 5! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll take that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about 0, 1, 2, 4, 8, 16, 32, 64...? 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we stay below 65536 sure. I can't memorize the ones after. |
||||||||
export const NormalPriority: ReactPriorityLevel = 97; | ||||||||
export const LowPriority: ReactPriorityLevel = 96; | ||||||||
export const IdlePriority: ReactPriorityLevel = 95; | ||||||||
// NoPriority is the absence of priority. Also React-only. | ||||||||
export const NoPriority: ReactPriorityLevel = 90; | ||||||||
|
||||||||
export const shouldYield = Scheduler_shouldYield; | ||||||||
export const requestPaint = | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't relevant anymore because Scheduler is a hard dependency of React, now and for the foreseeable future. |
||||||||
// Fall back gracefully if we're running an older version of Scheduler. | ||||||||
Scheduler_requestPaint !== undefined ? Scheduler_requestPaint : () => {}; | ||||||||
|
||||||||
// TODO: Move sync task queue to its own module. | ||||||||
let syncQueue: Array<SchedulerCallback> | null = null; | ||||||||
let isFlushingSyncQueue: boolean = false; | ||||||||
const initialTimeMs: number = Scheduler_now(); | ||||||||
|
||||||||
// If the initial timestamp is reasonably small, use Scheduler's `now` directly. | ||||||||
// This will be the case for modern browsers that support `performance.now`. In | ||||||||
// older browsers, Scheduler falls back to `Date.now`, which returns a Unix | ||||||||
// timestamp. In that case, subtract the module initialization time to simulate | ||||||||
// the behavior of performance.now and keep our times small enough to fit | ||||||||
// within 32 bits. | ||||||||
// TODO: Consider lifting this into Scheduler. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was moved to Scheduler react/packages/scheduler/src/forks/SchedulerDOM.js Lines 49 to 51 in 8b74143
|
||||||||
export const now = | ||||||||
initialTimeMs < 10000 ? Scheduler_now : () => Scheduler_now() - initialTimeMs; | ||||||||
|
||||||||
export function getCurrentPriorityLevel(): ReactPriorityLevel { | ||||||||
switch (Scheduler_getCurrentPriorityLevel()) { | ||||||||
case Scheduler_ImmediatePriority: | ||||||||
return ImmediatePriority; | ||||||||
case Scheduler_UserBlockingPriority: | ||||||||
return UserBlockingPriority; | ||||||||
case Scheduler_NormalPriority: | ||||||||
return NormalPriority; | ||||||||
case Scheduler_LowPriority: | ||||||||
return LowPriority; | ||||||||
case Scheduler_IdlePriority: | ||||||||
return IdlePriority; | ||||||||
default: | ||||||||
invariant(false, 'Unknown priority level.'); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
function reactPriorityToSchedulerPriority(reactPriorityLevel) { | ||||||||
switch (reactPriorityLevel) { | ||||||||
case ImmediatePriority: | ||||||||
return Scheduler_ImmediatePriority; | ||||||||
case UserBlockingPriority: | ||||||||
return Scheduler_UserBlockingPriority; | ||||||||
case NormalPriority: | ||||||||
return Scheduler_NormalPriority; | ||||||||
case LowPriority: | ||||||||
return Scheduler_LowPriority; | ||||||||
case IdlePriority: | ||||||||
return Scheduler_IdlePriority; | ||||||||
default: | ||||||||
invariant(false, 'Unknown priority level.'); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
export function scheduleCallback( | ||||||||
reactPriorityLevel: ReactPriorityLevel, | ||||||||
callback: SchedulerCallback, | ||||||||
options: SchedulerCallbackOptions | void | null, | ||||||||
) { | ||||||||
const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel); | ||||||||
return Scheduler_scheduleCallback(priorityLevel, callback, options); | ||||||||
} | ||||||||
|
||||||||
export function scheduleSyncCallback(callback: SchedulerCallback) { | ||||||||
// Push this callback into an internal queue. We'll flush these either in | ||||||||
|
@@ -138,10 +66,6 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
export function cancelCallback(callbackNode: mixed) { | ||||||||
Scheduler_cancelCallback(callbackNode); | ||||||||
} | ||||||||
|
||||||||
export function flushSyncCallbackQueue() { | ||||||||
if (!isFlushingSyncQueue && syncQueue !== null) { | ||||||||
// Prevent re-entrancy. | ||||||||
|
@@ -167,10 +91,7 @@ export function flushSyncCallbackQueue() { | |||||||
syncQueue = syncQueue.slice(i + 1); | ||||||||
} | ||||||||
// Resume flushing in the next tick | ||||||||
Scheduler_scheduleCallback( | ||||||||
Scheduler_ImmediatePriority, | ||||||||
flushSyncCallbackQueue, | ||||||||
); | ||||||||
scheduleCallback(ImmediatePriority, flushSyncCallbackQueue); | ||||||||
throw error; | ||||||||
} finally { | ||||||||
setCurrentUpdatePriority(previousUpdatePriority); | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice