Skip to content

Commit b26c555

Browse files
committed
Less indirection in SchedulerWithReactIntegration
I originally kept the React PriorityLevel and Scheduler PriorityLevel types separate in case there was a versioning mismatch between the two modules. However, it looks like we're going to keep the Scheduler module private in the short to medium term, and longer term the public interface will match `postTask`. So I've removed the extra indirection (the switch statements that convert between the two types).
1 parent 114ab52 commit b26c555

File tree

2 files changed

+22
-110
lines changed

2 files changed

+22
-110
lines changed

packages/react-reconciler/src/SchedulerWithReactIntegration.new.js

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ type SchedulerCallbackOptions = {timeout?: number, ...};
5959

6060
const fakeCallbackNode = {};
6161

62-
// Except for NoPriority, these correspond to Scheduler priorities. We use
63-
// ascending numbers so we can compare them like numbers. They start at 90 to
64-
// avoid clashing with Scheduler's priorities.
65-
export const ImmediatePriority: ReactPriorityLevel = 99;
66-
export const UserBlockingPriority: ReactPriorityLevel = 98;
67-
export const NormalPriority: ReactPriorityLevel = 97;
68-
export const LowPriority: ReactPriorityLevel = 96;
69-
export const IdlePriority: ReactPriorityLevel = 95;
70-
// NoPriority is the absence of priority. Also React-only.
62+
export const ImmediatePriority: ReactPriorityLevel = Scheduler_ImmediatePriority;
63+
export const UserBlockingPriority: ReactPriorityLevel = Scheduler_UserBlockingPriority;
64+
export const NormalPriority: ReactPriorityLevel = Scheduler_NormalPriority;
65+
export const LowPriority: ReactPriorityLevel = Scheduler_LowPriority;
66+
export const IdlePriority: ReactPriorityLevel = Scheduler_IdlePriority;
67+
// NoPriority is the absence of priority. React-only.
7168
export const NoPriority: ReactPriorityLevel = 90;
7269

7370
export const shouldYield = Scheduler_shouldYield;
@@ -78,66 +75,25 @@ export const requestPaint =
7875
let syncQueue: Array<SchedulerCallback> | null = null;
7976
let immediateQueueCallbackNode: mixed | null = null;
8077
let isFlushingSyncQueue: boolean = false;
81-
const initialTimeMs: number = Scheduler_now();
82-
83-
// If the initial timestamp is reasonably small, use Scheduler's `now` directly.
84-
// This will be the case for modern browsers that support `performance.now`. In
85-
// older browsers, Scheduler falls back to `Date.now`, which returns a Unix
86-
// timestamp. In that case, subtract the module initialization time to simulate
87-
// the behavior of performance.now and keep our times small enough to fit
88-
// within 32 bits.
89-
// TODO: Consider lifting this into Scheduler.
90-
export const now =
91-
initialTimeMs < 10000 ? Scheduler_now : () => Scheduler_now() - initialTimeMs;
9278

93-
export function getCurrentPriorityLevel(): ReactPriorityLevel {
94-
switch (Scheduler_getCurrentPriorityLevel()) {
95-
case Scheduler_ImmediatePriority:
96-
return ImmediatePriority;
97-
case Scheduler_UserBlockingPriority:
98-
return UserBlockingPriority;
99-
case Scheduler_NormalPriority:
100-
return NormalPriority;
101-
case Scheduler_LowPriority:
102-
return LowPriority;
103-
case Scheduler_IdlePriority:
104-
return IdlePriority;
105-
default:
106-
invariant(false, 'Unknown priority level.');
107-
}
108-
}
79+
export const now = Scheduler_now;
10980

110-
function reactPriorityToSchedulerPriority(reactPriorityLevel) {
111-
switch (reactPriorityLevel) {
112-
case ImmediatePriority:
113-
return Scheduler_ImmediatePriority;
114-
case UserBlockingPriority:
115-
return Scheduler_UserBlockingPriority;
116-
case NormalPriority:
117-
return Scheduler_NormalPriority;
118-
case LowPriority:
119-
return Scheduler_LowPriority;
120-
case IdlePriority:
121-
return Scheduler_IdlePriority;
122-
default:
123-
invariant(false, 'Unknown priority level.');
124-
}
81+
export function getCurrentPriorityLevel(): ReactPriorityLevel {
82+
return Scheduler_getCurrentPriorityLevel();
12583
}
12684

12785
export function runWithPriority<T>(
128-
reactPriorityLevel: ReactPriorityLevel,
86+
priorityLevel: ReactPriorityLevel,
12987
fn: () => T,
13088
): T {
131-
const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
13289
return Scheduler_runWithPriority(priorityLevel, fn);
13390
}
13491

13592
export function scheduleCallback(
136-
reactPriorityLevel: ReactPriorityLevel,
93+
priorityLevel: ReactPriorityLevel,
13794
callback: SchedulerCallback,
13895
options: SchedulerCallbackOptions | void | null,
13996
) {
140-
const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
14197
return Scheduler_scheduleCallback(priorityLevel, callback, options);
14298
}
14399

packages/react-reconciler/src/SchedulerWithReactIntegration.old.js

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ type SchedulerCallbackOptions = {timeout?: number, ...};
5959

6060
const fakeCallbackNode = {};
6161

62-
// Except for NoPriority, these correspond to Scheduler priorities. We use
63-
// ascending numbers so we can compare them like numbers. They start at 90 to
64-
// avoid clashing with Scheduler's priorities.
65-
export const ImmediatePriority: ReactPriorityLevel = 99;
66-
export const UserBlockingPriority: ReactPriorityLevel = 98;
67-
export const NormalPriority: ReactPriorityLevel = 97;
68-
export const LowPriority: ReactPriorityLevel = 96;
69-
export const IdlePriority: ReactPriorityLevel = 95;
70-
// NoPriority is the absence of priority. Also React-only.
62+
export const ImmediatePriority: ReactPriorityLevel = Scheduler_ImmediatePriority;
63+
export const UserBlockingPriority: ReactPriorityLevel = Scheduler_UserBlockingPriority;
64+
export const NormalPriority: ReactPriorityLevel = Scheduler_NormalPriority;
65+
export const LowPriority: ReactPriorityLevel = Scheduler_LowPriority;
66+
export const IdlePriority: ReactPriorityLevel = Scheduler_IdlePriority;
67+
// NoPriority is the absence of priority. React-only.
7168
export const NoPriority: ReactPriorityLevel = 90;
7269

7370
export const shouldYield = Scheduler_shouldYield;
@@ -78,66 +75,25 @@ export const requestPaint =
7875
let syncQueue: Array<SchedulerCallback> | null = null;
7976
let immediateQueueCallbackNode: mixed | null = null;
8077
let isFlushingSyncQueue: boolean = false;
81-
const initialTimeMs: number = Scheduler_now();
82-
83-
// If the initial timestamp is reasonably small, use Scheduler's `now` directly.
84-
// This will be the case for modern browsers that support `performance.now`. In
85-
// older browsers, Scheduler falls back to `Date.now`, which returns a Unix
86-
// timestamp. In that case, subtract the module initialization time to simulate
87-
// the behavior of performance.now and keep our times small enough to fit
88-
// within 32 bits.
89-
// TODO: Consider lifting this into Scheduler.
90-
export const now =
91-
initialTimeMs < 10000 ? Scheduler_now : () => Scheduler_now() - initialTimeMs;
9278

93-
export function getCurrentPriorityLevel(): ReactPriorityLevel {
94-
switch (Scheduler_getCurrentPriorityLevel()) {
95-
case Scheduler_ImmediatePriority:
96-
return ImmediatePriority;
97-
case Scheduler_UserBlockingPriority:
98-
return UserBlockingPriority;
99-
case Scheduler_NormalPriority:
100-
return NormalPriority;
101-
case Scheduler_LowPriority:
102-
return LowPriority;
103-
case Scheduler_IdlePriority:
104-
return IdlePriority;
105-
default:
106-
invariant(false, 'Unknown priority level.');
107-
}
108-
}
79+
export const now = Scheduler_now;
10980

110-
function reactPriorityToSchedulerPriority(reactPriorityLevel) {
111-
switch (reactPriorityLevel) {
112-
case ImmediatePriority:
113-
return Scheduler_ImmediatePriority;
114-
case UserBlockingPriority:
115-
return Scheduler_UserBlockingPriority;
116-
case NormalPriority:
117-
return Scheduler_NormalPriority;
118-
case LowPriority:
119-
return Scheduler_LowPriority;
120-
case IdlePriority:
121-
return Scheduler_IdlePriority;
122-
default:
123-
invariant(false, 'Unknown priority level.');
124-
}
81+
export function getCurrentPriorityLevel(): ReactPriorityLevel {
82+
return Scheduler_getCurrentPriorityLevel();
12583
}
12684

12785
export function runWithPriority<T>(
128-
reactPriorityLevel: ReactPriorityLevel,
86+
priorityLevel: ReactPriorityLevel,
12987
fn: () => T,
13088
): T {
131-
const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
13289
return Scheduler_runWithPriority(priorityLevel, fn);
13390
}
13491

13592
export function scheduleCallback(
136-
reactPriorityLevel: ReactPriorityLevel,
93+
priorityLevel: ReactPriorityLevel,
13794
callback: SchedulerCallback,
13895
options: SchedulerCallbackOptions | void | null,
13996
) {
140-
const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
14197
return Scheduler_scheduleCallback(priorityLevel, callback, options);
14298
}
14399

0 commit comments

Comments
 (0)