GeezerVison is showing github.com — Land enableSyncMicrotasks (#20979) · react/react@be5a2e2
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__/ReactDOMNativeEventHeuristic-test.js +10-8Lines changed: 10 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -243,11 +243,12 @@ describe('ReactDOMNativeEventHeuristic-test', () => { | 243 | 243 | mouseOverEvent.initEvent('mouseover', true, true); | 244 | 244 | dispatchAndSetCurrentEvent(target.current, mouseOverEvent); | 245 | 245 |
| 246 | | - // 3s should be enough to expire the updates | 247 | | - Scheduler.unstable_advanceTime(3000); | 248 | | - expect(Scheduler).toFlushExpired([]); | 249 | | - expect(container.textContent).toEqual('hovered'); | | 246 | + // Flush discrete updates | | 247 | + ReactDOM.flushSync(); | | 248 | + // Since mouse over is not discrete, should not have updated yet | | 249 | + expect(container.textContent).toEqual('not hovered'); | 250 | 250 | }); | | 251 | + expect(container.textContent).toEqual('hovered'); | 251 | 252 | }); | 252 | 253 |
| 253 | 254 | // @gate experimental | @@ -275,11 +276,12 @@ describe('ReactDOMNativeEventHeuristic-test', () => { | 275 | 276 | mouseEnterEvent.initEvent('mouseenter', true, true); | 276 | 277 | dispatchAndSetCurrentEvent(target.current, mouseEnterEvent); | 277 | 278 |
| 278 | | - // 3s should be enough to expire the updates | 279 | | - Scheduler.unstable_advanceTime(3000); | 280 | | - expect(Scheduler).toFlushExpired([]); | 281 | | - expect(container.textContent).toEqual('hovered'); | | 279 | + // Flush discrete updates | | 280 | + ReactDOM.flushSync(); | | 281 | + // Since mouse end is not discrete, should not have updated yet | | 282 | + expect(container.textContent).toEqual('not hovered'); | 282 | 283 | }); | | 284 | + expect(container.textContent).toEqual('hovered'); | 283 | 285 | }); | 284 | 286 |
| 285 | 287 | // @gate experimental | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -761,11 +761,12 @@ describe('ChangeEventPlugin', () => { | 761 | 761 | mouseOverEvent.initEvent('mouseover', true, true); | 762 | 762 | target.current.dispatchEvent(mouseOverEvent); | 763 | 763 |
| 764 | | - // 3s should be enough to expire the updates | 765 | | - Scheduler.unstable_advanceTime(3000); | 766 | | - expect(Scheduler).toFlushExpired([]); | 767 | | - expect(container.textContent).toEqual('hovered'); | | 764 | + // Flush discrete updates | | 765 | + ReactDOM.flushSync(); | | 766 | + // Since mouse enter/leave is not discrete, should not have updated yet | | 767 | + expect(container.textContent).toEqual('not hovered'); | 768 | 768 | }); | | 769 | + expect(container.textContent).toEqual('hovered'); | 769 | 770 | }); | 770 | 771 | }); | 771 | 772 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/SchedulerWithReactIntegration.new.js +2-5Lines changed: 2 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,10 +13,7 @@ import type {ReactPriorityLevel} from './ReactInternalTypes'; | 13 | 13 | // CommonJS interop named imports. | 14 | 14 | import * as Scheduler from 'scheduler'; | 15 | 15 | import {__interactionsRef} from 'scheduler/tracing'; | 16 | | -import { | 17 | | - enableSchedulerTracing, | 18 | | - enableSyncMicroTasks, | 19 | | -} from 'shared/ReactFeatureFlags'; | | 16 | +import {enableSchedulerTracing} from 'shared/ReactFeatureFlags'; | 20 | 17 | import invariant from 'shared/invariant'; | 21 | 18 | import { | 22 | 19 | SyncLanePriority, | @@ -139,7 +136,7 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { | 139 | 136 |
| 140 | 137 | // TODO: Figure out how to remove this It's only here as a last resort if we | 141 | 138 | // forget to explicitly flush. | 142 | | - if (enableSyncMicroTasks && supportsMicrotasks) { | | 139 | + if (supportsMicrotasks) { | 143 | 140 | // Flush the queue in a microtask. | 144 | 141 | scheduleMicrotask(flushSyncCallbackQueueImpl); | 145 | 142 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/SchedulerWithReactIntegration.old.js +2-5Lines changed: 2 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,10 +13,7 @@ import type {ReactPriorityLevel} from './ReactInternalTypes'; | 13 | 13 | // CommonJS interop named imports. | 14 | 14 | import * as Scheduler from 'scheduler'; | 15 | 15 | import {__interactionsRef} from 'scheduler/tracing'; | 16 | | -import { | 17 | | - enableSchedulerTracing, | 18 | | - enableSyncMicroTasks, | 19 | | -} from 'shared/ReactFeatureFlags'; | | 16 | +import {enableSchedulerTracing} from 'shared/ReactFeatureFlags'; | 20 | 17 | import invariant from 'shared/invariant'; | 21 | 18 | import { | 22 | 19 | SyncLanePriority, | @@ -139,7 +136,7 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { | 139 | 136 |
| 140 | 137 | // TODO: Figure out how to remove this It's only here as a last resort if we | 141 | 138 | // forget to explicitly flush. | 142 | | - if (enableSyncMicroTasks && supportsMicrotasks) { | | 139 | + if (supportsMicrotasks) { | 143 | 140 | // Flush the queue in a microtask. | 144 | 141 | scheduleMicrotask(flushSyncCallbackQueueImpl); | 145 | 142 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactExpiration-test.js +25-10Lines changed: 25 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,23 +103,32 @@ describe('ReactExpiration', () => { | 103 | 103 | return {type: 'span', children: [], prop, hidden: false}; | 104 | 104 | } | 105 | 105 |
| | 106 | + function flushNextRenderIfExpired() { | | 107 | + // This will start rendering the next level of work. If the work hasn't | | 108 | + // expired yet, React will exit without doing anything. If it has expired, | | 109 | + // it will schedule a sync task. | | 110 | + Scheduler.unstable_flushExpired(); | | 111 | + // Flush the sync task. | | 112 | + ReactNoop.flushSync(); | | 113 | + } | | 114 | + | 106 | 115 | it('increases priority of updates as time progresses', () => { | 107 | 116 | ReactNoop.render(<span prop="done" />); | 108 | 117 |
| 109 | 118 | expect(ReactNoop.getChildren()).toEqual([]); | 110 | 119 |
| 111 | 120 | // Nothing has expired yet because time hasn't advanced. | 112 | | - ReactNoop.flushExpired(); | | 121 | + flushNextRenderIfExpired(); | 113 | 122 | expect(ReactNoop.getChildren()).toEqual([]); | 114 | 123 |
| 115 | 124 | // Advance time a bit, but not enough to expire the low pri update. | 116 | 125 | ReactNoop.expire(4500); | 117 | | - ReactNoop.flushExpired(); | | 126 | + flushNextRenderIfExpired(); | 118 | 127 | expect(ReactNoop.getChildren()).toEqual([]); | 119 | 128 |
| 120 | 129 | // Advance by another second. Now the update should expire and flush. | 121 | | - ReactNoop.expire(1000); | 122 | | - ReactNoop.flushExpired(); | | 130 | + ReactNoop.expire(500); | | 131 | + flushNextRenderIfExpired(); | 123 | 132 | expect(ReactNoop.getChildren()).toEqual([span('done')]); | 124 | 133 | }); | 125 | 134 |
| @@ -323,7 +332,8 @@ describe('ReactExpiration', () => { | 323 | 332 |
| 324 | 333 | Scheduler.unstable_advanceTime(10000); | 325 | 334 |
| 326 | | - expect(Scheduler).toFlushExpired(['D', 'E']); | | 335 | + flushNextRenderIfExpired(); | | 336 | + expect(Scheduler).toHaveYielded(['D', 'E']); | 327 | 337 | expect(root).toMatchRenderedOutput('ABCDE'); | 328 | 338 | }); | 329 | 339 |
| @@ -351,7 +361,8 @@ describe('ReactExpiration', () => { | 351 | 361 |
| 352 | 362 | Scheduler.unstable_advanceTime(10000); | 353 | 363 |
| 354 | | - expect(Scheduler).toFlushExpired(['D', 'E']); | | 364 | + flushNextRenderIfExpired(); | | 365 | + expect(Scheduler).toHaveYielded(['D', 'E']); | 355 | 366 | expect(root).toMatchRenderedOutput('ABCDE'); | 356 | 367 | }); | 357 | 368 |
| @@ -373,12 +384,14 @@ describe('ReactExpiration', () => { | 373 | 384 | ReactNoop.render('Hi'); | 374 | 385 |
| 375 | 386 | // The update should not have expired yet. | 376 | | - expect(Scheduler).toFlushExpired([]); | | 387 | + flushNextRenderIfExpired(); | | 388 | + expect(Scheduler).toHaveYielded([]); | 377 | 389 | expect(ReactNoop).toMatchRenderedOutput(null); | 378 | 390 |
| 379 | 391 | // Advance the time some more to expire the update. | 380 | 392 | Scheduler.unstable_advanceTime(10000); | 381 | | - expect(Scheduler).toFlushExpired([]); | | 393 | + flushNextRenderIfExpired(); | | 394 | + expect(Scheduler).toHaveYielded([]); | 382 | 395 | expect(ReactNoop).toMatchRenderedOutput('Hi'); | 383 | 396 | }); | 384 | 397 |
| @@ -391,15 +404,17 @@ describe('ReactExpiration', () => { | 391 | 404 | Scheduler.unstable_advanceTime(10000); | 392 | 405 |
| 393 | 406 | ReactNoop.render('Hi'); | 394 | | - expect(Scheduler).toFlushExpired([]); | | 407 | + flushNextRenderIfExpired(); | | 408 | + expect(Scheduler).toHaveYielded([]); | 395 | 409 | expect(ReactNoop).toMatchRenderedOutput(null); | 396 | 410 |
| 397 | 411 | // Advancing by ~5 seconds should be sufficient to expire the update. (I | 398 | 412 | // used a slightly larger number to allow for possible rounding.) | 399 | 413 | Scheduler.unstable_advanceTime(6000); | 400 | 414 |
| 401 | 415 | ReactNoop.render('Hi'); | 402 | | - expect(Scheduler).toFlushExpired([]); | | 416 | + flushNextRenderIfExpired(); | | 417 | + expect(Scheduler).toHaveYielded([]); | 403 | 418 | expect(ReactNoop).toMatchRenderedOutput('Hi'); | 404 | 419 | }); | 405 | 420 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactIncrementalErrorHandling-test.internal.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -402,7 +402,7 @@ describe('ReactIncrementalErrorHandling', () => { | 402 | 402 | expect(ReactNoop.getChildren()).toEqual([]); | 403 | 403 | }); | 404 | 404 |
| 405 | | - it('retries one more time if an error occurs during a render that expires midway through the tree', () => { | | 405 | + it('retries one more time if an error occurs during a render that expires midway through the tree', async () => { | 406 | 406 | function Oops({unused}) { | 407 | 407 | Scheduler.unstable_yieldValue('Oops'); | 408 | 408 | throw new Error('Oops'); | @@ -432,7 +432,11 @@ describe('ReactIncrementalErrorHandling', () => { | 432 | 432 |
| 433 | 433 | // Expire the render midway through | 434 | 434 | Scheduler.unstable_advanceTime(10000); | 435 | | - expect(() => Scheduler.unstable_flushExpired()).toThrow('Oops'); | | 435 | + | | 436 | + expect(() => { | | 437 | + Scheduler.unstable_flushExpired(); | | 438 | + ReactNoop.flushSync(); | | 439 | + }).toThrow('Oops'); | 436 | 440 |
| 437 | 441 | expect(Scheduler).toHaveYielded([ | 438 | 442 | // The render expired, but we shouldn't throw out the partial work. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactIncrementalUpdates-test.js +19-5Lines changed: 19 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,15 @@ describe('ReactIncrementalUpdates', () => { | 32 | 32 | return {type: 'span', children: [], prop, hidden: false}; | 33 | 33 | } | 34 | 34 |
| | 35 | + function flushNextRenderIfExpired() { | | 36 | + // This will start rendering the next level of work. If the work hasn't | | 37 | + // expired yet, React will exit without doing anything. If it has expired, | | 38 | + // it will schedule a sync task. | | 39 | + Scheduler.unstable_flushExpired(); | | 40 | + // Flush the sync task. | | 41 | + ReactNoop.flushSync(); | | 42 | + } | | 43 | + | 35 | 44 | it('applies updates in order of priority', () => { | 36 | 45 | let state; | 37 | 46 | class Foo extends React.Component { | @@ -469,7 +478,8 @@ describe('ReactIncrementalUpdates', () => { | 469 | 478 |
| 470 | 479 | ReactNoop.act(() => { | 471 | 480 | ReactNoop.render(<App />); | 472 | | - expect(Scheduler).toFlushExpired([]); | | 481 | + flushNextRenderIfExpired(); | | 482 | + expect(Scheduler).toHaveYielded([]); | 473 | 483 | expect(Scheduler).toFlushAndYield([ | 474 | 484 | 'Render: 0', | 475 | 485 | 'Commit: 0', | @@ -479,7 +489,8 @@ describe('ReactIncrementalUpdates', () => { | 479 | 489 | Scheduler.unstable_advanceTime(10000); | 480 | 490 |
| 481 | 491 | setCount(2); | 482 | | - expect(Scheduler).toFlushExpired([]); | | 492 | + flushNextRenderIfExpired(); | | 493 | + expect(Scheduler).toHaveYielded([]); | 483 | 494 | }); | 484 | 495 | }); | 485 | 496 |
| @@ -497,7 +508,8 @@ describe('ReactIncrementalUpdates', () => { | 497 | 508 | Scheduler.unstable_advanceTime(10000); | 498 | 509 |
| 499 | 510 | ReactNoop.render(<Text text="B" />); | 500 | | - expect(Scheduler).toFlushExpired([]); | | 511 | + flushNextRenderIfExpired(); | | 512 | + expect(Scheduler).toHaveYielded([]); | 501 | 513 | }); | 502 | 514 |
| 503 | 515 | it('regression: does not expire soon due to previous expired work', () => { | @@ -508,12 +520,14 @@ describe('ReactIncrementalUpdates', () => { | 508 | 520 |
| 509 | 521 | ReactNoop.render(<Text text="A" />); | 510 | 522 | Scheduler.unstable_advanceTime(10000); | 511 | | - expect(Scheduler).toFlushExpired(['A']); | | 523 | + flushNextRenderIfExpired(); | | 524 | + expect(Scheduler).toHaveYielded(['A']); | 512 | 525 |
| 513 | 526 | Scheduler.unstable_advanceTime(10000); | 514 | 527 |
| 515 | 528 | ReactNoop.render(<Text text="B" />); | 516 | | - expect(Scheduler).toFlushExpired([]); | | 529 | + flushNextRenderIfExpired(); | | 530 | + expect(Scheduler).toHaveYielded([]); | 517 | 531 | }); | 518 | 532 |
| 519 | 533 | it('when rebasing, does not exclude updates that were already committed, regardless of priority', async () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js +31-10Lines changed: 31 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -984,6 +984,10 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 984 | 984 | expect(ReactNoop.getChildren()).toEqual([span('C')]); | 985 | 985 | }); | 986 | 986 |
| | 987 | + // TODO: This test was written against the old Expiration Times | | 988 | + // implementation. It doesn't really test what it was intended to test | | 989 | + // anymore, because all updates to the same queue get entangled together. | | 990 | + // Even if they haven't expired. Consider either deleting or rewriting. | 987 | 991 | // @gate enableCache | 988 | 992 | it('flushes all expired updates in a single batch', async () => { | 989 | 993 | class Foo extends React.Component { | @@ -1013,10 +1017,7 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 1013 | 1017 | jest.advanceTimersByTime(1000); | 1014 | 1018 | ReactNoop.render(<Foo text="goodbye" />); | 1015 | 1019 |
| 1016 | | - Scheduler.unstable_advanceTime(10000); | 1017 | | - jest.advanceTimersByTime(10000); | 1018 | | - | 1019 | | - expect(Scheduler).toFlushExpired([ | | 1020 | + expect(Scheduler).toFlushAndYield([ | 1020 | 1021 | 'Suspend! [goodbye]', | 1021 | 1022 | 'Loading...', | 1022 | 1023 | 'Commit: goodbye', | @@ -1797,12 +1798,32 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 1797 | 1798 | await advanceTimers(5000); | 1798 | 1799 |
| 1799 | 1800 | // Retry with the new content. | 1800 | | - expect(Scheduler).toFlushAndYield([ | 1801 | | - 'A', | 1802 | | - // B still suspends | 1803 | | - 'Suspend! [B]', | 1804 | | - 'Loading more...', | 1805 | | - ]); | | 1801 | + if (gate(flags => flags.disableSchedulerTimeoutInWorkLoop)) { | | 1802 | + expect(Scheduler).toFlushAndYield([ | | 1803 | + 'A', | | 1804 | + // B still suspends | | 1805 | + 'Suspend! [B]', | | 1806 | + 'Loading more...', | | 1807 | + ]); | | 1808 | + } else { | | 1809 | + // In this branch, right as we start rendering, we detect that the work | | 1810 | + // has expired (via Scheduler's didTimeout argument) and re-schedule the | | 1811 | + // work as synchronous. Since sync work does not flow through Scheduler, | | 1812 | + // we need to use `flushSync`. | | 1813 | + // | | 1814 | + // Usually we would use `act`, which fluses both sync work and Scheduler | | 1815 | + // work, but that would also force the fallback to display, and this test | | 1816 | + // is specifically about whether we delay or show the fallback. | | 1817 | + expect(Scheduler).toFlushAndYield([]); | | 1818 | + // This will flush the synchronous callback we just scheduled. | | 1819 | + ReactNoop.flushSync(); | | 1820 | + expect(Scheduler).toHaveYielded([ | | 1821 | + 'A', | | 1822 | + // B still suspends | | 1823 | + 'Suspend! [B]', | | 1824 | + 'Loading more...', | | 1825 | + ]); | | 1826 | + } | 1806 | 1827 | // Because we've already been waiting for so long we've exceeded | 1807 | 1828 | // our threshold and we show the next level immediately. | 1808 | 1829 | expect(ReactNoop.getChildren()).toEqual([ | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactFeatureFlags.js -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -150,6 +150,4 @@ export const enableRecursiveCommitTraversal = false; | 150 | 150 |
| 151 | 151 | export const disableSchedulerTimeoutInWorkLoop = false; | 152 | 152 |
| 153 | | -export const enableSyncMicroTasks = false; | 154 | | - | 155 | 153 | export const enableLazyContextPropagation = false; |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-fb.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,6 @@ export const enableUseRefAccessWarning = false; | 57 | 57 |
| 58 | 58 | export const enableRecursiveCommitTraversal = false; | 59 | 59 | export const disableSchedulerTimeoutInWorkLoop = false; | 60 | | -export const enableSyncMicroTasks = false; | 61 | 60 | export const enableLazyContextPropagation = false; | 62 | 61 |
| 63 | 62 | // Flow magic to verify the exports of this file match the original version. | |
|