Skip to content

Commit 7e30d63

Browse files
committed
Flush discrete passive effects before paint
If a discrete render results in passive effects, we should flush them synchronously at the end of the current task so that the result is immediately observable. For example, if a passive effect adds an event listener, the listener will be added before the next input. We don't need to do this for effects that don't have discrete/sync priority, because we assume they are not order-dependent and do not need to be observed by external systems. For legacy mode, we will maintain the existing behavior, since it hasn't been reported as an issue, and we'd have to do additional work to distinguish "legacy default sync" from "discrete sync" to prevent all passive effects from being treated this way.
1 parent b48b38a commit 7e30d63

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,21 @@ function commitRootImpl(root, renderPriorityLevel) {
20152015
return null;
20162016
}
20172017

2018+
// If the passive effects are the result of a discrete render, flush them
2019+
// synchronously at the end of the current task so that the result is
2020+
// immediately observable. Otherwise, we assume that they are not
2021+
// order-dependent and do not need to be observed by external systems, so we
2022+
// can wait until after paint.
2023+
// TODO: We can optimize this by not scheduling the callback earlier. Since we
2024+
// currently schedule the callback in multiple places, will wait until those
2025+
// are consolidated.
2026+
if (
2027+
includesSomeLane(pendingPassiveEffectsLanes, SyncLane) &&
2028+
root.tag !== LegacyRoot
2029+
) {
2030+
flushPassiveEffects();
2031+
}
2032+
20182033
// If layout work was scheduled, flush it now.
20192034
flushSyncCallbackQueue();
20202035

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,21 @@ function commitRootImpl(root, renderPriorityLevel) {
20152015
return null;
20162016
}
20172017

2018+
// If the passive effects are the result of a discrete render, flush them
2019+
// synchronously at the end of the current task so that the result is
2020+
// immediately observable. Otherwise, we assume that they are not
2021+
// order-dependent and do not need to be observed by external systems, so we
2022+
// can wait until after paint.
2023+
// TODO: We can optimize this by not scheduling the callback earlier. Since we
2024+
// currently schedule the callback in multiple places, will wait until those
2025+
// are consolidated.
2026+
if (
2027+
includesSomeLane(pendingPassiveEffectsLanes, SyncLane) &&
2028+
root.tag !== LegacyRoot
2029+
) {
2030+
flushPassiveEffects();
2031+
}
2032+
20182033
// If layout work was scheduled, flush it now.
20192034
flushSyncCallbackQueue();
20202035

packages/react-reconciler/src/__tests__/ReactFlushSync-test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,73 @@ describe('ReactFlushSync', () => {
9797
expect(Scheduler).toHaveYielded(['1, 1']);
9898
expect(root).toMatchRenderedOutput('1, 1');
9999
});
100+
101+
test('flushes passive effects synchronously when they are the result of a sync render', async () => {
102+
function App() {
103+
useEffect(() => {
104+
Scheduler.unstable_yieldValue('Effect');
105+
}, []);
106+
return <Text text="Child" />;
107+
}
108+
109+
const root = ReactNoop.createRoot();
110+
await ReactNoop.act(async () => {
111+
ReactNoop.flushSync(() => {
112+
root.render(<App />);
113+
});
114+
expect(Scheduler).toHaveYielded([
115+
'Child',
116+
// Because the pending effect was the result of a sync update, calling
117+
// flushSync should flush it.
118+
'Effect',
119+
]);
120+
expect(root).toMatchRenderedOutput('Child');
121+
});
122+
});
123+
124+
test('do not flush passive effects synchronously in legacy mode', async () => {
125+
function App() {
126+
useEffect(() => {
127+
Scheduler.unstable_yieldValue('Effect');
128+
}, []);
129+
return <Text text="Child" />;
130+
}
131+
132+
const root = ReactNoop.createLegacyRoot();
133+
await ReactNoop.act(async () => {
134+
ReactNoop.flushSync(() => {
135+
root.render(<App />);
136+
});
137+
expect(Scheduler).toHaveYielded([
138+
'Child',
139+
// Because we're in legacy mode, we shouldn't have flushed the passive
140+
// effects yet.
141+
]);
142+
expect(root).toMatchRenderedOutput('Child');
143+
});
144+
// Effect flushes after paint.
145+
expect(Scheduler).toHaveYielded(['Effect']);
146+
});
147+
148+
test("do not flush passive effects synchronously when they aren't the result of a sync render", async () => {
149+
function App() {
150+
useEffect(() => {
151+
Scheduler.unstable_yieldValue('Effect');
152+
}, []);
153+
return <Text text="Child" />;
154+
}
155+
156+
const root = ReactNoop.createRoot();
157+
await ReactNoop.act(async () => {
158+
root.render(<App />);
159+
expect(Scheduler).toFlushUntilNextPaint([
160+
'Child',
161+
// Because the passive effect was not the result of a sync update, it
162+
// should not flush before paint.
163+
]);
164+
expect(root).toMatchRenderedOutput('Child');
165+
});
166+
// Effect flushes after paint.
167+
expect(Scheduler).toHaveYielded(['Effect']);
168+
});
100169
});

0 commit comments

Comments
 (0)