Skip to content

Commit

Permalink
Modifications after rebasing onto batching lanes
Browse files Browse the repository at this point in the history
  • Loading branch information
tyao1 committed Jan 5, 2023
1 parent 079f792 commit 6a82e51
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 274 deletions.
9 changes: 5 additions & 4 deletions packages/jest-react/src/internalAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ function flushActWork(resolve, reject) {
reject(error);
}

// Flush scheduled rAF.
if (global.flushRequestAnimationFrameQueue) {
global.flushRequestAnimationFrameQueue();
}

// If Scheduler yields while there's still work, it's so that we can
// unblock the main thread (e.g. for paint or for microtasks). Yield to
// the main thread and continue in a new task.
Expand All @@ -149,10 +154,6 @@ function flushActWork(resolve, reject) {
// $FlowFixMe: Flow doesn't know about global Jest object
jest.runOnlyPendingTimers();
if (Scheduler.unstable_hasPendingWork()) {
// Flush scheduled rAF.
if (global.flushRequestAnimationFrameQueue) {
global.flushRequestAnimationFrameQueue();
}
// Committing a fallback scheduled additional work. Continue flushing.
flushActWork(resolve, reject);
return;
Expand Down
13 changes: 9 additions & 4 deletions packages/react-dom-bindings/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,12 @@ type FrameAlignedTask = {|
let currentTask: FrameAlignedTask | null = null;
function performFrameAlignedWork() {
if (currentTask != null) {
const currentTaskForFlow = currentTask;
const task = currentTask.task;
localCancelAnimationFrame(currentTask.rafNode);
Scheduler.unstable_cancelCallback(currentTask.schedulerNode);
localCancelAnimationFrame(currentTaskForFlow.rafNode);
if (currentTaskForFlow.schedulerNode !== null) {
Scheduler.unstable_cancelCallback(currentTaskForFlow.schedulerNode);
}
currentTask = null;
if (task != null) {
task();
Expand Down Expand Up @@ -498,8 +501,10 @@ export function scheduleFrameAlignedTask(task: any): any {
}

export function cancelFrameAlignedTask(task: any) {
Scheduler.unstable_cancelCallback(task.schedulerNode);
task.schedulerNode = null;
if (task.schedulerNode) {
Scheduler.unstable_cancelCallback(task.schedulerNode);
task.schedulerNode = null;
}
// We don't cancel the rAF in case it gets re-used later.
// But clear the task so if it fires and shouldn't run, it won't.
task.task = null;
Expand Down
79 changes: 37 additions & 42 deletions packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling
it('Unknown update followed by default update is batched, scheduled in a rAF', () => {
it('Unknown update followed by default update is batched, scheduled in a rAF', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -575,7 +575,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
act(() => {
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand Down Expand Up @@ -634,7 +634,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling
it('Should re-use scheduled rAF, not cancel and schedule anew', () => {
it('Should re-use scheduled rAF, not cancel and schedule anew', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -647,7 +647,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
act(() => {
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand All @@ -672,7 +672,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling
it('Default update followed by an unknown update is batched, scheduled in a rAF', () => {
it('Default update followed by an unknown update is batched, scheduled in a rAF', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -685,7 +685,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
act(() => {
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand All @@ -709,7 +709,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling
it('Default update followed by unknown update is batched, scheduled in a task', () => {
it('Default update followed by unknown update is batched, scheduled in a task', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -722,7 +722,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
act(() => {
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand All @@ -745,7 +745,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling || !allowConcurrentByDefault
it('When allowConcurrentByDefault is enabled, unknown updates should not be time sliced', () => {
it('When allowConcurrentByDefault is enabled, unknown updates should not be time sliced', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -757,10 +757,8 @@ describe('ReactDOMFiberAsync', () => {
return <p ref={ref}>Count: {count}</p>;
}

const root = ReactDOMClient.createRoot(container, {
unstable_concurrentUpdatesByDefault: true,
});
act(() => {
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand All @@ -773,7 +771,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling || !allowConcurrentByDefault
it('When allowConcurrentByDefault is enabled, unknown updates should not be time sliced event with default first', () => {
it('When allowConcurrentByDefault is enabled, unknown updates should not be time sliced event with default first', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -785,10 +783,8 @@ describe('ReactDOMFiberAsync', () => {
return <p ref={ref}>Count: {count}</p>;
}

const root = ReactDOMClient.createRoot(container, {
unstable_concurrentUpdatesByDefault: true,
});
act(() => {
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand All @@ -804,7 +800,7 @@ describe('ReactDOMFiberAsync', () => {
});

// @gate enableFrameEndScheduling || !allowConcurrentByDefault
it('When allowConcurrentByDefault is enabled, unknown updates should not be time sliced event with default after', () => {
it('When allowConcurrentByDefault is enabled, unknown updates should not be time sliced event with default after', async () => {
let setState = null;
let counterRef = null;
function Counter() {
Expand All @@ -816,10 +812,8 @@ describe('ReactDOMFiberAsync', () => {
return <p ref={ref}>Count: {count}</p>;
}

const root = ReactDOMClient.createRoot(container, {
unstable_concurrentUpdatesByDefault: true,
});
act(() => {
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand Down Expand Up @@ -855,10 +849,8 @@ describe('ReactDOMFiberAsync', () => {
);
}

const root = ReactDOMClient.createRoot(container, {
unstable_concurrentUpdatesByDefault: true,
});
act(() => {
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand All @@ -867,21 +859,26 @@ describe('ReactDOMFiberAsync', () => {
setState(1);

// Dispatch a click event on the button.
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
counterRef.current.dispatchEvent(firstEvent);

await null;
await act(async () => {
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
counterRef.current.dispatchEvent(firstEvent);
});

expect(Scheduler).toHaveYielded(['Count: 1']);
expect(counterRef.current.textContent).toBe('Count: 1');
if (gate(flags => flags.enableUnifiedSyncLane)) {
expect(Scheduler).toHaveYielded(['Count: 2']);
expect(counterRef.current.textContent).toBe('Count: 2');
} else {
expect(Scheduler).toHaveYielded(['Count: 1']);
expect(counterRef.current.textContent).toBe('Count: 1');

global.flushRequestAnimationFrameQueue();
expect(Scheduler).toHaveYielded(['Count: 2']);
expect(counterRef.current.textContent).toBe('Count: 2');
global.flushRequestAnimationFrameQueue();
expect(Scheduler).toHaveYielded(['Count: 2']);
expect(counterRef.current.textContent).toBe('Count: 2');
}
});

// @gate enableFrameEndScheduling
// @gate enableFrameEndScheduling && enableUnifiedSyncLane
it('unknown updates should be rescheduled in rAF after suspending without a boundary', async () => {
let setState = null;
let setThrowing = null;
Expand Down Expand Up @@ -920,10 +917,8 @@ describe('ReactDOMFiberAsync', () => {
);
}

const root = ReactDOMClient.createRoot(container, {
unstable_concurrentUpdatesByDefault: true,
});
act(() => {
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Counter />);
});
expect(Scheduler).toHaveYielded(['Count: 0']);
Expand Down
7 changes: 6 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,12 @@ describe('ReactDOMRoot', () => {
expect(container.textContent).toEqual('a');

expect(Scheduler).toFlushAndYieldThrough(['b']);
if (gate(flags => flags.allowConcurrentByDefault)) {
if (
gate(
flags =>
flags.allowConcurrentByDefault && !flags.enableFrameEndScheduling,
)
) {
expect(container.textContent).toEqual('a');
} else {
expect(container.textContent).toEqual('b');
Expand Down
Loading

0 comments on commit 6a82e51

Please sign in to comment.