GeezerVison is showing github.com — Flush discrete passive effects before paint (#21150) · react/react@f8ef4ff
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2015,6 +2015,21 @@ function commitRootImpl(root, renderPriorityLevel) { | 2015 | 2015 | return null; | 2016 | 2016 | } | 2017 | 2017 |
| | 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 | + | 2018 | 2033 | // If layout work was scheduled, flush it now. | 2019 | 2034 | flushSyncCallbackQueue(); | 2020 | 2035 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2015,6 +2015,21 @@ function commitRootImpl(root, renderPriorityLevel) { | 2015 | 2015 | return null; | 2016 | 2016 | } | 2017 | 2017 |
| | 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 | + | 2018 | 2033 | // If layout work was scheduled, flush it now. | 2019 | 2034 | flushSyncCallbackQueue(); | 2020 | 2035 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactFlushSync-test.js +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,4 +97,73 @@ describe('ReactFlushSync', () => { | 97 | 97 | expect(Scheduler).toHaveYielded(['1, 1']); | 98 | 98 | expect(root).toMatchRenderedOutput('1, 1'); | 99 | 99 | }); | | 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 | + }); | 100 | 169 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,7 @@ let useDeferredValue; | 33 | 33 | let forwardRef; | 34 | 34 | let memo; | 35 | 35 | let act; | | 36 | +let ContinuousEventPriority; | 36 | 37 |
| 37 | 38 | describe('ReactHooksWithNoopRenderer', () => { | 38 | 39 | beforeEach(() => { | @@ -57,6 +58,8 @@ describe('ReactHooksWithNoopRenderer', () => { | 57 | 58 | useDeferredValue = React.unstable_useDeferredValue; | 58 | 59 | Suspense = React.Suspense; | 59 | 60 | act = ReactNoop.act; | | 61 | + ContinuousEventPriority = require('react-reconciler/constants') | | 62 | + .ContinuousEventPriority; | 60 | 63 |
| 61 | 64 | textCache = new Map(); | 62 | 65 |
| @@ -1351,10 +1354,10 @@ describe('ReactHooksWithNoopRenderer', () => { | 1351 | 1354 | expect(Scheduler).toFlushAndYieldThrough(['Child one render']); | 1352 | 1355 |
| 1353 | 1356 | // Schedule unmount for the parent that unmounts children with pending update. | 1354 | | - ReactNoop.flushSync(() => { | | 1357 | + ReactNoop.unstable_runWithPriority(ContinuousEventPriority, () => { | 1355 | 1358 | setParentState(false); | 1356 | 1359 | }); | 1357 | | - expect(Scheduler).toHaveYielded([ | | 1360 | + expect(Scheduler).toFlushUntilNextPaint([ | 1358 | 1361 | 'Parent false render', | 1359 | 1362 | 'Parent false commit', | 1360 | 1363 | ]); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/__tests__/ReactProfiler-test.internal.js +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3764,13 +3764,10 @@ describe('Profiler', () => { | 3764 | 3764 | ); | 3765 | 3765 | }); | 3766 | 3766 |
| 3767 | | - // Profiler tag causes passive effects to be scheduled, | 3768 | | - // so the interactions are still not completed. | 3769 | | - expect(Scheduler).toHaveYielded(['SecondComponent']); | 3770 | | - expect(onInteractionTraced).toHaveBeenCalledTimes(2); | 3771 | | - expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled(); | 3772 | | - expect(Scheduler).toFlushAndYieldThrough(['onPostCommit']); | 3773 | | - | | 3767 | + expect(Scheduler).toHaveYielded([ | | 3768 | + 'SecondComponent', | | 3769 | + 'onPostCommit', | | 3770 | + ]); | 3774 | 3771 | expect(onInteractionTraced).toHaveBeenCalledTimes(2); | 3775 | 3772 | expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes( | 3776 | 3773 | 1, | |
|