Skip to content

Commit

Permalink
Make opaque EventPriority type a Lane internally (#21065)
Browse files Browse the repository at this point in the history
Instead of LanePriority, we can use a Lane and skip the extra
conversion. Eventually I want to get rid of LanePriority completely.
  • Loading branch information
acdlite committed Mar 24, 2021
1 parent eb58c39 commit fa868d6
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 60 deletions.
42 changes: 17 additions & 25 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
*/

import type {AnyNativeEvent} from '../events/PluginModuleType';
import type {
FiberRoot,
ReactPriorityLevel,
} from 'react-reconciler/src/ReactInternalTypes';
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
import type {Container, SuspenseInstance} from '../client/ReactDOMHostConfig';
import type {DOMEventName} from '../events/DOMEventNames';
import type {LanePriority} from 'react-reconciler/src/ReactFiberLane.new';

import {
isReplayableDiscreteEvent,
Expand Down Expand Up @@ -74,6 +70,8 @@ import {
IdleEventPriority,
} from 'react-reconciler/src/ReactEventPriorities';

// TODO: These should use the opaque EventPriority type instead of LanePriority.
// Then internally we can use a Lane.
const InputContinuousLanePriority = enableNewReconciler
? InputContinuousLanePriority_new
: InputContinuousLanePriority_old;
Expand All @@ -87,25 +85,6 @@ const getCurrentPriorityLevel = enableNewReconciler
? getCurrentPriorityLevel_new
: getCurrentPriorityLevel_old;

function schedulerPriorityToLanePriority(
schedulerPriorityLevel: ReactPriorityLevel,
): LanePriority {
switch (schedulerPriorityLevel) {
case ImmediateSchedulerPriority:
return DiscreteEventPriority;
case UserBlockingSchedulerPriority:
return ContinuousEventPriority;
case NormalSchedulerPriority:
case LowSchedulerPriority:
// TODO: Handle LowSchedulerPriority, somehow. Maybe the same lane as hydration.
return DefaultEventPriority;
case IdleSchedulerPriority:
return IdleEventPriority;
default:
return DefaultEventPriority;
}
}

// TODO: can we stop exporting these?
export let _enabled = true;

Expand Down Expand Up @@ -431,7 +410,20 @@ export function getEventPriority(domEventName: DOMEventName): * {
// Eventually this mechanism will be replaced by a check
// of the current priority on the native scheduler.
const schedulerPriority = getCurrentPriorityLevel();
return schedulerPriorityToLanePriority(schedulerPriority);
switch (schedulerPriority) {
case ImmediateSchedulerPriority:
return DiscreteEventPriority;
case UserBlockingSchedulerPriority:
return ContinuousEventPriority;
case NormalSchedulerPriority:
case LowSchedulerPriority:
// TODO: Handle LowSchedulerPriority, somehow. Maybe the same lane as hydration.
return DefaultEventPriority;
case IdleSchedulerPriority:
return IdleEventPriority;
default:
return DefaultEventPriority;
}
}
default:
return DefaultEventPriority;
Expand Down
3 changes: 2 additions & 1 deletion packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as Scheduler from 'scheduler/unstable_mock';
import {REACT_FRAGMENT_TYPE, REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
import {
DefaultEventPriority,
IdleEventPriority,
ConcurrentRoot,
LegacyRoot,
} from 'react-reconciler/constants';
Expand Down Expand Up @@ -909,7 +910,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

idleUpdates<T>(fn: () => T): T {
const prevEventPriority = currentEventPriority;
currentEventPriority = NoopRenderer.IdleEventPriority;
currentEventPriority = IdleEventPriority;
try {
fn();
} finally {
Expand Down
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactEventPriorities.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

export {
SyncLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
SyncLane as DiscreteEventPriority,
InputContinuousLane as ContinuousEventPriority,
DefaultLane as DefaultEventPriority,
IdleLane as IdleEventPriority,
} from './ReactFiberLane.new';
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactEventPriorities.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

export {
SyncLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
SyncLane as DiscreteEventPriority,
InputContinuousLane as ContinuousEventPriority,
DefaultLane as DefaultEventPriority,
IdleLane as IdleEventPriority,
} from './ReactFiberLane.old';
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberLane.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const SelectiveHydrationLane: Lane = /* */ 0b0001000000000000000
const NonIdleLanes = /* */ 0b0001111111111111111111111111111;

export const IdleHydrationLane: Lane = /* */ 0b0010000000000000000000000000000;
const IdleLane: Lanes = /* */ 0b0100000000000000000000000000000;
export const IdleLane: Lanes = /* */ 0b0100000000000000000000000000000;

export const OffscreenLane: Lane = /* */ 0b1000000000000000000000000000000;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberLane.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const SelectiveHydrationLane: Lane = /* */ 0b0001000000000000000
const NonIdleLanes = /* */ 0b0001111111111111111111111111111;

export const IdleHydrationLane: Lane = /* */ 0b0010000000000000000000000000000;
const IdleLane: Lanes = /* */ 0b0100000000000000000000000000000;
export const IdleLane: Lanes = /* */ 0b0100000000000000000000000000000;

export const OffscreenLane: Lane = /* */ 0b1000000000000000000000000000000;

Expand Down
10 changes: 0 additions & 10 deletions packages/react-reconciler/src/ReactFiberReconciler.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ import {
} from './ReactFiberHotReloading.new';
import {markRenderScheduled} from './SchedulingProfiler';

// Ideally host configs would import these constants from the reconciler
// entry point, but we can't do this because of a circular dependency.
// They are used by third-party renderers so they need to stay up to date.
export {
SyncLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.new';

export {registerMutableSourceForHydration} from './ReactMutableSource.new';
export {createPortal} from './ReactPortal';
export {
Expand Down
10 changes: 0 additions & 10 deletions packages/react-reconciler/src/ReactFiberReconciler.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ import {
} from './ReactFiberHotReloading.old';
import {markRenderScheduled} from './SchedulingProfiler';

// Ideally host configs would import these constants from the reconciler
// entry point, but we can't do this because of a circular dependency.
// They are used by third-party renderers so they need to stay up to date.
export {
SyncLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.old';

export {registerMutableSourceForHydration} from './ReactMutableSource.old';
export {createPortal} from './ReactPortal';
export {
Expand Down
7 changes: 5 additions & 2 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,11 @@ export function requestUpdateLane(fiber: Fiber): Lane {

// This update originated outside React. Ask the host environement for an
// appropriate priority, based on the type of event.
const eventLanePriority = getCurrentEventPriority();
return findUpdateLane(eventLanePriority);
//
// The opaque type returned by the host config is internally a lane, so we can
// use that directly.
const eventLane = getCurrentEventPriority();
return eventLane;
}

function requestRetryLane(fiber: Fiber) {
Expand Down
7 changes: 5 additions & 2 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,11 @@ export function requestUpdateLane(fiber: Fiber): Lane {

// This update originated outside React. Ask the host environement for an
// appropriate priority, based on the type of event.
const eventLanePriority = getCurrentEventPriority();
return findUpdateLane(eventLanePriority);
//
// The opaque type returned by the host config is internally a lane, so we can
// use that directly.
const eventLane = getCurrentEventPriority();
return eventLane;
}

function requestRetryLane(fiber: Fiber) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactReconcilerConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export {
DiscreteEventPriority,
ContinuousEventPriority,
DefaultEventPriority,
IdleEventPriority,
} from './ReactEventPriorities';
export {ConcurrentRoot, LegacyRoot} from './ReactRootTags';

0 comments on commit fa868d6

Please sign in to comment.