77 * @flow
88 */
99
10- import type { ReactPriorityLevel } from './ReactInternalTypes' ;
11-
12- // Intentionally not named imports because Rollup would use dynamic dispatch for
13- // CommonJS interop named imports .
10+ // This module only exists as an ESM wrapper around the external CommonJS
11+ // Scheduler dependency. Notice that we're intentionally not using named imports
12+ // because Rollup would use dynamic dispatch for CommonJS interop named imports.
13+ // When we switch to ESM, we can delete this module .
1414import * as Scheduler from 'scheduler' ;
1515import { __interactionsRef } from 'scheduler/tracing' ;
1616import { enableSchedulerTracing } from 'shared/ReactFeatureFlags' ;
@@ -21,19 +21,18 @@ import {
2121 setCurrentUpdatePriority ,
2222} from './ReactEventPriorities.new' ;
2323
24- const {
25- unstable_scheduleCallback : Scheduler_scheduleCallback ,
26- unstable_cancelCallback : Scheduler_cancelCallback ,
27- unstable_shouldYield : Scheduler_shouldYield ,
28- unstable_requestPaint : Scheduler_requestPaint ,
29- unstable_now : Scheduler_now ,
30- unstable_getCurrentPriorityLevel : Scheduler_getCurrentPriorityLevel ,
31- unstable_ImmediatePriority : Scheduler_ImmediatePriority ,
32- unstable_UserBlockingPriority : Scheduler_UserBlockingPriority ,
33- unstable_NormalPriority : Scheduler_NormalPriority ,
34- unstable_LowPriority : Scheduler_LowPriority ,
35- unstable_IdlePriority : Scheduler_IdlePriority ,
36- } = Scheduler ;
24+ export const scheduleCallback = Scheduler . unstable_scheduleCallback ;
25+ export const cancelCallback = Scheduler . unstable_cancelCallback ;
26+ export const shouldYield = Scheduler . unstable_shouldYield ;
27+ export const requestPaint = Scheduler . unstable_requestPaint ;
28+ export const now = Scheduler . unstable_now ;
29+ export const getCurrentPriorityLevel =
30+ Scheduler . unstable_getCurrentPriorityLevel ;
31+ export const ImmediatePriority = Scheduler . unstable_ImmediatePriority ;
32+ export const UserBlockingPriority = Scheduler . unstable_UserBlockingPriority ;
33+ export const NormalPriority = Scheduler . unstable_NormalPriority ;
34+ export const LowPriority = Scheduler . unstable_LowPriority ;
35+ export const IdlePriority = Scheduler . unstable_IdlePriority ;
3736
3837if ( enableSchedulerTracing ) {
3938 // Provide explicit error message when production+profiling bundle of e.g.
@@ -51,80 +50,9 @@ if (enableSchedulerTracing) {
5150
5251export type SchedulerCallback = ( isSync : boolean ) => SchedulerCallback | null ;
5352
54- type SchedulerCallbackOptions = { timeout ?: number , ...} ;
55-
56- // Except for NoPriority, these correspond to Scheduler priorities. We use
57- // ascending numbers so we can compare them like numbers. They start at 90 to
58- // avoid clashing with Scheduler's priorities.
59- export const ImmediatePriority : ReactPriorityLevel = 99 ;
60- export const UserBlockingPriority : ReactPriorityLevel = 98 ;
61- export const NormalPriority : ReactPriorityLevel = 97 ;
62- export const LowPriority : ReactPriorityLevel = 96 ;
63- export const IdlePriority : ReactPriorityLevel = 95 ;
64- // NoPriority is the absence of priority. Also React-only.
65- export const NoPriority : ReactPriorityLevel = 90 ;
66-
67- export const shouldYield = Scheduler_shouldYield ;
68- export const requestPaint =
69- // Fall back gracefully if we're running an older version of Scheduler.
70- Scheduler_requestPaint !== undefined ? Scheduler_requestPaint : ( ) => { } ;
71-
53+ // TODO: Move sync task queue to its own module.
7254let syncQueue : Array < SchedulerCallback > | null = null ;
7355let isFlushingSyncQueue : boolean = false ;
74- const initialTimeMs : number = Scheduler_now ( ) ;
75-
76- // If the initial timestamp is reasonably small, use Scheduler's `now` directly.
77- // This will be the case for modern browsers that support `performance.now`. In
78- // older browsers, Scheduler falls back to `Date.now`, which returns a Unix
79- // timestamp. In that case, subtract the module initialization time to simulate
80- // the behavior of performance.now and keep our times small enough to fit
81- // within 32 bits.
82- // TODO: Consider lifting this into Scheduler.
83- export const now =
84- initialTimeMs < 10000 ? Scheduler_now : ( ) => Scheduler_now ( ) - initialTimeMs ;
85-
86- export function getCurrentPriorityLevel ( ) : ReactPriorityLevel {
87- switch ( Scheduler_getCurrentPriorityLevel ( ) ) {
88- case Scheduler_ImmediatePriority :
89- return ImmediatePriority ;
90- case Scheduler_UserBlockingPriority :
91- return UserBlockingPriority ;
92- case Scheduler_NormalPriority :
93- return NormalPriority ;
94- case Scheduler_LowPriority :
95- return LowPriority ;
96- case Scheduler_IdlePriority :
97- return IdlePriority ;
98- default :
99- invariant ( false , 'Unknown priority level.' ) ;
100- }
101- }
102-
103- function reactPriorityToSchedulerPriority ( reactPriorityLevel ) {
104- switch ( reactPriorityLevel ) {
105- case ImmediatePriority :
106- return Scheduler_ImmediatePriority ;
107- case UserBlockingPriority :
108- return Scheduler_UserBlockingPriority ;
109- case NormalPriority :
110- return Scheduler_NormalPriority ;
111- case LowPriority :
112- return Scheduler_LowPriority ;
113- case IdlePriority :
114- return Scheduler_IdlePriority ;
115- default :
116- invariant ( false , 'Unknown priority level.' ) ;
117- }
118- }
119-
120- export function scheduleCallback (
121- reactPriorityLevel : ReactPriorityLevel ,
122- callback : SchedulerCallback ,
123- options : SchedulerCallbackOptions | void | null ,
124- ) {
125- const priorityLevel = reactPriorityToSchedulerPriority ( reactPriorityLevel ) ;
126- return Scheduler_scheduleCallback ( priorityLevel , callback , options ) ;
127- }
12856
12957export function scheduleSyncCallback ( callback : SchedulerCallback ) {
13058 // Push this callback into an internal queue. We'll flush these either in
@@ -138,10 +66,6 @@ export function scheduleSyncCallback(callback: SchedulerCallback) {
13866 }
13967}
14068
141- export function cancelCallback ( callbackNode : mixed ) {
142- Scheduler_cancelCallback ( callbackNode ) ;
143- }
144-
14569export function flushSyncCallbackQueue ( ) {
14670 if ( ! isFlushingSyncQueue && syncQueue !== null ) {
14771 // Prevent re-entrancy.
@@ -167,10 +91,7 @@ export function flushSyncCallbackQueue() {
16791 syncQueue = syncQueue . slice ( i + 1 ) ;
16892 }
16993 // Resume flushing in the next tick
170- Scheduler_scheduleCallback (
171- Scheduler_ImmediatePriority ,
172- flushSyncCallbackQueue ,
173- ) ;
94+ scheduleCallback ( ImmediatePriority , flushSyncCallbackQueue ) ;
17495 throw error ;
17596 } finally {
17697 setCurrentUpdatePriority ( previousUpdatePriority ) ;
0 commit comments