GeezerVison is showing github.com — Support nesting of startTransition and flushSync (alt) (#21149) · react/react@b48b38a
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-dom/src/events/ReactDOMEventListener.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,9 @@ import { | 57 | 57 | getCurrentUpdatePriority, | 58 | 58 | setCurrentUpdatePriority, | 59 | 59 | } from 'react-reconciler/src/ReactEventPriorities'; | | 60 | +import ReactSharedInternals from 'shared/ReactSharedInternals'; | | 61 | + | | 62 | +const {ReactCurrentBatchConfig} = ReactSharedInternals; | 60 | 63 |
| 61 | 64 | // TODO: can we stop exporting these? | 62 | 65 | export let _enabled = true; | @@ -141,11 +144,14 @@ function dispatchContinuousEvent( | 141 | 144 | nativeEvent, | 142 | 145 | ) { | 143 | 146 | const previousPriority = getCurrentUpdatePriority(); | | 147 | + const prevTransition = ReactCurrentBatchConfig.transition; | | 148 | + ReactCurrentBatchConfig.transition = 0; | 144 | 149 | try { | 145 | 150 | setCurrentUpdatePriority(ContinuousEventPriority); | 146 | 151 | dispatchEvent(domEventName, eventSystemFlags, container, nativeEvent); | 147 | 152 | } finally { | 148 | 153 | setCurrentUpdatePriority(previousPriority); | | 154 | + ReactCurrentBatchConfig.transition = prevTransition; | 149 | 155 | } | 150 | 156 | } | 151 | 157 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -240,6 +240,7 @@ const ceil = Math.ceil; | 240 | 240 | const { | 241 | 241 | ReactCurrentDispatcher, | 242 | 242 | ReactCurrentOwner, | | 243 | + ReactCurrentBatchConfig, | 243 | 244 | IsSomeRendererActing, | 244 | 245 | } = ReactSharedInternals; | 245 | 246 |
| @@ -1072,11 +1073,14 @@ export function flushDiscreteUpdates() { | 1072 | 1073 |
| 1073 | 1074 | export function deferredUpdates<A>(fn: () => A): A { | 1074 | 1075 | const previousPriority = getCurrentUpdatePriority(); | | 1076 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1075 | 1077 | try { | | 1078 | + ReactCurrentBatchConfig.transition = 0; | 1076 | 1079 | setCurrentUpdatePriority(DefaultEventPriority); | 1077 | 1080 | return fn(); | 1078 | 1081 | } finally { | 1079 | 1082 | setCurrentUpdatePriority(previousPriority); | | 1083 | + ReactCurrentBatchConfig.transition = prevTransition; | 1080 | 1084 | } | 1081 | 1085 | } | 1082 | 1086 |
| @@ -1118,11 +1122,14 @@ export function discreteUpdates<A, B, C, D, R>( | 1118 | 1122 | d: D, | 1119 | 1123 | ): R { | 1120 | 1124 | const previousPriority = getCurrentUpdatePriority(); | | 1125 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1121 | 1126 | try { | | 1127 | + ReactCurrentBatchConfig.transition = 0; | 1122 | 1128 | setCurrentUpdatePriority(DiscreteEventPriority); | 1123 | 1129 | return fn(a, b, c, d); | 1124 | 1130 | } finally { | 1125 | 1131 | setCurrentUpdatePriority(previousPriority); | | 1132 | + ReactCurrentBatchConfig.transition = prevTransition; | 1126 | 1133 | if (executionContext === NoContext) { | 1127 | 1134 | // Flush the immediate callbacks that were scheduled during this batch | 1128 | 1135 | resetRenderTimer(); | @@ -1151,8 +1158,10 @@ export function flushSync<A, R>(fn: A => R, a: A): R { | 1151 | 1158 | const prevExecutionContext = executionContext; | 1152 | 1159 | executionContext |= BatchedContext; | 1153 | 1160 |
| | 1161 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1154 | 1162 | const previousPriority = getCurrentUpdatePriority(); | 1155 | 1163 | try { | | 1164 | + ReactCurrentBatchConfig.transition = 0; | 1156 | 1165 | setCurrentUpdatePriority(DiscreteEventPriority); | 1157 | 1166 | if (fn) { | 1158 | 1167 | return fn(a); | @@ -1161,6 +1170,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R { | 1161 | 1170 | } | 1162 | 1171 | } finally { | 1163 | 1172 | setCurrentUpdatePriority(previousPriority); | | 1173 | + ReactCurrentBatchConfig.transition = prevTransition; | 1164 | 1174 | executionContext = prevExecutionContext; | 1165 | 1175 | // Flush the immediate callbacks that were scheduled during this batch. | 1166 | 1176 | // Note that this will happen even if batchedUpdates is higher up | @@ -1182,12 +1192,15 @@ export function flushSync<A, R>(fn: A => R, a: A): R { | 1182 | 1192 | export function flushControlled(fn: () => mixed): void { | 1183 | 1193 | const prevExecutionContext = executionContext; | 1184 | 1194 | executionContext |= BatchedContext; | | 1195 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1185 | 1196 | const previousPriority = getCurrentUpdatePriority(); | 1186 | 1197 | try { | | 1198 | + ReactCurrentBatchConfig.transition = 0; | 1187 | 1199 | setCurrentUpdatePriority(DiscreteEventPriority); | 1188 | 1200 | fn(); | 1189 | 1201 | } finally { | 1190 | 1202 | setCurrentUpdatePriority(previousPriority); | | 1203 | + ReactCurrentBatchConfig.transition = prevTransition; | 1191 | 1204 |
| 1192 | 1205 | executionContext = prevExecutionContext; | 1193 | 1206 | if (executionContext === NoContext) { | @@ -1681,10 +1694,13 @@ function commitRoot(root) { | 1681 | 1694 | // TODO: This no longer makes any sense. We already wrap the mutation and | 1682 | 1695 | // layout phases. Should be able to remove. | 1683 | 1696 | const previousUpdateLanePriority = getCurrentUpdatePriority(); | | 1697 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1684 | 1698 | try { | | 1699 | + ReactCurrentBatchConfig.transition = 0; | 1685 | 1700 | setCurrentUpdatePriority(DiscreteEventPriority); | 1686 | 1701 | commitRootImpl(root, previousUpdateLanePriority); | 1687 | 1702 | } finally { | | 1703 | + ReactCurrentBatchConfig.transition = prevTransition; | 1688 | 1704 | setCurrentUpdatePriority(previousUpdateLanePriority); | 1689 | 1705 | } | 1690 | 1706 |
| @@ -1797,6 +1813,8 @@ function commitRootImpl(root, renderPriorityLevel) { | 1797 | 1813 | NoFlags; | 1798 | 1814 |
| 1799 | 1815 | if (subtreeHasEffects || rootHasEffect) { | | 1816 | + const prevTransition = ReactCurrentBatchConfig.transition; | | 1817 | + ReactCurrentBatchConfig.transition = 0; | 1800 | 1818 | const previousPriority = getCurrentUpdatePriority(); | 1801 | 1819 | setCurrentUpdatePriority(DiscreteEventPriority); | 1802 | 1820 |
| @@ -1882,6 +1900,7 @@ function commitRootImpl(root, renderPriorityLevel) { | 1882 | 1900 |
| 1883 | 1901 | // Reset the priority to the previous non-sync value. | 1884 | 1902 | setCurrentUpdatePriority(previousPriority); | | 1903 | + ReactCurrentBatchConfig.transition = prevTransition; | 1885 | 1904 | } else { | 1886 | 1905 | // No effects. | 1887 | 1906 | root.current = finishedWork; | @@ -2019,12 +2038,15 @@ export function flushPassiveEffects(): boolean { | 2019 | 2038 | DefaultEventPriority, | 2020 | 2039 | lanesToEventPriority(pendingPassiveEffectsLanes), | 2021 | 2040 | ); | | 2041 | + const prevTransition = ReactCurrentBatchConfig.transition; | 2022 | 2042 | const previousPriority = getCurrentUpdatePriority(); | 2023 | 2043 | try { | | 2044 | + ReactCurrentBatchConfig.transition = 0; | 2024 | 2045 | setCurrentUpdatePriority(priority); | 2025 | 2046 | return flushPassiveEffectsImpl(); | 2026 | 2047 | } finally { | 2027 | 2048 | setCurrentUpdatePriority(previousPriority); | | 2049 | + ReactCurrentBatchConfig.transition = prevTransition; | 2028 | 2050 | } | 2029 | 2051 | } | 2030 | 2052 | return false; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -240,6 +240,7 @@ const ceil = Math.ceil; | 240 | 240 | const { | 241 | 241 | ReactCurrentDispatcher, | 242 | 242 | ReactCurrentOwner, | | 243 | + ReactCurrentBatchConfig, | 243 | 244 | IsSomeRendererActing, | 244 | 245 | } = ReactSharedInternals; | 245 | 246 |
| @@ -1072,11 +1073,14 @@ export function flushDiscreteUpdates() { | 1072 | 1073 |
| 1073 | 1074 | export function deferredUpdates<A>(fn: () => A): A { | 1074 | 1075 | const previousPriority = getCurrentUpdatePriority(); | | 1076 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1075 | 1077 | try { | | 1078 | + ReactCurrentBatchConfig.transition = 0; | 1076 | 1079 | setCurrentUpdatePriority(DefaultEventPriority); | 1077 | 1080 | return fn(); | 1078 | 1081 | } finally { | 1079 | 1082 | setCurrentUpdatePriority(previousPriority); | | 1083 | + ReactCurrentBatchConfig.transition = prevTransition; | 1080 | 1084 | } | 1081 | 1085 | } | 1082 | 1086 |
| @@ -1118,11 +1122,14 @@ export function discreteUpdates<A, B, C, D, R>( | 1118 | 1122 | d: D, | 1119 | 1123 | ): R { | 1120 | 1124 | const previousPriority = getCurrentUpdatePriority(); | | 1125 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1121 | 1126 | try { | | 1127 | + ReactCurrentBatchConfig.transition = 0; | 1122 | 1128 | setCurrentUpdatePriority(DiscreteEventPriority); | 1123 | 1129 | return fn(a, b, c, d); | 1124 | 1130 | } finally { | 1125 | 1131 | setCurrentUpdatePriority(previousPriority); | | 1132 | + ReactCurrentBatchConfig.transition = prevTransition; | 1126 | 1133 | if (executionContext === NoContext) { | 1127 | 1134 | // Flush the immediate callbacks that were scheduled during this batch | 1128 | 1135 | resetRenderTimer(); | @@ -1151,8 +1158,10 @@ export function flushSync<A, R>(fn: A => R, a: A): R { | 1151 | 1158 | const prevExecutionContext = executionContext; | 1152 | 1159 | executionContext |= BatchedContext; | 1153 | 1160 |
| | 1161 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1154 | 1162 | const previousPriority = getCurrentUpdatePriority(); | 1155 | 1163 | try { | | 1164 | + ReactCurrentBatchConfig.transition = 0; | 1156 | 1165 | setCurrentUpdatePriority(DiscreteEventPriority); | 1157 | 1166 | if (fn) { | 1158 | 1167 | return fn(a); | @@ -1161,6 +1170,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R { | 1161 | 1170 | } | 1162 | 1171 | } finally { | 1163 | 1172 | setCurrentUpdatePriority(previousPriority); | | 1173 | + ReactCurrentBatchConfig.transition = prevTransition; | 1164 | 1174 | executionContext = prevExecutionContext; | 1165 | 1175 | // Flush the immediate callbacks that were scheduled during this batch. | 1166 | 1176 | // Note that this will happen even if batchedUpdates is higher up | @@ -1182,12 +1192,15 @@ export function flushSync<A, R>(fn: A => R, a: A): R { | 1182 | 1192 | export function flushControlled(fn: () => mixed): void { | 1183 | 1193 | const prevExecutionContext = executionContext; | 1184 | 1194 | executionContext |= BatchedContext; | | 1195 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1185 | 1196 | const previousPriority = getCurrentUpdatePriority(); | 1186 | 1197 | try { | | 1198 | + ReactCurrentBatchConfig.transition = 0; | 1187 | 1199 | setCurrentUpdatePriority(DiscreteEventPriority); | 1188 | 1200 | fn(); | 1189 | 1201 | } finally { | 1190 | 1202 | setCurrentUpdatePriority(previousPriority); | | 1203 | + ReactCurrentBatchConfig.transition = prevTransition; | 1191 | 1204 |
| 1192 | 1205 | executionContext = prevExecutionContext; | 1193 | 1206 | if (executionContext === NoContext) { | @@ -1681,10 +1694,13 @@ function commitRoot(root) { | 1681 | 1694 | // TODO: This no longer makes any sense. We already wrap the mutation and | 1682 | 1695 | // layout phases. Should be able to remove. | 1683 | 1696 | const previousUpdateLanePriority = getCurrentUpdatePriority(); | | 1697 | + const prevTransition = ReactCurrentBatchConfig.transition; | 1684 | 1698 | try { | | 1699 | + ReactCurrentBatchConfig.transition = 0; | 1685 | 1700 | setCurrentUpdatePriority(DiscreteEventPriority); | 1686 | 1701 | commitRootImpl(root, previousUpdateLanePriority); | 1687 | 1702 | } finally { | | 1703 | + ReactCurrentBatchConfig.transition = prevTransition; | 1688 | 1704 | setCurrentUpdatePriority(previousUpdateLanePriority); | 1689 | 1705 | } | 1690 | 1706 |
| @@ -1797,6 +1813,8 @@ function commitRootImpl(root, renderPriorityLevel) { | 1797 | 1813 | NoFlags; | 1798 | 1814 |
| 1799 | 1815 | if (subtreeHasEffects || rootHasEffect) { | | 1816 | + const prevTransition = ReactCurrentBatchConfig.transition; | | 1817 | + ReactCurrentBatchConfig.transition = 0; | 1800 | 1818 | const previousPriority = getCurrentUpdatePriority(); | 1801 | 1819 | setCurrentUpdatePriority(DiscreteEventPriority); | 1802 | 1820 |
| @@ -1882,6 +1900,7 @@ function commitRootImpl(root, renderPriorityLevel) { | 1882 | 1900 |
| 1883 | 1901 | // Reset the priority to the previous non-sync value. | 1884 | 1902 | setCurrentUpdatePriority(previousPriority); | | 1903 | + ReactCurrentBatchConfig.transition = prevTransition; | 1885 | 1904 | } else { | 1886 | 1905 | // No effects. | 1887 | 1906 | root.current = finishedWork; | @@ -2019,12 +2038,15 @@ export function flushPassiveEffects(): boolean { | 2019 | 2038 | DefaultEventPriority, | 2020 | 2039 | lanesToEventPriority(pendingPassiveEffectsLanes), | 2021 | 2040 | ); | | 2041 | + const prevTransition = ReactCurrentBatchConfig.transition; | 2022 | 2042 | const previousPriority = getCurrentUpdatePriority(); | 2023 | 2043 | try { | | 2044 | + ReactCurrentBatchConfig.transition = 0; | 2024 | 2045 | setCurrentUpdatePriority(priority); | 2025 | 2046 | return flushPassiveEffectsImpl(); | 2026 | 2047 | } finally { | 2027 | 2048 | setCurrentUpdatePriority(previousPriority); | | 2049 | + ReactCurrentBatchConfig.transition = prevTransition; | 2028 | 2050 | } | 2029 | 2051 | } | 2030 | 2052 | return false; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactFlushSync-test.js +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ let ReactNoop; | 3 | 3 | let Scheduler; | 4 | 4 | let useState; | 5 | 5 | let useEffect; | | 6 | +let startTransition; | 6 | 7 |
| 7 | 8 | describe('ReactFlushSync', () => { | 8 | 9 | beforeEach(() => { | @@ -13,6 +14,7 @@ describe('ReactFlushSync', () => { | 13 | 14 | Scheduler = require('scheduler'); | 14 | 15 | useState = React.useState; | 15 | 16 | useEffect = React.useEffect; | | 17 | + startTransition = React.unstable_startTransition; | 16 | 18 | }); | 17 | 19 |
| 18 | 20 | function Text({text}) { | @@ -54,4 +56,45 @@ describe('ReactFlushSync', () => { | 54 | 56 | }); | 55 | 57 | expect(root).toMatchRenderedOutput('1, 1'); | 56 | 58 | }); | | 59 | + | | 60 | + // @gate experimental | | 61 | + test('nested with startTransition', async () => { | | 62 | + let setSyncState; | | 63 | + let setState; | | 64 | + function App() { | | 65 | + const [syncState, _setSyncState] = useState(0); | | 66 | + const [state, _setState] = useState(0); | | 67 | + setSyncState = _setSyncState; | | 68 | + setState = _setState; | | 69 | + return <Text text={`${syncState}, ${state}`} />; | | 70 | + } | | 71 | + | | 72 | + const root = ReactNoop.createRoot(); | | 73 | + await ReactNoop.act(async () => { | | 74 | + root.render(<App />); | | 75 | + }); | | 76 | + expect(Scheduler).toHaveYielded(['0, 0']); | | 77 | + expect(root).toMatchRenderedOutput('0, 0'); | | 78 | + | | 79 | + await ReactNoop.act(async () => { | | 80 | + ReactNoop.flushSync(() => { | | 81 | + startTransition(() => { | | 82 | + // This should be async even though flushSync is on the stack, because | | 83 | + // startTransition is closer. | | 84 | + setState(1); | | 85 | + ReactNoop.flushSync(() => { | | 86 | + // This should be async even though startTransition is on the stack, | | 87 | + // because flushSync is closer. | | 88 | + setSyncState(1); | | 89 | + }); | | 90 | + }); | | 91 | + }); | | 92 | + // Only the sync update should have flushed | | 93 | + expect(Scheduler).toHaveYielded(['1, 0']); | | 94 | + expect(root).toMatchRenderedOutput('1, 0'); | | 95 | + }); | | 96 | + // Now the async update has flushed, too. | | 97 | + expect(Scheduler).toHaveYielded(['1, 1']); | | 98 | + expect(root).toMatchRenderedOutput('1, 1'); | | 99 | + }); | 57 | 100 | }); |
|