Skip to content

Commit

Permalink
[Fiber] Move updatePriority tracking to renderers (facebook#28751)
Browse files Browse the repository at this point in the history
Currently updatePriority is tracked in the reconciler. `flushSync` is
going to be implemented reconciler agnostic soon and we need to move the
tracking of this state to the renderer and out of reconciler. This
change implements new renderer bin dings for getCurrentUpdatePriority
and setCurrentUpdatePriority.

I was originally going to have the getter also do the event priority
defaulting using window.event so we eliminate getCur rentEventPriority
but this makes all the callsites where we store the true current
updatePriority on the stack harder to work with so for now they remain
separate.

I also moved runWithPriority to the renderer since it really belongs
whereever the state is being managed and it is only currently exposed in
the DOM renderer.

Additionally the current update priority is not stored on
ReactDOMSharedInternals. While not particularly meaningful in this
change it opens the door to implementing `flushSync` outside of the
reconciler
  • Loading branch information
gnoff authored and AndyPengc12 committed Apr 15, 2024
1 parent 83ee133 commit 8acc078
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 98 deletions.
21 changes: 18 additions & 3 deletions packages/react-art/src/ReactFiberConfigART.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';

import Transform from 'art/core/transform';
import Mode from 'art/modes/current';

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

import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';
import {
DefaultEventPriority,
NoEventPriority,
} from 'react-reconciler/src/ReactEventPriorities';

const pooledTransform = new Transform();

Expand Down Expand Up @@ -336,8 +341,18 @@ export function shouldSetTextContent(type, props) {
);
}

export function getCurrentEventPriority() {
return DefaultEventPriority;
let currentUpdatePriority: EventPriority = NoEventPriority;

export function setCurrentUpdatePriority(newPriority: EventPriority): void {
currentUpdatePriority = newPriority;
}

export function getCurrentUpdatePriority(): EventPriority {
return currentUpdatePriority;
}

export function resolveUpdatePriority(): EventPriority {
return currentUpdatePriority || DefaultEventPriority;
}

export function shouldAttemptEagerTransition() {
Expand Down
55 changes: 55 additions & 0 deletions packages/react-dom-bindings/src/client/ReactDOMUpdatePriority.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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 type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';

import {getEventPriority} from '../events/ReactDOMEventListener';
import {
NoEventPriority,
DefaultEventPriority,
} from 'react-reconciler/src/ReactEventPriorities';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';

export function setCurrentUpdatePriority(
newPriority: EventPriority,
// Closure will consistently not inline this function when it has arity 1
// however when it has arity 2 even if the second arg is omitted at every
// callsite it seems to inline it even when the internal length of the function
// is much longer. I hope this is consistent enough to rely on across builds
IntentionallyUnusedArgument?: empty,
): void {
ReactDOMSharedInternals.up = newPriority;
}

export function getCurrentUpdatePriority(): EventPriority {
return ReactDOMSharedInternals.up;
}

export function resolveUpdatePriority(): EventPriority {
const updatePriority = ReactDOMSharedInternals.up;
if (updatePriority !== NoEventPriority) {
return updatePriority;
}
const currentEvent = window.event;
if (currentEvent === undefined) {
return DefaultEventPriority;
}
return getEventPriority(currentEvent.type);
}

export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
const previousPriority = getCurrentUpdatePriority();
try {
setCurrentUpdatePriority(priority);
return fn();
} finally {
setCurrentUpdatePriority(previousPriority);
}
}
16 changes: 5 additions & 11 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @flow
*/

import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
import type {DOMEventName} from '../events/DOMEventNames';
import type {Fiber, FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
import type {
Expand All @@ -29,11 +28,15 @@ import type {

import {NotPending} from 'react-dom-bindings/src/shared/ReactDOMFormActions';
import {getCurrentRootHostContainer} from 'react-reconciler/src/ReactFiberHostContext';
import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';

import hasOwnProperty from 'shared/hasOwnProperty';
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';

export {
setCurrentUpdatePriority,
getCurrentUpdatePriority,
resolveUpdatePriority,
} from './ReactDOMUpdatePriority';
import {
precacheFiberNode,
updateFiberProps,
Expand Down Expand Up @@ -69,7 +72,6 @@ import {
import {
isEnabled as ReactBrowserEventEmitterIsEnabled,
setEnabled as ReactBrowserEventEmitterSetEnabled,
getEventPriority,
} from '../events/ReactDOMEventListener';
import {SVG_NAMESPACE, MATH_NAMESPACE} from './DOMNamespaces';
import {
Expand Down Expand Up @@ -572,14 +574,6 @@ export function createTextInstance(
return textNode;
}

export function getCurrentEventPriority(): EventPriority {
const currentEvent = window.event;
if (currentEvent === undefined) {
return DefaultEventPriority;
}
return getEventPriority(currentEvent.type);
}

let currentPopstateTransitionEvent: Event | null = null;
export function shouldAttemptEagerTransition(): boolean {
const event = window.event;
Expand Down
10 changes: 6 additions & 4 deletions packages/react-dom-bindings/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import {
} from '../client/ReactDOMComponentTree';

import {dispatchEventForPluginEventSystem} from './DOMPluginEventSystem';
import {
getCurrentUpdatePriority,
setCurrentUpdatePriority,
} from '../client/ReactDOMUpdatePriority';

import {
getCurrentPriorityLevel as getCurrentSchedulerPriorityLevel,
Expand All @@ -48,8 +52,6 @@ import {
ContinuousEventPriority,
DefaultEventPriority,
IdleEventPriority,
getCurrentUpdatePriority,
setCurrentUpdatePriority,
} from 'react-reconciler/src/ReactEventPriorities';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {isRootDehydrated} from 'react-reconciler/src/ReactFiberShellHydration';
Expand Down Expand Up @@ -115,9 +117,9 @@ function dispatchDiscreteEvent(
container: EventTarget,
nativeEvent: AnyNativeEvent,
) {
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = null;
const previousPriority = getCurrentUpdatePriority();
try {
setCurrentUpdatePriority(DiscreteEventPriority);
dispatchEvent(domEventName, eventSystemFlags, container, nativeEvent);
Expand All @@ -133,9 +135,9 @@ function dispatchContinuousEvent(
container: EventTarget,
nativeEvent: AnyNativeEvent,
) {
const previousPriority = getCurrentUpdatePriority();
const prevTransition = ReactCurrentBatchConfig.transition;
ReactCurrentBatchConfig.transition = null;
const previousPriority = getCurrentUpdatePriority();
try {
setCurrentUpdatePriority(ContinuousEventPriority);
dispatchEvent(domEventName, eventSystemFlags, container, nativeEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ import {HostRoot, SuspenseComponent} from 'react-reconciler/src/ReactWorkTags';
import {isHigherEventPriority} from 'react-reconciler/src/ReactEventPriorities';
import {isRootDehydrated} from 'react-reconciler/src/ReactFiberShellHydration';
import {dispatchReplayedFormAction} from './plugins/FormActionEventPlugin';
import {
getCurrentUpdatePriority,
runWithPriority as attemptHydrationAtPriority,
} from '../client/ReactDOMUpdatePriority';

import {
attemptContinuousHydration,
attemptHydrationAtCurrentPriority,
} from 'react-reconciler/src/ReactFiberReconciler';
import {
runWithPriority as attemptHydrationAtPriority,
getCurrentUpdatePriority,
} from 'react-reconciler/src/ReactEventPriorities';

// TODO: Upgrade this definition once we're on a newer version of Flow that
// has this definition built-in.
Expand Down
11 changes: 8 additions & 3 deletions packages/react-dom/src/ReactDOMSharedInternals.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
* @flow
*/

import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
import type {HostDispatcher} from './shared/ReactDOMTypes';

import {NoEventPriority} from 'react-reconciler/src/ReactEventPriorities';

type InternalsType = {
usingClientEntryPoint: boolean,
Events: [any, any, any, any, any, any],
Expand All @@ -20,6 +23,7 @@ type InternalsType = {
| ((
componentOrElement: React$Component<any, any>,
) => null | Element | Text),
up /* currentUpdatePriority */: EventPriority,
};

function noop() {}
Expand All @@ -34,13 +38,14 @@ const DefaultDispatcher: HostDispatcher = {
preinitModuleScript: noop,
};

const Internals: InternalsType = ({
const Internals: InternalsType = {
usingClientEntryPoint: false,
Events: null,
Events: (null: any),
ReactDOMCurrentDispatcher: {
current: DefaultDispatcher,
},
findDOMNode: null,
}: any);
up /* currentUpdatePriority */: NoEventPriority,
};

export default Internals;
2 changes: 1 addition & 1 deletion packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import {
isValidContainer,
} from './ReactDOMRoot';
import {createEventHandle} from 'react-dom-bindings/src/client/ReactDOMEventHandle';
import {runWithPriority} from 'react-dom-bindings/src/client/ReactDOMUpdatePriority';

import {
flushSync as flushSyncWithoutWarningIfAlreadyRendering,
isAlreadyRendering,
injectIntoDevTools,
findHostInstance,
} from 'react-reconciler/src/ReactFiberReconciler';
import {runWithPriority} from 'react-reconciler/src/ReactEventPriorities';
import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal';
import {canUseDOM} from 'shared/ExecutionEnvironment';
import ReactVersion from 'shared/ReactVersion';
Expand Down
16 changes: 15 additions & 1 deletion packages/react-native-renderer/src/ReactFiberConfigFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
import {create, diff} from './ReactNativeAttributePayload';
import {dispatchEvent} from './ReactFabricEventEmitter';
import {
NoEventPriority,
DefaultEventPriority,
DiscreteEventPriority,
type EventPriority,
Expand Down Expand Up @@ -311,7 +312,20 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
return false;
}

export function getCurrentEventPriority(): EventPriority {
let currentUpdatePriority: EventPriority = NoEventPriority;
export function setCurrentUpdatePriority(newPriority: EventPriority): void {
currentUpdatePriority = newPriority;
}

export function getCurrentUpdatePriority(): EventPriority {
return currentUpdatePriority;
}

export function resolveUpdatePriority(): EventPriority {
if (currentUpdatePriority !== NoEventPriority) {
return currentUpdatePriority;
}

const currentEventPriority = fabricGetCurrentEventPriority
? fabricGetCurrentEventPriority()
: null;
Expand Down
15 changes: 14 additions & 1 deletion packages/react-native-renderer/src/ReactFiberConfigNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent';

import {
DefaultEventPriority,
NoEventPriority,
type EventPriority,
} from 'react-reconciler/src/ReactEventPriorities';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
Expand Down Expand Up @@ -253,7 +254,19 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
return false;
}

export function getCurrentEventPriority(): EventPriority {
let currentUpdatePriority: EventPriority = NoEventPriority;
export function setCurrentUpdatePriority(newPriority: EventPriority): void {
currentUpdatePriority = newPriority;
}

export function getCurrentUpdatePriority(): EventPriority {
return currentUpdatePriority;
}

export function resolveUpdatePriority(): EventPriority {
if (currentUpdatePriority !== NoEventPriority) {
return currentUpdatePriority;
}
return DefaultEventPriority;
}

Expand Down
32 changes: 30 additions & 2 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import type {
import type {UpdateQueue} from 'react-reconciler/src/ReactFiberClassUpdateQueue';
import type {ReactNodeList} from 'shared/ReactTypes';
import type {RootTag} from 'react-reconciler/src/ReactRootTags';
import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';

import * as Scheduler from 'scheduler/unstable_mock';
import {REACT_FRAGMENT_TYPE, REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
import isArray from 'shared/isArray';
import {checkPropStringCoercion} from 'shared/CheckStringCoercion';
import {
NoEventPriority,
DefaultEventPriority,
IdleEventPriority,
ConcurrentRoot,
Expand Down Expand Up @@ -512,7 +514,13 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

resetAfterCommit(): void {},

getCurrentEventPriority() {
setCurrentUpdatePriority,
getCurrentUpdatePriority,

resolveUpdatePriority() {
if (currentUpdatePriority !== NoEventPriority) {
return currentUpdatePriority;
}
return currentEventPriority;
},

Expand Down Expand Up @@ -786,6 +794,15 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
const roots = new Map();
const DEFAULT_ROOT_ID = '<default>';

let currentUpdatePriority = NoEventPriority;
function setCurrentUpdatePriority(newPriority: EventPriority): void {
currentUpdatePriority = newPriority;
}

function getCurrentUpdatePriority(): EventPriority {
return currentUpdatePriority;
}

let currentEventPriority = DefaultEventPriority;

function createJSXElementForTestComparison(type, props) {
Expand Down Expand Up @@ -1223,7 +1240,18 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
return Scheduler.unstable_flushExpired();
},

unstable_runWithPriority: NoopRenderer.runWithPriority,
unstable_runWithPriority: function runWithPriority<T>(
priority: EventPriority,
fn: () => T,
): T {
const previousPriority = getCurrentUpdatePriority();
try {
setCurrentUpdatePriority(priority);
return fn();
} finally {
setCurrentUpdatePriority(previousPriority);
}
},

batchedUpdates: NoopRenderer.batchedUpdates,

Expand Down
Loading

0 comments on commit 8acc078

Please sign in to comment.