Skip to content

Commit

Permalink
Apply facebook#20778 to new fork, too (facebook#20782)
Browse files Browse the repository at this point in the history
* Apply facebook#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.
  • Loading branch information
acdlite authored and koto committed Jun 15, 2021
1 parent 89d9d7c commit 8028efe
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ function dispatchClickEvent(target) {
return target.dispatchEvent(mouseOutEvent);
}

// TODO: There's currently no React DOM API to opt into Idle priority updates,
// and there's no native DOM event that maps to idle priority, so this is a
// temporary workaround. Need something like ReactDOM.unstable_IdleUpdates.
function TODO_scheduleIdleDOMSchedulerTask(fn) {
Scheduler.unstable_runWithPriority(Scheduler.unstable_IdlePriority, () => {
const prevEvent = window.event;
window.event = {type: 'message'};
try {
fn();
} finally {
window.event = prevEvent;
}
});
}

describe('ReactDOMServerSelectiveHydration', () => {
beforeEach(() => {
jest.resetModuleRegistry();
Expand Down Expand Up @@ -889,12 +904,10 @@ describe('ReactDOMServerSelectiveHydration', () => {
expect(Scheduler).toFlushAndYieldThrough(['App', 'Commit']);

// Render an update at Idle priority that needs to update A.
Scheduler.unstable_runWithPriority(
Scheduler.unstable_IdlePriority,
() => {
root.render(<App a="AA" />);
},
);

TODO_scheduleIdleDOMSchedulerTask(() => {
root.render(<App a="AA" />);
});

// Start rendering. This will force the first boundary to hydrate
// by scheduling it at one higher pri than Idle.
Expand Down
1 change: 1 addition & 0 deletions packages/react-noop-renderer/src/ReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const {
deferredUpdates,
unbatchedUpdates,
discreteUpdates,
idleUpdates,
flushDiscreteUpdates,
flushSync,
flushPassiveEffects,
Expand Down
1 change: 1 addition & 0 deletions packages/react-noop-renderer/src/ReactNoopPersistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const {
deferredUpdates,
unbatchedUpdates,
discreteUpdates,
idleUpdates,
flushDiscreteUpdates,
flushSync,
flushPassiveEffects,
Expand Down
22 changes: 21 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,7 @@ import {
LegacyRoot,
} from 'react-reconciler/src/ReactRootTags';

import {enableNativeEventPriorityInference} from 'shared/ReactFeatureFlags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import enqueueTask from 'shared/enqueueTask';
const {IsSomeRendererActing} = ReactSharedInternals;
Expand Down Expand Up @@ -392,7 +393,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
resetAfterCommit(): void {},

getCurrentEventPriority() {
return NoopRenderer.DefaultEventPriority;
return currentEventPriority;
},

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

let currentEventPriority = NoopRenderer.DefaultEventPriority;

function childToJSX(child, text) {
if (text !== null) {
return text;
Expand Down Expand Up @@ -925,6 +928,23 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

discreteUpdates: NoopRenderer.discreteUpdates,

idleUpdates<T>(fn: () => T): T {
if (enableNativeEventPriorityInference) {
const prevEventPriority = currentEventPriority;
currentEventPriority = NoopRenderer.IdleEventPriority;
try {
fn();
} finally {
currentEventPriority = prevEventPriority;
}
} else {
return Scheduler.unstable_runWithPriority(
Scheduler.unstable_IdlePriority,
fn,
);
}
},

flushDiscreteUpdates: NoopRenderer.flushDiscreteUpdates,

flushSync(fn: () => mixed) {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-reconciler/src/ReactFiberLane.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const RetryLanePriority: LanePriority = 5;
const SelectiveHydrationLanePriority: LanePriority = 4;

const IdleHydrationLanePriority: LanePriority = 3;
const IdleLanePriority: LanePriority = 2;
export const IdleLanePriority: LanePriority = 2;

const OffscreenLanePriority: LanePriority = 1;

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

// Check if any work has expired.
if (expiredLanes !== NoLanes) {
// TODO: Should entangle with SyncLane
nextLanes = expiredLanes;
nextLanePriority = return_highestLanePriority = SyncLanePriority;
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-reconciler/src/ReactFiberLane.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const RetryLanePriority: LanePriority = 5;
const SelectiveHydrationLanePriority: LanePriority = 4;

const IdleHydrationLanePriority: LanePriority = 3;
const IdleLanePriority: LanePriority = 2;
export const IdleLanePriority: LanePriority = 2;

const OffscreenLanePriority: LanePriority = 1;

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

// Check if any work has expired.
if (expiredLanes !== NoLanes) {
// TODO: Should entangle with SyncLane
nextLanes = expiredLanes;
nextLanePriority = return_highestLanePriority = SyncLanePriority;
} else {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
DefaultEventPriority as DefaultEventPriority_old,
DiscreteEventPriority as DiscreteEventPriority_old,
ContinuousEventPriority as ContinuousEventPriority_old,
IdleEventPriority as IdleEventPriority_old,
} from './ReactFiberReconciler.old';

import {
Expand Down Expand Up @@ -98,6 +99,7 @@ import {
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 @@ -183,6 +185,9 @@ export const DiscreteEventPriority = enableNewReconciler
export const ContinuousEventPriority = enableNewReconciler
? ContinuousEventPriority_new
: ContinuousEventPriority_old;
export const IdleEventPriority = enableNewReconciler
? IdleEventPriority_new
: IdleEventPriority_old;

//TODO: "psuedo" is spelled "pseudo"
export const createHasPsuedoClassSelector = enableNewReconciler
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactFiberReconciler.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export {
InputDiscreteLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.new';

export {registerMutableSourceForHydration} from './ReactMutableSource.new';
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactFiberReconciler.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export {
InputDiscreteLanePriority as DiscreteEventPriority,
InputContinuousLanePriority as ContinuousEventPriority,
DefaultLanePriority as DefaultEventPriority,
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.old';

export {registerMutableSourceForHydration} from './ReactMutableSource.new';
Expand Down
10 changes: 1 addition & 9 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,7 @@ export function requestUpdateLane(fiber: Fiber): Lane {
} else {
if (enableNativeEventPriorityInference) {
const eventLanePriority = getCurrentEventPriority();
if (eventLanePriority === DefaultLanePriority) {
// TODO: move this case into the ReactDOM host config.
const schedulerLanePriority = schedulerPriorityToLanePriority(
schedulerPriority,
);
lane = findUpdateLane(schedulerLanePriority, currentEventWipLanes);
} else {
lane = findUpdateLane(eventLanePriority, currentEventWipLanes);
}
lane = findUpdateLane(eventLanePriority, currentEventWipLanes);
} else {
const schedulerLanePriority = schedulerPriorityToLanePriority(
schedulerPriority,
Expand Down
Loading

0 comments on commit 8028efe

Please sign in to comment.