Skip to content

Commit d43dd65

Browse files
committed
[Fiber] Move updatePriority tracking to renderers
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.
1 parent 6090cab commit d43dd65

21 files changed

+148
-46
lines changed

packages/react-art/src/ReactFiberConfigART.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
9+
810
import Transform from 'art/core/transform';
911
import Mode from 'art/modes/current';
1012

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

13-
import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';
15+
import {
16+
DefaultEventPriority,
17+
NoEventPriority,
18+
} from 'react-reconciler/src/ReactEventPriorities';
1419

1520
const pooledTransform = new Transform();
1621

@@ -336,6 +341,16 @@ export function shouldSetTextContent(type, props) {
336341
);
337342
}
338343

344+
let currentUpdatePriority: EventPriority = NoEventPriority;
345+
346+
export function setCurrentUpdatePriority(newPriority: EventPriority) {
347+
currentUpdatePriority = newPriority;
348+
}
349+
350+
export function getCurrentUpdatePriority() {
351+
return currentUpdatePriority;
352+
}
353+
339354
export function getCurrentEventPriority() {
340355
return DefaultEventPriority;
341356
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
11+
12+
import {NoEventPriority} from 'react-reconciler/src/ReactEventPriorities';
13+
14+
let currentUpdatePriority: EventPriority = NoEventPriority;
15+
16+
export function setCurrentUpdatePriority(newPriority: EventPriority): void {
17+
currentUpdatePriority = newPriority;
18+
}
19+
20+
export function getCurrentUpdatePriority(): EventPriority {
21+
return currentUpdatePriority;
22+
}
23+
24+
export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
25+
const previousPriority = getCurrentUpdatePriority();
26+
try {
27+
setCurrentUpdatePriority(priority);
28+
return fn();
29+
} finally {
30+
setCurrentUpdatePriority(previousPriority);
31+
}
32+
}

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities';
3434
import hasOwnProperty from 'shared/hasOwnProperty';
3535
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';
3636

37+
export {
38+
setCurrentUpdatePriority,
39+
getCurrentUpdatePriority,
40+
} from './ReactDOMUpdatePriority';
3741
import {
3842
precacheFiberNode,
3943
updateFiberProps,

packages/react-dom-bindings/src/events/ReactDOMEventListener.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import {
3434
} from '../client/ReactDOMComponentTree';
3535

3636
import {dispatchEventForPluginEventSystem} from './DOMPluginEventSystem';
37+
import {
38+
getCurrentUpdatePriority,
39+
setCurrentUpdatePriority,
40+
} from '../client/ReactDOMUpdatePriority';
3741

3842
import {
3943
getCurrentPriorityLevel as getCurrentSchedulerPriorityLevel,
@@ -48,8 +52,6 @@ import {
4852
ContinuousEventPriority,
4953
DefaultEventPriority,
5054
IdleEventPriority,
51-
getCurrentUpdatePriority,
52-
setCurrentUpdatePriority,
5355
} from 'react-reconciler/src/ReactEventPriorities';
5456
import ReactSharedInternals from 'shared/ReactSharedInternals';
5557
import {isRootDehydrated} from 'react-reconciler/src/ReactFiberShellHydration';

packages/react-dom-bindings/src/events/ReactDOMEventReplaying.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ import {HostRoot, SuspenseComponent} from 'react-reconciler/src/ReactWorkTags';
3737
import {isHigherEventPriority} from 'react-reconciler/src/ReactEventPriorities';
3838
import {isRootDehydrated} from 'react-reconciler/src/ReactFiberShellHydration';
3939
import {dispatchReplayedFormAction} from './plugins/FormActionEventPlugin';
40+
import {
41+
getCurrentUpdatePriority,
42+
runWithPriority as attemptHydrationAtPriority,
43+
} from '../client/ReactDOMUpdatePriority';
4044

4145
import {
4246
attemptContinuousHydration,
4347
attemptHydrationAtCurrentPriority,
4448
} from 'react-reconciler/src/ReactFiberReconciler';
45-
import {
46-
runWithPriority as attemptHydrationAtPriority,
47-
getCurrentUpdatePriority,
48-
} from 'react-reconciler/src/ReactEventPriorities';
4949

5050
// TODO: Upgrade this definition once we're on a newer version of Flow that
5151
// has this definition built-in.

packages/react-dom/src/client/ReactDOM.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import {
2020
isValidContainer,
2121
} from './ReactDOMRoot';
2222
import {createEventHandle} from 'react-dom-bindings/src/client/ReactDOMEventHandle';
23+
import {runWithPriority} from 'react-dom-bindings/src/client/ReactDOMUpdatePriority';
2324

2425
import {
2526
flushSync as flushSyncWithoutWarningIfAlreadyRendering,
2627
isAlreadyRendering,
2728
injectIntoDevTools,
2829
findHostInstance,
2930
} from 'react-reconciler/src/ReactFiberReconciler';
30-
import {runWithPriority} from 'react-reconciler/src/ReactEventPriorities';
3131
import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal';
3232
import {canUseDOM} from 'shared/ExecutionEnvironment';
3333
import ReactVersion from 'shared/ReactVersion';

packages/react-native-renderer/src/ReactFiberConfigFabric.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
import {create, diff} from './ReactNativeAttributePayload';
1616
import {dispatchEvent} from './ReactFabricEventEmitter';
1717
import {
18+
NoEventPriority,
1819
DefaultEventPriority,
1920
DiscreteEventPriority,
2021
type EventPriority,
@@ -311,6 +312,17 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
311312
return false;
312313
}
313314

315+
let currentUpdatePriority: EventPriority = NoEventPriority;
316+
export function setCurrentUpdatePriority(
317+
newEventPriority: EventPriority,
318+
): void {
319+
currentUpdatePriority = newEventPriority;
320+
}
321+
322+
export function getCurrentUpdatePriority(): EventPriority {
323+
return currentUpdatePriority;
324+
}
325+
314326
export function getCurrentEventPriority(): EventPriority {
315327
const currentEventPriority = fabricGetCurrentEventPriority
316328
? fabricGetCurrentEventPriority()

packages/react-native-renderer/src/ReactFiberConfigNative.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent';
2626

2727
import {
2828
DefaultEventPriority,
29+
NoEventPriority,
2930
type EventPriority,
3031
} from 'react-reconciler/src/ReactEventPriorities';
3132
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
@@ -253,6 +254,15 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
253254
return false;
254255
}
255256

257+
let currentUpdatePriority: EventPriority = NoEventPriority;
258+
export function setCurrentUpdatePriority(newPriority: EventPriority): void {
259+
currentUpdatePriority = newPriority;
260+
}
261+
262+
export function getCurrentUpdatePriority(): EventPriority {
263+
return currentUpdatePriority;
264+
}
265+
256266
export function getCurrentEventPriority(): EventPriority {
257267
return DefaultEventPriority;
258268
}

packages/react-noop-renderer/src/createReactNoop.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import type {
2121
import type {UpdateQueue} from 'react-reconciler/src/ReactFiberClassUpdateQueue';
2222
import type {ReactNodeList} from 'shared/ReactTypes';
2323
import type {RootTag} from 'react-reconciler/src/ReactRootTags';
24+
import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
2425

2526
import * as Scheduler from 'scheduler/unstable_mock';
2627
import {REACT_FRAGMENT_TYPE, REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
2728
import isArray from 'shared/isArray';
2829
import {checkPropStringCoercion} from 'shared/CheckStringCoercion';
2930
import {
31+
NoEventPriority,
3032
DefaultEventPriority,
3133
IdleEventPriority,
3234
ConcurrentRoot,
@@ -508,6 +510,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
508510

509511
resetAfterCommit(): void {},
510512

513+
setCurrentUpdatePriority,
514+
getCurrentUpdatePriority,
515+
511516
getCurrentEventPriority() {
512517
return currentEventPriority;
513518
},
@@ -782,6 +787,15 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
782787
const roots = new Map();
783788
const DEFAULT_ROOT_ID = '<default>';
784789

790+
let currentUpdatePriority = NoEventPriority;
791+
function setCurrentUpdatePriority(newPriority: EventPriority): void {
792+
currentUpdatePriority = newPriority;
793+
}
794+
795+
function getCurrentUpdatePriority(): EventPriority {
796+
return currentUpdatePriority;
797+
}
798+
785799
let currentEventPriority = DefaultEventPriority;
786800

787801
function createJSXElementForTestComparison(type, props) {
@@ -1211,7 +1225,18 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
12111225
return Scheduler.unstable_flushExpired();
12121226
},
12131227

1214-
unstable_runWithPriority: NoopRenderer.runWithPriority,
1228+
unstable_runWithPriority: function runWithPriority<T>(
1229+
priority: EventPriority,
1230+
fn: () => T,
1231+
): T {
1232+
const previousPriority = getCurrentUpdatePriority();
1233+
try {
1234+
setCurrentUpdatePriority(priority);
1235+
return fn();
1236+
} finally {
1237+
setCurrentUpdatePriority(previousPriority);
1238+
}
1239+
},
12151240

12161241
batchedUpdates: NoopRenderer.batchedUpdates,
12171242

packages/react-reconciler/src/ReactEventPriorities.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,12 @@ import {
2121

2222
export opaque type EventPriority = Lane;
2323

24+
export const NoEventPriority: EventPriority = NoLane;
2425
export const DiscreteEventPriority: EventPriority = SyncLane;
2526
export const ContinuousEventPriority: EventPriority = InputContinuousLane;
2627
export const DefaultEventPriority: EventPriority = DefaultLane;
2728
export const IdleEventPriority: EventPriority = IdleLane;
2829

29-
let currentUpdatePriority: EventPriority = NoLane;
30-
31-
export function getCurrentUpdatePriority(): EventPriority {
32-
return currentUpdatePriority;
33-
}
34-
35-
export function setCurrentUpdatePriority(newPriority: EventPriority) {
36-
currentUpdatePriority = newPriority;
37-
}
38-
39-
export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
40-
const previousPriority = currentUpdatePriority;
41-
try {
42-
currentUpdatePriority = priority;
43-
return fn();
44-
} finally {
45-
currentUpdatePriority = previousPriority;
46-
}
47-
}
48-
4930
export function higherEventPriority(
5031
a: EventPriority,
5132
b: EventPriority,

0 commit comments

Comments
 (0)