GeezerVison is showing github.com — Schedule sync updates in microtask (#20872) · react/react@c581cdd
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/__tests__/ReactDOMServerPartialHydration-test.internal.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -380,6 +380,7 @@ describe('ReactDOMServerPartialHydration', () => { | 380 | 380 | resolve(); | 381 | 381 | await promise; | 382 | 382 | Scheduler.unstable_flushAll(); | | 383 | + await null; | 383 | 384 | jest.runAllTimers(); | 384 | 385 |
| 385 | 386 | // We should now have hydrated with a ref on the existing span. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactServerRenderingHydration-test.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -488,6 +488,7 @@ describe('ReactDOMServerHydration', () => { | 488 | 488 | jest.runAllTimers(); | 489 | 489 | await Promise.resolve(); | 490 | 490 | Scheduler.unstable_flushAll(); | | 491 | + await null; | 491 | 492 | expect(element.textContent).toBe('Hello world'); | 492 | 493 | }); | 493 | 494 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/SchedulerWithReactIntegration.new.js +13-5Lines changed: 13 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,13 +16,15 @@ import {__interactionsRef} from 'scheduler/tracing'; | 16 | 16 | import { | 17 | 17 | enableSchedulerTracing, | 18 | 18 | decoupleUpdatePriorityFromScheduler, | | 19 | + enableSyncMicroTasks, | 19 | 20 | } from 'shared/ReactFeatureFlags'; | 20 | 21 | import invariant from 'shared/invariant'; | 21 | 22 | import { | 22 | 23 | SyncLanePriority, | 23 | 24 | getCurrentUpdateLanePriority, | 24 | 25 | setCurrentUpdateLanePriority, | 25 | 26 | } from './ReactFiberLane.new'; | | 27 | +import {scheduleMicrotask, supportsMicrotasks} from './ReactFiberHostConfig'; | 26 | 28 |
| 27 | 29 | const { | 28 | 30 | unstable_runWithPriority: Scheduler_runWithPriority, | @@ -144,13 +146,19 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { | 144 | 146 | // the next tick, or earlier if something calls `flushSyncCallbackQueue`. | 145 | 147 | if (syncQueue === null) { | 146 | 148 | syncQueue = [callback]; | 147 | | - // Flush the queue in the next tick, at the earliest. | | 149 | + | 148 | 150 | // TODO: Figure out how to remove this It's only here as a last resort if we | 149 | 151 | // forget to explicitly flush. | 150 | | - immediateQueueCallbackNode = Scheduler_scheduleCallback( | 151 | | - Scheduler_ImmediatePriority, | 152 | | - flushSyncCallbackQueueImpl, | 153 | | - ); | | 152 | + if (enableSyncMicroTasks && supportsMicrotasks) { | | 153 | + // Flush the queue in a microtask. | | 154 | + scheduleMicrotask(flushSyncCallbackQueueImpl); | | 155 | + } else { | | 156 | + // Flush the queue in the next tick. | | 157 | + immediateQueueCallbackNode = Scheduler_scheduleCallback( | | 158 | + Scheduler_ImmediatePriority, | | 159 | + flushSyncCallbackQueueImpl, | | 160 | + ); | | 161 | + } | 154 | 162 | } else { | 155 | 163 | // Push onto existing queue. Don't need to schedule a callback because | 156 | 164 | // we already scheduled one when we created the queue. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/SchedulerWithReactIntegration.old.js +13-5Lines changed: 13 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,13 +16,15 @@ import {__interactionsRef} from 'scheduler/tracing'; | 16 | 16 | import { | 17 | 17 | enableSchedulerTracing, | 18 | 18 | decoupleUpdatePriorityFromScheduler, | | 19 | + enableSyncMicroTasks, | 19 | 20 | } from 'shared/ReactFeatureFlags'; | 20 | 21 | import invariant from 'shared/invariant'; | 21 | 22 | import { | 22 | 23 | SyncLanePriority, | 23 | 24 | getCurrentUpdateLanePriority, | 24 | 25 | setCurrentUpdateLanePriority, | 25 | 26 | } from './ReactFiberLane.old'; | | 27 | +import {scheduleMicrotask, supportsMicrotasks} from './ReactFiberHostConfig'; | 26 | 28 |
| 27 | 29 | const { | 28 | 30 | unstable_runWithPriority: Scheduler_runWithPriority, | @@ -144,13 +146,19 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { | 144 | 146 | // the next tick, or earlier if something calls `flushSyncCallbackQueue`. | 145 | 147 | if (syncQueue === null) { | 146 | 148 | syncQueue = [callback]; | 147 | | - // Flush the queue in the next tick, at the earliest. | | 149 | + | 148 | 150 | // TODO: Figure out how to remove this It's only here as a last resort if we | 149 | 151 | // forget to explicitly flush. | 150 | | - immediateQueueCallbackNode = Scheduler_scheduleCallback( | 151 | | - Scheduler_ImmediatePriority, | 152 | | - flushSyncCallbackQueueImpl, | 153 | | - ); | | 152 | + if (enableSyncMicroTasks && supportsMicrotasks) { | | 153 | + // Flush the queue in a microtask. | | 154 | + scheduleMicrotask(flushSyncCallbackQueueImpl); | | 155 | + } else { | | 156 | + // Flush the queue in the next tick. | | 157 | + immediateQueueCallbackNode = Scheduler_scheduleCallback( | | 158 | + Scheduler_ImmediatePriority, | | 159 | + flushSyncCallbackQueueImpl, | | 160 | + ); | | 161 | + } | 154 | 162 | } else { | 155 | 163 | // Push onto existing queue. Don't need to schedule a callback because | 156 | 164 | // we already scheduled one when we created the queue. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactExpiration-test.js +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -598,12 +598,13 @@ describe('ReactExpiration', () => { | 598 | 598 | // second one. | 599 | 599 | Scheduler.unstable_advanceTime(1000); | 600 | 600 | // Attempt to interrupt with a high pri update. | 601 | | - updateHighPri(); | | 601 | + await ReactNoop.act(async () => { | | 602 | + updateHighPri(); | | 603 | + }); | 602 | 604 |
| 603 | | - // The first update expired, so first will finish it without | 604 | | - // interrupting. But not the second update, which hasn't expired yet. | 605 | | - expect(Scheduler).toFlushExpired(['Sibling']); | 606 | | - expect(Scheduler).toFlushAndYield([ | | 605 | + expect(Scheduler).toHaveYielded([ | | 606 | + // The first update expired | | 607 | + 'Sibling', | 607 | 608 | // Then render the high pri update | 608 | 609 | 'High pri: 1', | 609 | 610 | 'Normal pri: 1', | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1792,7 +1792,7 @@ describe('ReactHooksWithNoopRenderer', () => { | 1792 | 1792 | it( | 1793 | 1793 | 'in legacy mode, useEffect is deferred and updates finish synchronously ' + | 1794 | 1794 | '(in a single batch)', | 1795 | | - () => { | | 1795 | + async () => { | 1796 | 1796 | function Counter(props) { | 1797 | 1797 | const [count, updateCount] = useState('(empty)'); | 1798 | 1798 | useEffect(() => { | @@ -1807,10 +1807,12 @@ describe('ReactHooksWithNoopRenderer', () => { | 1807 | 1807 | }, [props.count]); | 1808 | 1808 | return <Text text={'Count: ' + count} />; | 1809 | 1809 | } | 1810 | | - act(() => { | | 1810 | + await act(async () => { | 1811 | 1811 | ReactNoop.renderLegacySyncRoot(<Counter count={0} />); | | 1812 | + | 1812 | 1813 | // Even in legacy mode, effects are deferred until after paint | 1813 | | - expect(Scheduler).toFlushAndYieldThrough(['Count: (empty)']); | | 1814 | + ReactNoop.flushSync(); | | 1815 | + expect(Scheduler).toHaveYielded(['Count: (empty)']); | 1814 | 1816 | expect(ReactNoop.getChildren()).toEqual([span('Count: (empty)')]); | 1815 | 1817 | }); | 1816 | 1818 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactIncrementalErrorHandling-test.internal.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1400,6 +1400,10 @@ describe('ReactIncrementalErrorHandling', () => { | 1400 | 1400 | 'BrokenRenderAndUnmount componentWillUnmount', | 1401 | 1401 | ]); | 1402 | 1402 | expect(ReactNoop.getChildren()).toEqual([]); | | 1403 | + | | 1404 | + expect(() => { | | 1405 | + ReactNoop.flushSync(); | | 1406 | + }).toThrow('One does not simply unmount me.'); | 1403 | 1407 | }); | 1404 | 1408 |
| 1405 | 1409 | it('does not interrupt unmounting if detaching a ref throws', () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactOffscreen-test.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -98,8 +98,11 @@ describe('ReactOffscreen', () => { | 98 | 98 | <Text text="Outside" /> | 99 | 99 | </>, | 100 | 100 | ); | | 101 | + | | 102 | + ReactNoop.flushSync(); | | 103 | + | 101 | 104 | // Should not defer the hidden tree | 102 | | - expect(Scheduler).toFlushUntilNextPaint(['A', 'Outside']); | | 105 | + expect(Scheduler).toHaveYielded(['A', 'Outside']); | 103 | 106 | }); | 104 | 107 | expect(root).toMatchRenderedOutput( | 105 | 108 | <> | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactSchedulerIntegration-test.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -569,9 +569,11 @@ describe( | 569 | 569 | ReactNoop.render(<App />); | 570 | 570 | }); | 571 | 571 |
| | 572 | + ReactNoop.flushSync(); | | 573 | + | 572 | 574 | // Because the render expired, React should finish the tree without | 573 | 575 | // consulting `shouldYield` again | 574 | | - expect(Scheduler).toFlushExpired(['B', 'C']); | | 576 | + expect(Scheduler).toHaveYielded(['B', 'C']); | 575 | 577 | }); | 576 | 578 | }); | 577 | 579 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactSuspenseList-test.js +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -292,9 +292,11 @@ describe('ReactSuspenseList', () => { | 292 | 292 | </>, | 293 | 293 | ); | 294 | 294 |
| 295 | | - await C.resolve(); | | 295 | + await ReactNoop.act(async () => { | | 296 | + C.resolve(); | | 297 | + }); | 296 | 298 |
| 297 | | - expect(Scheduler).toFlushAndYield(['C']); | | 299 | + expect(Scheduler).toHaveYielded(['C']); | 298 | 300 |
| 299 | 301 | expect(ReactNoop).toMatchRenderedOutput( | 300 | 302 | <> | @@ -304,9 +306,11 @@ describe('ReactSuspenseList', () => { | 304 | 306 | </>, | 305 | 307 | ); | 306 | 308 |
| 307 | | - await B.resolve(); | | 309 | + await ReactNoop.act(async () => { | | 310 | + B.resolve(); | | 311 | + }); | 308 | 312 |
| 309 | | - expect(Scheduler).toFlushAndYield(['B']); | | 313 | + expect(Scheduler).toHaveYielded(['B']); | 310 | 314 |
| 311 | 315 | expect(ReactNoop).toMatchRenderedOutput( | 312 | 316 | <> | |
|