Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: flushSync should flush passive effects if they were the result of a sync update #21124

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,14 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
}

export function flushSync<A, R>(fn: A => R, a: A): R {
if (includesSomeLane(pendingPassiveEffectsLanes, SyncLane)) {
// If there are pending passive effects with sync priority, flush them now,
// so the callback can observe the result.
if ((executionContext & (RenderContext | CommitContext)) === NoContext) {
flushPassiveEffects();
}
}

const prevExecutionContext = executionContext;
executionContext |= BatchedContext;

Expand Down
8 changes: 8 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,14 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
}

export function flushSync<A, R>(fn: A => R, a: A): R {
if (includesSomeLane(pendingPassiveEffectsLanes, SyncLane)) {
// If there are pending passive effects with sync priority, flush them now,
// so the callback can observe the result.
if ((executionContext & (RenderContext | CommitContext)) === NoContext) {
flushPassiveEffects();
}
}

const prevExecutionContext = executionContext;
executionContext |= BatchedContext;

Expand Down
51 changes: 51 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactFlushSync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,55 @@ describe('ReactFlushSync', () => {
});
expect(root).toMatchRenderedOutput('1, 1');
});

test('flushes pending passive effects if they were the result of a sync render', async () => {
function App() {
useEffect(() => {
Scheduler.unstable_yieldValue('Effect');
}, []);
return <Text text="Child" />;
}

const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
ReactNoop.flushSync(() => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded([
'Child',
// Passive effect hasn't fired yet
]);
expect(root).toMatchRenderedOutput('Child');

// Because the pending effect was the result of a sync update, calling
// flushSync should flush it.
ReactNoop.flushSync();
expect(Scheduler).toHaveYielded(['Effect']);
});
});

test("does not flush pending passive effects if they weren't the result of a sync render", async () => {
function App() {
useEffect(() => {
Scheduler.unstable_yieldValue('Effect');
}, []);
return <Text text="Child" />;
}

const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
expect(Scheduler).toFlushUntilNextPaint([
'Child',
// Passive effect hasn't fired yet
]);
expect(root).toMatchRenderedOutput('Child');

// Because the pending effect was not the result of a sync update, calling
// flushSync should not flush it.
ReactNoop.flushSync();
expect(Scheduler).toHaveYielded([]);
});
expect(Scheduler).toHaveYielded(['Effect']);
});
});