Skip to content

Commit

Permalink
[Fiber] Move updatePriority tracking to renderers
Browse files Browse the repository at this point in the history
Current 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.
  • Loading branch information
gnoff committed Apr 4, 2024
1 parent 6090cab commit d43dd65
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 46 deletions.
17 changes: 16 additions & 1 deletion 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,6 +341,16 @@ export function shouldSetTextContent(type, props) {
);
}

let currentUpdatePriority: EventPriority = NoEventPriority;

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

export function getCurrentUpdatePriority() {
return currentUpdatePriority;
}

export function getCurrentEventPriority() {
return DefaultEventPriority;
}
Expand Down
32 changes: 32 additions & 0 deletions packages/react-dom-bindings/src/client/ReactDOMUpdatePriority.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 {NoEventPriority} from 'react-reconciler/src/ReactEventPriorities';

let currentUpdatePriority: EventPriority = NoEventPriority;

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

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

export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
const previousPriority = getCurrentUpdatePriority();
try {
setCurrentUpdatePriority(priority);
return fn();
} finally {
setCurrentUpdatePriority(previousPriority);
}
}
4 changes: 4 additions & 0 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';
import hasOwnProperty from 'shared/hasOwnProperty';
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';

export {
setCurrentUpdatePriority,
getCurrentUpdatePriority,
} from './ReactDOMUpdatePriority';
import {
precacheFiberNode,
updateFiberProps,
Expand Down
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
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
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
12 changes: 12 additions & 0 deletions 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,6 +312,17 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
return false;
}

let currentUpdatePriority: EventPriority = NoEventPriority;
export function setCurrentUpdatePriority(
newEventPriority: EventPriority,
): void {
currentUpdatePriority = newEventPriority;
}

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

export function getCurrentEventPriority(): EventPriority {
const currentEventPriority = fabricGetCurrentEventPriority
? fabricGetCurrentEventPriority()
Expand Down
10 changes: 10 additions & 0 deletions 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,6 +254,15 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
return false;
}

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

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

export function getCurrentEventPriority(): EventPriority {
return DefaultEventPriority;
}
Expand Down
27 changes: 26 additions & 1 deletion 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 @@ -508,6 +510,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

resetAfterCommit(): void {},

setCurrentUpdatePriority,
getCurrentUpdatePriority,

getCurrentEventPriority() {
return currentEventPriority;
},
Expand Down Expand Up @@ -782,6 +787,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 @@ -1211,7 +1225,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
21 changes: 1 addition & 20 deletions packages/react-reconciler/src/ReactEventPriorities.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,12 @@ import {

export opaque type EventPriority = Lane;

export const NoEventPriority: EventPriority = NoLane;
export const DiscreteEventPriority: EventPriority = SyncLane;
export const ContinuousEventPriority: EventPriority = InputContinuousLane;
export const DefaultEventPriority: EventPriority = DefaultLane;
export const IdleEventPriority: EventPriority = IdleLane;

let currentUpdatePriority: EventPriority = NoLane;

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

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

export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
const previousPriority = currentUpdatePriority;
try {
currentUpdatePriority = priority;
return fn();
} finally {
currentUpdatePriority = previousPriority;
}
}

export function higherEventPriority(
a: EventPriority,
b: EventPriority,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ function updateOffscreenComponent(
// pending work. We can't read `childLanes` from the current Offscreen
// fiber because we reset it when it was deferred; however, we can read
// the pending lanes from the child fibers.
let currentChildLanes = NoLanes;
let currentChildLanes: Lanes = NoLanes;
while (currentChild !== null) {
currentChildLanes = mergeLanes(
mergeLanes(currentChildLanes, currentChild.lanes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ export function processUpdateQueue<State>(
let newState = queue.baseState;
// TODO: Don't need to accumulate this. Instead, we can remove renderLanes
// from the original lanes.
let newLanes = NoLanes;
let newLanes: Lanes = NoLanes;

let newBaseState = null;
let newFirstBaseUpdate = null;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ function bubbleProperties(completedWork: Fiber) {
completedWork.alternate !== null &&
completedWork.alternate.child === completedWork.child;

let newChildLanes = NoLanes;
let newChildLanes: Lanes = NoLanes;
let subtreeFlags = NoFlags;

if (!didBailout) {
Expand Down
8 changes: 5 additions & 3 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import type {HookFlags} from './ReactHookEffectTags';
import type {Flags} from './ReactFiberFlags';
import type {TransitionStatus} from './ReactFiberConfig';

import {NotPendingTransition as NoPendingHostTransition} from './ReactFiberConfig';
import {
NotPendingTransition as NoPendingHostTransition,
setCurrentUpdatePriority,
getCurrentUpdatePriority,
} from './ReactFiberConfig';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
enableDebugTracing,
Expand Down Expand Up @@ -74,8 +78,6 @@ import {
} from './ReactFiberLane';
import {
ContinuousEventPriority,
getCurrentUpdatePriority,
setCurrentUpdatePriority,
higherEventPriority,
} from './ReactEventPriorities';
import {readContext, checkIfContextChanged} from './ReactFiberNewContext';
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberLane.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes {
return NoLanes;
}

let nextLanes = NoLanes;
let nextLanes: Lanes = NoLanes;

const suspendedLanes = root.suspendedLanes;
const pingedLanes = root.pingedLanes;
Expand Down
8 changes: 2 additions & 6 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFrom
import isArray from 'shared/isArray';
import {enableSchedulingProfiler} from 'shared/ReactFeatureFlags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {getPublicInstance} from './ReactFiberConfig';
import {getPublicInstance, getCurrentUpdatePriority} from './ReactFiberConfig';
import {
findCurrentUnmaskedContext,
processChildContext,
Expand Down Expand Up @@ -86,10 +86,6 @@ import {
getHighestPriorityPendingLanes,
higherPriorityLane,
} from './ReactFiberLane';
import {
getCurrentUpdatePriority,
runWithPriority,
} from './ReactEventPriorities';
import {
scheduleRefresh,
scheduleRoot,
Expand Down Expand Up @@ -525,7 +521,7 @@ export function attemptHydrationAtCurrentPriority(fiber: Fiber): void {
markRetryLaneIfNotHydrated(fiber, lane);
}

export {getCurrentUpdatePriority, runWithPriority};
export {getCurrentUpdatePriority};

export {findHostInstance};

Expand Down
Loading

0 comments on commit d43dd65

Please sign in to comment.