Skip to content

Commit 3499c34

Browse files
authored
Apply #20778 to new fork, too (#20782)
* Apply #20778 to new fork, too * Fix tests that use runWithPriority Where possible, I tried to rewrite in terms of an idiomatic API. For DOM tests, we should be dispatching an event with the desired priority level. For Idle updates (very unstable feature), probably need an unstable API like ReactDOM.unstable_IdleUpdates. Some of these fixes are not great, but we can replace them once we've landed the more of our planned changes to the layering between Scheduler, the reconciler, and the renderer.
1 parent 3d10eca commit 3499c34

17 files changed

+197
-197
lines changed

packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ function dispatchClickEvent(target) {
9292
return target.dispatchEvent(mouseOutEvent);
9393
}
9494

95+
// TODO: There's currently no React DOM API to opt into Idle priority updates,
96+
// and there's no native DOM event that maps to idle priority, so this is a
97+
// temporary workaround. Need something like ReactDOM.unstable_IdleUpdates.
98+
function TODO_scheduleIdleDOMSchedulerTask(fn) {
99+
Scheduler.unstable_runWithPriority(Scheduler.unstable_IdlePriority, () => {
100+
const prevEvent = window.event;
101+
window.event = {type: 'message'};
102+
try {
103+
fn();
104+
} finally {
105+
window.event = prevEvent;
106+
}
107+
});
108+
}
109+
95110
describe('ReactDOMServerSelectiveHydration', () => {
96111
beforeEach(() => {
97112
jest.resetModuleRegistry();
@@ -889,12 +904,10 @@ describe('ReactDOMServerSelectiveHydration', () => {
889904
expect(Scheduler).toFlushAndYieldThrough(['App', 'Commit']);
890905

891906
// Render an update at Idle priority that needs to update A.
892-
Scheduler.unstable_runWithPriority(
893-
Scheduler.unstable_IdlePriority,
894-
() => {
895-
root.render(<App a="AA" />);
896-
},
897-
);
907+
908+
TODO_scheduleIdleDOMSchedulerTask(() => {
909+
root.render(<App a="AA" />);
910+
});
898911

899912
// Start rendering. This will force the first boundary to hydrate
900913
// by scheduling it at one higher pri than Idle.

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

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const {
4141
deferredUpdates,
4242
unbatchedUpdates,
4343
discreteUpdates,
44+
idleUpdates,
4445
flushDiscreteUpdates,
4546
flushSync,
4647
flushPassiveEffects,

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

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const {
4141
deferredUpdates,
4242
unbatchedUpdates,
4343
discreteUpdates,
44+
idleUpdates,
4445
flushDiscreteUpdates,
4546
flushSync,
4647
flushPassiveEffects,

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
LegacyRoot,
2828
} from 'react-reconciler/src/ReactRootTags';
2929

30+
import {enableNativeEventPriorityInference} from 'shared/ReactFeatureFlags';
3031
import ReactSharedInternals from 'shared/ReactSharedInternals';
3132
import enqueueTask from 'shared/enqueueTask';
3233
const {IsSomeRendererActing} = ReactSharedInternals;
@@ -392,7 +393,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
392393
resetAfterCommit(): void {},
393394

394395
getCurrentEventPriority() {
395-
return NoopRenderer.DefaultEventPriority;
396+
return currentEventPriority;
396397
},
397398

398399
now: Scheduler.unstable_now,
@@ -587,6 +588,8 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
587588
const roots = new Map();
588589
const DEFAULT_ROOT_ID = '<default>';
589590

591+
let currentEventPriority = NoopRenderer.DefaultEventPriority;
592+
590593
function childToJSX(child, text) {
591594
if (text !== null) {
592595
return text;
@@ -925,6 +928,23 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
925928

926929
discreteUpdates: NoopRenderer.discreteUpdates,
927930

931+
idleUpdates<T>(fn: () => T): T {
932+
if (enableNativeEventPriorityInference) {
933+
const prevEventPriority = currentEventPriority;
934+
currentEventPriority = NoopRenderer.IdleEventPriority;
935+
try {
936+
fn();
937+
} finally {
938+
currentEventPriority = prevEventPriority;
939+
}
940+
} else {
941+
return Scheduler.unstable_runWithPriority(
942+
Scheduler.unstable_IdlePriority,
943+
fn,
944+
);
945+
}
946+
},
947+
928948
flushDiscreteUpdates: NoopRenderer.flushDiscreteUpdates,
929949

930950
flushSync(fn: () => mixed) {

packages/react-reconciler/src/ReactFiberLane.new.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const RetryLanePriority: LanePriority = 5;
7070
const SelectiveHydrationLanePriority: LanePriority = 4;
7171

7272
const IdleHydrationLanePriority: LanePriority = 3;
73-
const IdleLanePriority: LanePriority = 2;
73+
export const IdleLanePriority: LanePriority = 2;
7474

7575
const OffscreenLanePriority: LanePriority = 1;
7676

@@ -275,6 +275,7 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes {
275275

276276
// Check if any work has expired.
277277
if (expiredLanes !== NoLanes) {
278+
// TODO: Should entangle with SyncLane
278279
nextLanes = expiredLanes;
279280
nextLanePriority = return_highestLanePriority = SyncLanePriority;
280281
} else {

packages/react-reconciler/src/ReactFiberLane.old.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const RetryLanePriority: LanePriority = 5;
7070
const SelectiveHydrationLanePriority: LanePriority = 4;
7171

7272
const IdleHydrationLanePriority: LanePriority = 3;
73-
const IdleLanePriority: LanePriority = 2;
73+
export const IdleLanePriority: LanePriority = 2;
7474

7575
const OffscreenLanePriority: LanePriority = 1;
7676

@@ -275,6 +275,7 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes {
275275

276276
// Check if any work has expired.
277277
if (expiredLanes !== NoLanes) {
278+
// TODO: Should entangle with SyncLane
278279
nextLanes = expiredLanes;
279280
nextLanePriority = return_highestLanePriority = SyncLanePriority;
280281
} else {

packages/react-reconciler/src/ReactFiberReconciler.js

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
DefaultEventPriority as DefaultEventPriority_old,
5656
DiscreteEventPriority as DiscreteEventPriority_old,
5757
ContinuousEventPriority as ContinuousEventPriority_old,
58+
IdleEventPriority as IdleEventPriority_old,
5859
} from './ReactFiberReconciler.old';
5960

6061
import {
@@ -98,6 +99,7 @@ import {
9899
DefaultEventPriority as DefaultEventPriority_new,
99100
DiscreteEventPriority as DiscreteEventPriority_new,
100101
ContinuousEventPriority as ContinuousEventPriority_new,
102+
IdleEventPriority as IdleEventPriority_new,
101103
} from './ReactFiberReconciler.new';
102104

103105
export const createContainer = enableNewReconciler
@@ -183,6 +185,9 @@ export const DiscreteEventPriority = enableNewReconciler
183185
export const ContinuousEventPriority = enableNewReconciler
184186
? ContinuousEventPriority_new
185187
: ContinuousEventPriority_old;
188+
export const IdleEventPriority = enableNewReconciler
189+
? IdleEventPriority_new
190+
: IdleEventPriority_old;
186191

187192
//TODO: "psuedo" is spelled "pseudo"
188193
export const createHasPsuedoClassSelector = enableNewReconciler

packages/react-reconciler/src/ReactFiberReconciler.new.js

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export {
101101
InputDiscreteLanePriority as DiscreteEventPriority,
102102
InputContinuousLanePriority as ContinuousEventPriority,
103103
DefaultLanePriority as DefaultEventPriority,
104+
IdleLanePriority as IdleEventPriority,
104105
} from './ReactFiberLane.new';
105106

106107
export {registerMutableSourceForHydration} from './ReactMutableSource.new';

packages/react-reconciler/src/ReactFiberReconciler.old.js

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export {
101101
InputDiscreteLanePriority as DiscreteEventPriority,
102102
InputContinuousLanePriority as ContinuousEventPriority,
103103
DefaultLanePriority as DefaultEventPriority,
104+
IdleLanePriority as IdleEventPriority,
104105
} from './ReactFiberLane.old';
105106

106107
export {registerMutableSourceForHydration} from './ReactMutableSource.new';

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,7 @@ export function requestUpdateLane(fiber: Fiber): Lane {
465465
} else {
466466
if (enableNativeEventPriorityInference) {
467467
const eventLanePriority = getCurrentEventPriority();
468-
if (eventLanePriority === DefaultLanePriority) {
469-
// TODO: move this case into the ReactDOM host config.
470-
const schedulerLanePriority = schedulerPriorityToLanePriority(
471-
schedulerPriority,
472-
);
473-
lane = findUpdateLane(schedulerLanePriority, currentEventWipLanes);
474-
} else {
475-
lane = findUpdateLane(eventLanePriority, currentEventWipLanes);
476-
}
468+
lane = findUpdateLane(eventLanePriority, currentEventWipLanes);
477469
} else {
478470
const schedulerLanePriority = schedulerPriorityToLanePriority(
479471
schedulerPriority,

0 commit comments

Comments
 (0)