Skip to content

Commit 96c7ce3

Browse files
committed
Separate EventPriority from Lane
1 parent d1e35c7 commit 96c7ce3

File tree

8 files changed

+98
-43
lines changed

8 files changed

+98
-43
lines changed

packages/react-reconciler/src/ReactEventPriorities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
isHigherEventPriority as isHigherEventPriority_new,
3232
} from './ReactEventPriorities.new';
3333

34-
export opaque type EventPriority = number;
34+
export type EventPriority = number;
3535

3636
export const DiscreteEventPriority: EventPriority = enableNewReconciler
3737
? (DiscreteEventPriority_new: any)

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ import {
1919
includesNonIdleWork,
2020
} from './ReactFiberLane.new';
2121

22-
export opaque type EventPriority = Lane;
22+
// TODO: Ideally this would be opaque but that doesn't work well with
23+
// our reconciler fork infra, since these leak into non-reconciler packages.
24+
export type EventPriority = number;
2325

26+
export const NoEventPriority: EventPriority = NoLane;
2427
export const DiscreteEventPriority: EventPriority = SyncLane;
25-
export const ContinuousEventPriority: EventPriority = InputContinuousLane;
26-
export const DefaultEventPriority: EventPriority = DefaultLane;
28+
export const ContinuousEventPriority: EventPriority = SyncLane | (2 << 1);
29+
export const DefaultEventPriority: EventPriority = SyncLane | (1 << 1);
2730
export const IdleEventPriority: EventPriority = IdleLane;
2831

29-
let currentUpdatePriority: EventPriority = NoLane;
32+
let currentUpdatePriority: EventPriority = NoEventPriority;
3033

3134
export function getCurrentUpdatePriority(): EventPriority {
3235
return currentUpdatePriority;
@@ -80,3 +83,13 @@ export function lanesToEventPriority(lanes: Lanes): EventPriority {
8083
}
8184
return IdleEventPriority;
8285
}
86+
87+
export function laneToEventPriority(lane: Lane): EventPriority {
88+
if (lane === DefaultLane) {
89+
return DefaultEventPriority;
90+
}
91+
if (lane === InputContinuousLane) {
92+
return ContinuousEventPriority;
93+
}
94+
return (lane: any);
95+
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ import {
1919
includesNonIdleWork,
2020
} from './ReactFiberLane.old';
2121

22-
export opaque type EventPriority = Lane;
22+
// TODO: Ideally this would be opaque but that doesn't work well with
23+
// our reconciler fork infra, since these leak into non-reconciler packages.
24+
export type EventPriority = number;
2325

26+
export const NoEventPriority: EventPriority = NoLane;
2427
export const DiscreteEventPriority: EventPriority = SyncLane;
25-
export const ContinuousEventPriority: EventPriority = InputContinuousLane;
26-
export const DefaultEventPriority: EventPriority = DefaultLane;
28+
export const ContinuousEventPriority: EventPriority = SyncLane | (2 << 1);
29+
export const DefaultEventPriority: EventPriority = SyncLane | (1 << 1);
2730
export const IdleEventPriority: EventPriority = IdleLane;
2831

29-
let currentUpdatePriority: EventPriority = NoLane;
32+
let currentUpdatePriority: EventPriority = NoEventPriority;
3033

3134
export function getCurrentUpdatePriority(): EventPriority {
3235
return currentUpdatePriority;
@@ -80,3 +83,13 @@ export function lanesToEventPriority(lanes: Lanes): EventPriority {
8083
}
8184
return IdleEventPriority;
8285
}
86+
87+
export function laneToEventPriority(lane: Lane): EventPriority {
88+
if (lane === DefaultLane) {
89+
return DefaultEventPriority;
90+
}
91+
if (lane === InputContinuousLane) {
92+
return ContinuousEventPriority;
93+
}
94+
return (lane: any);
95+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import type {Container} from './ReactFiberHostConfig';
2020
import {noTimeout, supportsHydration} from './ReactFiberHostConfig';
2121
import {createHostRootFiber} from './ReactFiber.new';
2222
import {
23-
NoLane,
2423
NoLanes,
2524
NoTimestamp,
2625
TotalLanes,
2726
createLaneMap,
2827
} from './ReactFiberLane.new';
28+
import {NoEventPriority} from './ReactEventPriorities.new';
2929
import {
3030
enableSuspenseCallback,
3131
enableCache,
@@ -61,7 +61,7 @@ function FiberRootNode(
6161
this.context = null;
6262
this.pendingContext = null;
6363
this.callbackNode = null;
64-
this.callbackPriority = NoLane;
64+
this.callbackPriority = NoEventPriority;
6565
this.eventTimes = createLaneMap(NoLanes);
6666
this.expirationTimes = createLaneMap(NoTimestamp);
6767

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import type {Container} from './ReactFiberHostConfig';
2020
import {noTimeout, supportsHydration} from './ReactFiberHostConfig';
2121
import {createHostRootFiber} from './ReactFiber.old';
2222
import {
23-
NoLane,
2423
NoLanes,
2524
NoTimestamp,
2625
TotalLanes,
2726
createLaneMap,
2827
} from './ReactFiberLane.old';
28+
import {NoEventPriority} from './ReactEventPriorities.old';
2929
import {
3030
enableSuspenseCallback,
3131
enableCache,
@@ -61,7 +61,7 @@ function FiberRootNode(
6161
this.context = null;
6262
this.pendingContext = null;
6363
this.callbackNode = null;
64-
this.callbackPriority = NoLane;
64+
this.callbackPriority = NoEventPriority;
6565
this.eventTimes = createLaneMap(NoLanes);
6666
this.expirationTimes = createLaneMap(NoTimestamp);
6767

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,20 @@ import {
166166
movePendingFibersToMemoized,
167167
addTransitionToLanesMap,
168168
getTransitionsForLanes,
169+
InputContinuousLane,
170+
DefaultLane,
169171
} from './ReactFiberLane.new';
170172
import {
171173
DiscreteEventPriority,
172174
ContinuousEventPriority,
173175
DefaultEventPriority,
176+
NoEventPriority,
174177
IdleEventPriority,
175178
getCurrentUpdatePriority,
176179
setCurrentUpdatePriority,
177180
lowerEventPriority,
178181
lanesToEventPriority,
182+
laneToEventPriority,
179183
} from './ReactEventPriorities.new';
180184
import {requestCurrentTransition, NoTransition} from './ReactFiberTransition';
181185
import {beginWork as originalBeginWork} from './ReactFiberBeginWork.new';
@@ -663,22 +667,30 @@ export function requestUpdateLane(fiber: Fiber): Lane {
663667
// Updates originating inside certain React methods, like flushSync, have
664668
// their priority set by tracking it with a context variable.
665669
//
666-
// The opaque type returned by the host config is internally a lane, so we can
667-
// use that directly.
668670
// TODO: Move this type conversion to the event priority module.
669-
const updateLane: Lane = (getCurrentUpdatePriority(): any);
670-
if (updateLane !== NoLane) {
671-
return updateLane;
671+
const updatePriority = getCurrentUpdatePriority();
672+
if (updatePriority !== NoEventPriority) {
673+
if (updatePriority === DefaultEventPriority) {
674+
return DefaultLane;
675+
}
676+
if (updatePriority === ContinuousEventPriority) {
677+
return InputContinuousLane;
678+
}
679+
return (updatePriority: any);
672680
}
673681

674682
// This update originated outside React. Ask the host environment for an
675683
// appropriate priority, based on the type of event.
676684
//
677-
// The opaque type returned by the host config is internally a lane, so we can
678-
// use that directly.
679685
// TODO: Move this type conversion to the event priority module.
680-
const eventLane: Lane = (getCurrentEventPriority(): any);
681-
return eventLane;
686+
const eventPriority = getCurrentEventPriority();
687+
if (eventPriority === DefaultEventPriority) {
688+
return DefaultLane;
689+
}
690+
if (eventPriority === ContinuousEventPriority) {
691+
return InputContinuousLane;
692+
}
693+
return (eventPriority: any);
682694
}
683695

684696
function requestRetryLane(fiber: Fiber) {
@@ -887,12 +899,14 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
887899
cancelCallback(existingCallbackNode);
888900
}
889901
root.callbackNode = null;
890-
root.callbackPriority = NoLane;
902+
root.callbackPriority = NoEventPriority;
891903
return;
892904
}
893905

894906
// We use the highest priority lane to represent the priority of the callback.
895-
const newCallbackPriority = getHighestPriorityLane(nextLanes);
907+
const newCallbackPriority = laneToEventPriority(
908+
getHighestPriorityLane(nextLanes),
909+
);
896910

897911
// Check if there's an existing task. We may be able to reuse it.
898912
const existingCallbackPriority = root.callbackPriority;
@@ -913,7 +927,7 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
913927
// TODO: Temporary until we confirm this warning is not fired.
914928
if (
915929
existingCallbackNode == null &&
916-
existingCallbackPriority !== SyncLane
930+
existingCallbackPriority !== DiscreteEventPriority
917931
) {
918932
console.error(
919933
'Expected scheduled callback to exist. This error is likely caused by a bug in React. Please file an issue.',
@@ -931,7 +945,7 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
931945

932946
// Schedule a new callback.
933947
let newCallbackNode;
934-
if (newCallbackPriority === SyncLane) {
948+
if (newCallbackPriority === DiscreteEventPriority) {
935949
// Special case: Sync React callbacks are scheduled on a special
936950
// internal queue
937951
if (root.tag === LegacyRoot) {
@@ -2547,7 +2561,7 @@ function commitRootImpl(
25472561
// commitRoot never returns a continuation; it always finishes synchronously.
25482562
// So we can clear these now to allow a new callback to be scheduled.
25492563
root.callbackNode = null;
2550-
root.callbackPriority = NoLane;
2564+
root.callbackPriority = NoEventPriority;
25512565

25522566
// Check which lanes no longer have any work scheduled on them, and mark
25532567
// those as finished.

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,20 @@ import {
166166
movePendingFibersToMemoized,
167167
addTransitionToLanesMap,
168168
getTransitionsForLanes,
169+
InputContinuousLane,
170+
DefaultLane,
169171
} from './ReactFiberLane.old';
170172
import {
171173
DiscreteEventPriority,
172174
ContinuousEventPriority,
173175
DefaultEventPriority,
176+
NoEventPriority,
174177
IdleEventPriority,
175178
getCurrentUpdatePriority,
176179
setCurrentUpdatePriority,
177180
lowerEventPriority,
178181
lanesToEventPriority,
182+
laneToEventPriority,
179183
} from './ReactEventPriorities.old';
180184
import {requestCurrentTransition, NoTransition} from './ReactFiberTransition';
181185
import {beginWork as originalBeginWork} from './ReactFiberBeginWork.old';
@@ -663,22 +667,30 @@ export function requestUpdateLane(fiber: Fiber): Lane {
663667
// Updates originating inside certain React methods, like flushSync, have
664668
// their priority set by tracking it with a context variable.
665669
//
666-
// The opaque type returned by the host config is internally a lane, so we can
667-
// use that directly.
668670
// TODO: Move this type conversion to the event priority module.
669-
const updateLane: Lane = (getCurrentUpdatePriority(): any);
670-
if (updateLane !== NoLane) {
671-
return updateLane;
671+
const updatePriority = getCurrentUpdatePriority();
672+
if (updatePriority !== NoEventPriority) {
673+
if (updatePriority === DefaultEventPriority) {
674+
return DefaultLane;
675+
}
676+
if (updatePriority === ContinuousEventPriority) {
677+
return InputContinuousLane;
678+
}
679+
return (updatePriority: any);
672680
}
673681

674682
// This update originated outside React. Ask the host environment for an
675683
// appropriate priority, based on the type of event.
676684
//
677-
// The opaque type returned by the host config is internally a lane, so we can
678-
// use that directly.
679685
// TODO: Move this type conversion to the event priority module.
680-
const eventLane: Lane = (getCurrentEventPriority(): any);
681-
return eventLane;
686+
const eventPriority = getCurrentEventPriority();
687+
if (eventPriority === DefaultEventPriority) {
688+
return DefaultLane;
689+
}
690+
if (eventPriority === ContinuousEventPriority) {
691+
return InputContinuousLane;
692+
}
693+
return (eventPriority: any);
682694
}
683695

684696
function requestRetryLane(fiber: Fiber) {
@@ -887,12 +899,14 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
887899
cancelCallback(existingCallbackNode);
888900
}
889901
root.callbackNode = null;
890-
root.callbackPriority = NoLane;
902+
root.callbackPriority = NoEventPriority;
891903
return;
892904
}
893905

894906
// We use the highest priority lane to represent the priority of the callback.
895-
const newCallbackPriority = getHighestPriorityLane(nextLanes);
907+
const newCallbackPriority = laneToEventPriority(
908+
getHighestPriorityLane(nextLanes),
909+
);
896910

897911
// Check if there's an existing task. We may be able to reuse it.
898912
const existingCallbackPriority = root.callbackPriority;
@@ -913,7 +927,7 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
913927
// TODO: Temporary until we confirm this warning is not fired.
914928
if (
915929
existingCallbackNode == null &&
916-
existingCallbackPriority !== SyncLane
930+
existingCallbackPriority !== DiscreteEventPriority
917931
) {
918932
console.error(
919933
'Expected scheduled callback to exist. This error is likely caused by a bug in React. Please file an issue.',
@@ -931,7 +945,7 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
931945

932946
// Schedule a new callback.
933947
let newCallbackNode;
934-
if (newCallbackPriority === SyncLane) {
948+
if (newCallbackPriority === DiscreteEventPriority) {
935949
// Special case: Sync React callbacks are scheduled on a special
936950
// internal queue
937951
if (root.tag === LegacyRoot) {
@@ -2547,7 +2561,7 @@ function commitRootImpl(
25472561
// commitRoot never returns a continuation; it always finishes synchronously.
25482562
// So we can clear these now to allow a new callback to be scheduled.
25492563
root.callbackNode = null;
2550-
root.callbackPriority = NoLane;
2564+
root.callbackPriority = NoEventPriority;
25512565

25522566
// Check which lanes no longer have any work scheduled on them, and mark
25532567
// those as finished.

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import type {
2222
import type {WorkTag} from './ReactWorkTags';
2323
import type {TypeOfMode} from './ReactTypeOfMode';
2424
import type {Flags} from './ReactFiberFlags';
25-
import type {Lane, Lanes, LaneMap} from './ReactFiberLane.old';
25+
import type {Lanes, LaneMap} from './ReactFiberLane.old';
26+
import type {EventPriority} from './ReactEventPriorities';
2627
import type {RootTag} from './ReactRootTags';
2728
import type {
2829
Container,
@@ -239,7 +240,7 @@ type BaseFiberRootProperties = {
239240
// Node returned by Scheduler.scheduleCallback. Represents the next rendering
240241
// task that the root will work on.
241242
callbackNode: any,
242-
callbackPriority: Lane,
243+
callbackPriority: EventPriority,
243244
eventTimes: LaneMap<number>,
244245
expirationTimes: LaneMap<number>,
245246
hiddenUpdates: LaneMap<Array<ConcurrentUpdate> | null>,

0 commit comments

Comments
 (0)