From 4f09483ea8a079768917576e68275ce619b462e5 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Tue, 23 Mar 2021 23:08:48 -0500 Subject: [PATCH] Make opaque EventPriority type a Lane internally Instead of LanePriority, we can use a Lane and skip the extra conversion. Eventually I want to get rid of LanePriority completely. --- .../src/events/ReactDOMEventListener.js | 42 ++++++++----------- .../src/createReactNoop.js | 3 +- .../src/ReactEventPriorities.new.js | 8 ++-- .../src/ReactEventPriorities.old.js | 8 ++-- .../src/ReactFiberLane.new.js | 2 +- .../src/ReactFiberLane.old.js | 2 +- .../src/ReactFiberReconciler.new.js | 10 ----- .../src/ReactFiberReconciler.old.js | 10 ----- .../src/ReactFiberWorkLoop.new.js | 7 +++- .../src/ReactFiberWorkLoop.old.js | 7 +++- .../src/ReactReconcilerConstants.js | 1 + 11 files changed, 40 insertions(+), 60 deletions(-) diff --git a/packages/react-dom/src/events/ReactDOMEventListener.js b/packages/react-dom/src/events/ReactDOMEventListener.js index 57ec840e61b22..7b2dd4c5538f9 100644 --- a/packages/react-dom/src/events/ReactDOMEventListener.js +++ b/packages/react-dom/src/events/ReactDOMEventListener.js @@ -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, @@ -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; @@ -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; @@ -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; diff --git a/packages/react-noop-renderer/src/createReactNoop.js b/packages/react-noop-renderer/src/createReactNoop.js index 1f90af7bac00f..ce80ca3374374 100644 --- a/packages/react-noop-renderer/src/createReactNoop.js +++ b/packages/react-noop-renderer/src/createReactNoop.js @@ -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'; @@ -909,7 +910,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { idleUpdates(fn: () => T): T { const prevEventPriority = currentEventPriority; - currentEventPriority = NoopRenderer.IdleEventPriority; + currentEventPriority = IdleEventPriority; try { fn(); } finally { diff --git a/packages/react-reconciler/src/ReactEventPriorities.new.js b/packages/react-reconciler/src/ReactEventPriorities.new.js index 4f127a522783c..d8eb817db1c50 100644 --- a/packages/react-reconciler/src/ReactEventPriorities.new.js +++ b/packages/react-reconciler/src/ReactEventPriorities.new.js @@ -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'; diff --git a/packages/react-reconciler/src/ReactEventPriorities.old.js b/packages/react-reconciler/src/ReactEventPriorities.old.js index 2d6a60eb88178..8db438ed25e4e 100644 --- a/packages/react-reconciler/src/ReactEventPriorities.old.js +++ b/packages/react-reconciler/src/ReactEventPriorities.old.js @@ -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'; diff --git a/packages/react-reconciler/src/ReactFiberLane.new.js b/packages/react-reconciler/src/ReactFiberLane.new.js index 692f799bce858..fc594743fe53a 100644 --- a/packages/react-reconciler/src/ReactFiberLane.new.js +++ b/packages/react-reconciler/src/ReactFiberLane.new.js @@ -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; diff --git a/packages/react-reconciler/src/ReactFiberLane.old.js b/packages/react-reconciler/src/ReactFiberLane.old.js index aaf63a66403b5..4c7ac9ffd3c3a 100644 --- a/packages/react-reconciler/src/ReactFiberLane.old.js +++ b/packages/react-reconciler/src/ReactFiberLane.old.js @@ -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; diff --git a/packages/react-reconciler/src/ReactFiberReconciler.new.js b/packages/react-reconciler/src/ReactFiberReconciler.new.js index 5538b131fd923..95b8f5b38977f 100644 --- a/packages/react-reconciler/src/ReactFiberReconciler.new.js +++ b/packages/react-reconciler/src/ReactFiberReconciler.new.js @@ -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 { diff --git a/packages/react-reconciler/src/ReactFiberReconciler.old.js b/packages/react-reconciler/src/ReactFiberReconciler.old.js index 1e63a9999de24..d72c8f2c2b88d 100644 --- a/packages/react-reconciler/src/ReactFiberReconciler.old.js +++ b/packages/react-reconciler/src/ReactFiberReconciler.old.js @@ -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 { diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js index 186381b9b5cf8..f669ef12fdf61 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js @@ -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) { diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js index ba328d5fdb449..24ae5fdd638f6 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js @@ -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) { diff --git a/packages/react-reconciler/src/ReactReconcilerConstants.js b/packages/react-reconciler/src/ReactReconcilerConstants.js index ce41e42dc0b2f..7c0bdfc5ca1f7 100644 --- a/packages/react-reconciler/src/ReactReconcilerConstants.js +++ b/packages/react-reconciler/src/ReactReconcilerConstants.js @@ -14,5 +14,6 @@ export { DiscreteEventPriority, ContinuousEventPriority, DefaultEventPriority, + IdleEventPriority, } from './ReactEventPriorities'; export {ConcurrentRoot, LegacyRoot} from './ReactRootTags';