Skip to content

Commit

Permalink
Move priorities to separate import to break cycle (facebook#21060)
Browse files Browse the repository at this point in the history
The event priority constants exports by the reconciler package are
meant to be used by the reconciler (host config) itself. So it doesn't
make sense to export them from a module that requires them.

To break the cycle, we can move them to a separate module and import
that. This looks like a "deep import" of an internal module, which we
try to avoid, but conceptually these are part of the public interface
of the reconciler module. So, no different than importing from the main
`react-reconciler`.

We do need to be careful about not mixing these types of imports with
implementation details. Those are the ones to really avoid.

An unintended benefit of the reconciler fork infra is that it makes
deep imports harder. Any module that we treat as "public", like this
one, needs to account for the `enableNewReconciler` flag and forward
to the correct implementation.
  • Loading branch information
acdlite authored and threepointone committed Oct 24, 2021
1 parent 13332b9 commit 26a68f9
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 86 deletions.
10 changes: 2 additions & 8 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@

import Transform from 'art/core/transform';
import Mode from 'art/modes/current';
import {enableNewReconciler} from 'shared/ReactFeatureFlags';
import invariant from 'shared/invariant';

import {TYPES, EVENT_TYPES, childrenAsString} from './ReactARTInternals';

import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old';
import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new';

const DefaultLanePriority = enableNewReconciler
? DefaultLanePriority_new
: DefaultLanePriority_old;
import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';

const pooledTransform = new Transform();

Expand Down Expand Up @@ -347,7 +341,7 @@ export function shouldSetTextContent(type, props) {
}

export function getCurrentEventPriority() {
return DefaultLanePriority;
return DefaultEventPriority;
}

// The ART renderer is secondary to the React DOM renderer.
Expand Down
10 changes: 2 additions & 8 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,11 @@ import {
enableSuspenseServerRenderer,
enableCreateEventHandleAPI,
enableScopeAPI,
enableNewReconciler,
} from 'shared/ReactFeatureFlags';
import {HostComponent, HostText} from 'react-reconciler/src/ReactWorkTags';
import {listenToAllSupportedEvents} from '../events/DOMPluginEventSystem';

import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old';
import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new';

const DefaultLanePriority = enableNewReconciler
? DefaultLanePriority_new
: DefaultLanePriority_old;
import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';

export type Type = string;
export type Props = {
Expand Down Expand Up @@ -385,7 +379,7 @@ export function createTextInstance(
export function getCurrentEventPriority(): * {
const currentEvent = window.event;
if (currentEvent === undefined) {
return DefaultLanePriority;
return DefaultEventPriority;
}
return getEventPriority(currentEvent.type);
}
Expand Down
38 changes: 18 additions & 20 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} 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 @@ -49,18 +50,13 @@ import {

import {
InputContinuousLanePriority as InputContinuousLanePriority_old,
DefaultLanePriority as DefaultLanePriority_old,
getCurrentUpdateLanePriority as getCurrentUpdateLanePriority_old,
setCurrentUpdateLanePriority as setCurrentUpdateLanePriority_old,
} from 'react-reconciler/src/ReactFiberLane.old';
import {
InputContinuousLanePriority as InputContinuousLanePriority_new,
DefaultLanePriority as DefaultLanePriority_new,
getCurrentUpdateLanePriority as getCurrentUpdateLanePriority_new,
setCurrentUpdateLanePriority as setCurrentUpdateLanePriority_new,
SyncLanePriority,
IdleLanePriority,
NoLanePriority,
} from 'react-reconciler/src/ReactFiberLane.new';
import {getCurrentPriorityLevel as getCurrentPriorityLevel_old} from 'react-reconciler/src/SchedulerWithReactIntegration.old';
import {
Expand All @@ -71,14 +67,16 @@ import {
NormalPriority as NormalSchedulerPriority,
UserBlockingPriority as UserBlockingSchedulerPriority,
} from 'react-reconciler/src/SchedulerWithReactIntegration.new';
import type {LanePriority} from 'react-reconciler/src/ReactFiberLane.new';
import {
DiscreteEventPriority,
ContinuousEventPriority,
DefaultEventPriority,
IdleEventPriority,
} from 'react-reconciler/src/ReactEventPriorities';

const InputContinuousLanePriority = enableNewReconciler
? InputContinuousLanePriority_new
: InputContinuousLanePriority_old;
const DefaultLanePriority = enableNewReconciler
? DefaultLanePriority_new
: DefaultLanePriority_old;
const getCurrentUpdateLanePriority = enableNewReconciler
? getCurrentUpdateLanePriority_new
: getCurrentUpdateLanePriority_old;
Expand All @@ -94,17 +92,17 @@ function schedulerPriorityToLanePriority(
): LanePriority {
switch (schedulerPriorityLevel) {
case ImmediateSchedulerPriority:
return SyncLanePriority;
return DiscreteEventPriority;
case UserBlockingSchedulerPriority:
return InputContinuousLanePriority;
return ContinuousEventPriority;
case NormalSchedulerPriority:
case LowSchedulerPriority:
// TODO: Handle LowSchedulerPriority, somehow. Maybe the same lane as hydration.
return DefaultLanePriority;
return DefaultEventPriority;
case IdleSchedulerPriority:
return IdleLanePriority;
return IdleEventPriority;
default:
return NoLanePriority;
return DefaultEventPriority;
}
}

Expand Down Expand Up @@ -142,13 +140,13 @@ export function createEventListenerWrapperWithPriority(
const eventPriority = getEventPriority(domEventName);
let listenerWrapper;
switch (eventPriority) {
case SyncLanePriority:
case DiscreteEventPriority:
listenerWrapper = dispatchDiscreteEvent;
break;
case InputContinuousLanePriority:
case ContinuousEventPriority:
listenerWrapper = dispatchContinuousEvent;
break;
case DefaultLanePriority:
case DefaultEventPriority:
default:
listenerWrapper = dispatchEvent;
break;
Expand Down Expand Up @@ -407,7 +405,7 @@ export function getEventPriority(domEventName: DOMEventName): * {
case 'popstate':
case 'select':
case 'selectstart':
return SyncLanePriority;
return DiscreteEventPriority;
case 'drag':
case 'dragenter':
case 'dragexit':
Expand All @@ -427,7 +425,7 @@ export function getEventPriority(domEventName: DOMEventName): * {
// eslint-disable-next-line no-fallthrough
case 'mouseenter':
case 'mouseleave':
return InputContinuousLanePriority;
return ContinuousEventPriority;
case 'message': {
// We might be in the Scheduler callback.
// Eventually this mechanism will be replaced by a check
Expand All @@ -436,6 +434,6 @@ export function getEventPriority(domEventName: DOMEventName): * {
return schedulerPriorityToLanePriority(schedulerPriority);
}
default:
return DefaultLanePriority;
return DefaultEventPriority;
}
}
10 changes: 2 additions & 8 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ import type {
import {mountSafeCallback_NOT_REALLY_SAFE} from './NativeMethodsMixinUtils';
import {create, diff} from './ReactNativeAttributePayload';

import {enableNewReconciler} from 'shared/ReactFeatureFlags';
import invariant from 'shared/invariant';

import {dispatchEvent} from './ReactFabricEventEmitter';

import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old';
import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new';

const DefaultLanePriority = enableNewReconciler
? DefaultLanePriority_new
: DefaultLanePriority_old;
import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';

// Modules provided by RN:
import {
Expand Down Expand Up @@ -349,7 +343,7 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
}

export function getCurrentEventPriority(): * {
return DefaultLanePriority;
return DefaultEventPriority;
}

// The Fabric renderer is secondary to the existing React Native renderer.
Expand Down
10 changes: 2 additions & 8 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import type {TouchedViewDataAtPoint} from './ReactNativeTypes';

import invariant from 'shared/invariant';
import {enableNewReconciler} from 'shared/ReactFeatureFlags';

// Modules provided by RN:
import {
Expand All @@ -27,12 +26,7 @@ import {
} from './ReactNativeComponentTree';
import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent';

import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old';
import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new';

const DefaultLanePriority = enableNewReconciler
? DefaultLanePriority_new
: DefaultLanePriority_old;
import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';

const {get: getViewConfigForType} = ReactNativeViewConfigRegistry;

Expand Down Expand Up @@ -268,7 +262,7 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
}

export function getCurrentEventPriority(): * {
return DefaultLanePriority;
return DefaultEventPriority;
}

// -------------------
Expand Down
5 changes: 4 additions & 1 deletion packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import ReactSharedInternals from 'shared/ReactSharedInternals';
import enqueueTask from 'shared/enqueueTask';
const {IsSomeRendererActing} = ReactSharedInternals;

// TODO: Publish public entry point that exports the event priority constants
const DefaultEventPriority = 8;

type Container = {
rootID: string,
children: Array<Instance | TextInstance>,
Expand Down Expand Up @@ -587,7 +590,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
const roots = new Map();
const DEFAULT_ROOT_ID = '<default>';

let currentEventPriority = NoopRenderer.DefaultEventPriority;
let currentEventPriority = DefaultEventPriority;

function childToJSX(child, text) {
if (text !== null) {
Expand Down
14 changes: 10 additions & 4 deletions packages/react-reconciler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,16 @@ This is a property (not a function) that should be set to `true` if your rendere
To implement this method, you'll need some constants available on the _returned_ `Renderer` object:

```js
import {
DiscreteEventPriority,
ContinuousEventPriority,
DefaultEventPriority,
} from './ReactFiberReconciler/src/ReactEventPriorities';

const HostConfig = {
// ...
getCurrentEventPriority() {
return MyRenderer.DefaultEventPriority;
return DefaultEventPriority;
},
// ...
}
Expand All @@ -232,11 +238,11 @@ const MyRenderer = Reconciler(HostConfig);

The constant you return depends on which event, if any, is being handled right now. (In the browser, you can check this using `window.event && window.event.type`).

* **Discrete events:** If the active event is _directly caused by the user_ (such as mouse and keyboard events) and _each event in a sequence is intentional_ (e.g. `click`), return `MyRenderer.DiscreteEventPriority`. This tells React that they should interrupt any background work and cannot be batched across time.
* **Discrete events:** If the active event is _directly caused by the user_ (such as mouse and keyboard events) and _each event in a sequence is intentional_ (e.g. `click`), return `DiscreteEventPriority`. This tells React that they should interrupt any background work and cannot be batched across time.

* **Continuous events:** If the active event is _directly caused by the user_ but _the user can't distinguish between individual events in a sequence_ (e.g. `mouseover`), return `MyRenderer.ContinuousEventPriority`. This tells React they should interrupt any background work but can be batched across time.
* **Continuous events:** If the active event is _directly caused by the user_ but _the user can't distinguish between individual events in a sequence_ (e.g. `mouseover`), return `ContinuousEventPriority`. This tells React they should interrupt any background work but can be batched across time.

* **Other events / No active event:** In all other cases, return `MyRenderer.DefaultEventPriority`. This tells React that this event is considered background work, and interactive events will be prioritized over it.
* **Other events / No active event:** In all other cases, return `DefaultEventPriority`. This tells React that this event is considered background work, and interactive events will be prioritized over it.

You can consult the `getCurrentEventPriority()` implementation in `ReactDOMHostConfig.js` for a reference implementation.

Expand Down
37 changes: 37 additions & 0 deletions packages/react-reconciler/src/ReactEventPriorities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import {enableNewReconciler} from 'shared/ReactFeatureFlags';

import {
DiscreteEventPriority as DiscreteEventPriority_old,
ContinuousEventPriority as ContinuousEventPriority_old,
DefaultEventPriority as DefaultEventPriority_old,
IdleEventPriority as IdleEventPriority_old,
} from './ReactEventPriorities.old';

import {
DiscreteEventPriority as DiscreteEventPriority_new,
ContinuousEventPriority as ContinuousEventPriority_new,
DefaultEventPriority as DefaultEventPriority_new,
IdleEventPriority as IdleEventPriority_new,
} from './ReactEventPriorities.new';

export const DiscreteEventPriority = enableNewReconciler
? DiscreteEventPriority_new
: DiscreteEventPriority_old;
export const ContinuousEventPriority = enableNewReconciler
? ContinuousEventPriority_new
: ContinuousEventPriority_old;
export const DefaultEventPriority = enableNewReconciler
? DefaultEventPriority_new
: DefaultEventPriority_old;
export const IdleEventPriority = enableNewReconciler
? IdleEventPriority_new
: IdleEventPriority_old;
15 changes: 15 additions & 0 deletions packages/react-reconciler/src/ReactEventPriorities.new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export {
SyncLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.new';
15 changes: 15 additions & 0 deletions packages/react-reconciler/src/ReactEventPriorities.old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export {
SyncLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.old';
20 changes: 0 additions & 20 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ import {
registerMutableSourceForHydration as registerMutableSourceForHydration_old,
runWithPriority as runWithPriority_old,
getCurrentUpdateLanePriority as getCurrentUpdateLanePriority_old,
DefaultEventPriority as DefaultEventPriority_old,
DiscreteEventPriority as DiscreteEventPriority_old,
ContinuousEventPriority as ContinuousEventPriority_old,
IdleEventPriority as IdleEventPriority_old,
} from './ReactFiberReconciler.old';

import {
Expand Down Expand Up @@ -96,10 +92,6 @@ import {
registerMutableSourceForHydration as registerMutableSourceForHydration_new,
runWithPriority as runWithPriority_new,
getCurrentUpdateLanePriority as getCurrentUpdateLanePriority_new,
DefaultEventPriority as DefaultEventPriority_new,
DiscreteEventPriority as DiscreteEventPriority_new,
ContinuousEventPriority as ContinuousEventPriority_new,
IdleEventPriority as IdleEventPriority_new,
} from './ReactFiberReconciler.new';

export const createContainer = enableNewReconciler
Expand Down Expand Up @@ -176,18 +168,6 @@ export const createPortal = enableNewReconciler
export const createComponentSelector = enableNewReconciler
? createComponentSelector_new
: createComponentSelector_old;
export const DefaultEventPriority = enableNewReconciler
? DefaultEventPriority_new
: DefaultEventPriority_old;
export const DiscreteEventPriority = enableNewReconciler
? DiscreteEventPriority_new
: DiscreteEventPriority_old;
export const ContinuousEventPriority = enableNewReconciler
? ContinuousEventPriority_new
: ContinuousEventPriority_old;
export const IdleEventPriority = enableNewReconciler
? IdleEventPriority_new
: IdleEventPriority_old;

//TODO: "psuedo" is spelled "pseudo"
export const createHasPseudoClassSelector = enableNewReconciler
Expand Down
Loading

0 comments on commit 26a68f9

Please sign in to comment.