|
7 | 7 | * @flow |
8 | 8 | */ |
9 | 9 |
|
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. |
14 | 14 | import * as Scheduler from 'scheduler'; |
15 | 15 | import {__interactionsRef} from 'scheduler/tracing'; |
16 | 16 | import {enableSchedulerTracing} from 'shared/ReactFeatureFlags'; |
@@ -21,19 +21,18 @@ import { |
21 | 21 | setCurrentUpdatePriority, |
22 | 22 | } from './ReactEventPriorities.new'; |
23 | 23 |
|
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; |
37 | 36 |
|
38 | 37 | if (enableSchedulerTracing) { |
39 | 38 | // Provide explicit error message when production+profiling bundle of e.g. |
@@ -51,80 +50,9 @@ if (enableSchedulerTracing) { |
51 | 50 |
|
52 | 51 | export type SchedulerCallback = (isSync: boolean) => SchedulerCallback | null; |
53 | 52 |
|
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. |
72 | 54 | let syncQueue: Array<SchedulerCallback> | null = null; |
73 | 55 | let 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 | | -} |
128 | 56 |
|
129 | 57 | export function scheduleSyncCallback(callback: SchedulerCallback) { |
130 | 58 | // Push this callback into an internal queue. We'll flush these either in |
@@ -138,10 +66,6 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { |
138 | 66 | } |
139 | 67 | } |
140 | 68 |
|
141 | | -export function cancelCallback(callbackNode: mixed) { |
142 | | - Scheduler_cancelCallback(callbackNode); |
143 | | -} |
144 | | - |
145 | 69 | export function flushSyncCallbackQueue() { |
146 | 70 | if (!isFlushingSyncQueue && syncQueue !== null) { |
147 | 71 | // Prevent re-entrancy. |
@@ -167,10 +91,7 @@ export function flushSyncCallbackQueue() { |
167 | 91 | syncQueue = syncQueue.slice(i + 1); |
168 | 92 | } |
169 | 93 | // Resume flushing in the next tick |
170 | | - Scheduler_scheduleCallback( |
171 | | - Scheduler_ImmediatePriority, |
172 | | - flushSyncCallbackQueue, |
173 | | - ); |
| 94 | + scheduleCallback(ImmediatePriority, flushSyncCallbackQueue); |
174 | 95 | throw error; |
175 | 96 | } finally { |
176 | 97 | setCurrentUpdatePriority(previousUpdatePriority); |
|