GeezerVison is showing github.com — Queue discrete events in microtask (#20669) · react/react@e51bd6c
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/client/ReactDOMHostConfig.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -392,7 +392,7 @@ export const queueMicrotask: any = | 392 | 392 | Promise.resolve(null) | 393 | 393 | .then(callback) | 394 | 394 | .catch(handleErrorInNextTick) | 395 | | - : scheduleTimeout; | | 395 | + : scheduleTimeout; // TODO: Determine the best fallback here. | 396 | 396 |
| 397 | 397 | function handleErrorInNextTick(error) { | 398 | 398 | setTimeout(() => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -730,8 +730,15 @@ describe('ChangeEventPlugin', () => { | 730 | 730 |
| 731 | 731 | // Flush callbacks. | 732 | 732 | // Now the click update has flushed. | 733 | | - expect(Scheduler).toFlushAndYield(['render: ']); | 734 | | - expect(input.value).toBe(''); | | 733 | + if (gate(flags => flags.enableDiscreteEventMicroTasks)) { | | 734 | + // Flush microtask queue. | | 735 | + await null; | | 736 | + expect(Scheduler).toHaveYielded(['render: ']); | | 737 | + expect(input.value).toBe(''); | | 738 | + } else { | | 739 | + expect(Scheduler).toFlushAndYield(['render: ']); | | 740 | + expect(input.value).toBe(''); | | 741 | + } | 735 | 742 | }); | 736 | 743 |
| 737 | 744 | // @gate experimental | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js +18-5Lines changed: 18 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -470,11 +470,24 @@ describe('SimpleEventPlugin', function() { | 470 | 470 | 'High-pri count: 7, Low-pri count: 0', | 471 | 471 | ]); | 472 | 472 |
| 473 | | - // At the end, both counters should equal the total number of clicks | 474 | | - expect(Scheduler).toFlushAndYield([ | 475 | | - 'High-pri count: 8, Low-pri count: 0', | 476 | | - 'High-pri count: 8, Low-pri count: 8', | 477 | | - ]); | | 473 | + if (gate(flags => flags.enableDiscreteEventMicroTasks)) { | | 474 | + // Flush the microtask queue | | 475 | + await null; | | 476 | + | | 477 | + // At the end, both counters should equal the total number of clicks | | 478 | + expect(Scheduler).toHaveYielded([ | | 479 | + 'High-pri count: 8, Low-pri count: 0', | | 480 | + | | 481 | + // TODO: with cancellation, this required another flush? | | 482 | + 'High-pri count: 8, Low-pri count: 8', | | 483 | + ]); | | 484 | + } else { | | 485 | + // At the end, both counters should equal the total number of clicks | | 486 | + expect(Scheduler).toFlushAndYield([ | | 487 | + 'High-pri count: 8, Low-pri count: 0', | | 488 | + 'High-pri count: 8, Low-pri count: 8', | | 489 | + ]); | | 490 | + } | 478 | 491 | expect(button.textContent).toEqual('High-pri count: 8, Low-pri count: 8'); | 479 | 492 | }); | 480 | 493 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/README.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -203,6 +203,10 @@ You can proxy this to `clearTimeout` or its equivalent in your environment. | 203 | 203 |
| 204 | 204 | This is a property (not a function) that should be set to something that can never be a valid timeout ID. For example, you can set it to `-1`. | 205 | 205 |
| | 206 | +#### `queueMicrotask(fn)` | | 207 | + | | 208 | +You can proxy this to `queueMicrotask` or its equivalent in your environment. | | 209 | + | 206 | 210 | #### `isPrimaryRenderer` | 207 | 211 |
| 208 | 212 | This is a property (not a function) that should be set to `true` if your renderer is the main one on the page. For example, if you're writing a renderer for the Terminal, it makes sense to set it to `true`, but if your renderer is used *on top of* React DOM or some other existing renderer, set it to `false`. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +33-9Lines changed: 33 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -92,6 +92,7 @@ import { | 92 | 92 | warnsIfNotActing, | 93 | 93 | afterActiveInstanceBlur, | 94 | 94 | clearContainer, | | 95 | + queueMicrotask, | 95 | 96 | } from './ReactFiberHostConfig'; | 96 | 97 |
| 97 | 98 | import { | @@ -216,6 +217,7 @@ import { | 216 | 217 | syncNestedUpdateFlag, | 217 | 218 | } from './ReactProfilerTimer.new'; | 218 | 219 |
| | 220 | +import {enableDiscreteEventMicroTasks} from 'shared/ReactFeatureFlags'; | 219 | 221 | // DEV stuff | 220 | 222 | import getComponentName from 'shared/getComponentName'; | 221 | 223 | import ReactStrictModeWarnings from './ReactStrictModeWarnings.new'; | @@ -714,21 +716,34 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 714 | 716 | // Special case: There's nothing to work on. | 715 | 717 | if (existingCallbackNode !== null) { | 716 | 718 | cancelCallback(existingCallbackNode); | 717 | | - root.callbackNode = null; | 718 | | - root.callbackPriority = NoLanePriority; | 719 | 719 | } | | 720 | + root.callbackNode = null; | | 721 | + root.callbackPriority = NoLanePriority; | 720 | 722 | return; | 721 | 723 | } | 722 | 724 |
| 723 | 725 | // Check if there's an existing task. We may be able to reuse it. | 724 | | - if (existingCallbackNode !== null) { | 725 | | - const existingCallbackPriority = root.callbackPriority; | 726 | | - if (existingCallbackPriority === newCallbackPriority) { | 727 | | - // The priority hasn't changed. We can reuse the existing task. Exit. | 728 | | - return; | | 726 | + const existingCallbackPriority = root.callbackPriority; | | 727 | + if (existingCallbackPriority === newCallbackPriority) { | | 728 | + if (__DEV__) { | | 729 | + // If we're going to re-use an existing task, it needs to exist. | | 730 | + // Assume that discrete update microtasks are non-cancellable and null. | | 731 | + // TODO: Temporary until we confirm this warning is not fired. | | 732 | + if ( | | 733 | + existingCallbackNode == null && | | 734 | + existingCallbackPriority !== InputDiscreteLanePriority | | 735 | + ) { | | 736 | + console.error( | | 737 | + 'Expected scheduled callback to exist. This error is likely caused by a bug in React. Please file an issue.', | | 738 | + ); | | 739 | + } | 729 | 740 | } | 730 | | - // The priority changed. Cancel the existing callback. We'll schedule a new | 731 | | - // one below. | | 741 | + // The priority hasn't changed. We can reuse the existing task. Exit. | | 742 | + return; | | 743 | + } | | 744 | + | | 745 | + if (existingCallbackNode != null) { | | 746 | + // Cancel the existing callback. We'll schedule a new one below. | 732 | 747 | cancelCallback(existingCallbackNode); | 733 | 748 | } | 734 | 749 |
| @@ -737,6 +752,8 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 737 | 752 | if (newCallbackPriority === SyncLanePriority) { | 738 | 753 | // Special case: Sync React callbacks are scheduled on a special | 739 | 754 | // internal queue | | 755 | + | | 756 | + // TODO: After enableDiscreteEventMicroTasks lands, we can remove the fake node. | 740 | 757 | newCallbackNode = scheduleSyncCallback( | 741 | 758 | performSyncWorkOnRoot.bind(null, root), | 742 | 759 | ); | @@ -745,6 +762,12 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 745 | 762 | ImmediateSchedulerPriority, | 746 | 763 | performSyncWorkOnRoot.bind(null, root), | 747 | 764 | ); | | 765 | + } else if ( | | 766 | + enableDiscreteEventMicroTasks && | | 767 | + newCallbackPriority === InputDiscreteLanePriority | | 768 | + ) { | | 769 | + queueMicrotask(performSyncWorkOnRoot.bind(null, root)); | | 770 | + newCallbackNode = null; | 748 | 771 | } else { | 749 | 772 | const schedulerPriorityLevel = lanePriorityToSchedulerPriority( | 750 | 773 | newCallbackPriority, | @@ -1871,6 +1894,7 @@ function commitRootImpl(root, renderPriorityLevel) { | 1871 | 1894 | // commitRoot never returns a continuation; it always finishes synchronously. | 1872 | 1895 | // So we can clear these now to allow a new callback to be scheduled. | 1873 | 1896 | root.callbackNode = null; | | 1897 | + root.callbackPriority = NoLanePriority; | 1874 | 1898 |
| 1875 | 1899 | // Update the first and last pending times on this root. The new first | 1876 | 1900 | // pending time is whatever is left on the root fiber. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +33-9Lines changed: 33 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,7 @@ import { | 34 | 34 | disableSchedulerTimeoutInWorkLoop, | 35 | 35 | enableDoubleInvokingEffects, | 36 | 36 | skipUnmountedBoundaries, | | 37 | + enableDiscreteEventMicroTasks, | 37 | 38 | } from 'shared/ReactFeatureFlags'; | 38 | 39 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 39 | 40 | import invariant from 'shared/invariant'; | @@ -92,6 +93,7 @@ import { | 92 | 93 | warnsIfNotActing, | 93 | 94 | afterActiveInstanceBlur, | 94 | 95 | clearContainer, | | 96 | + queueMicrotask, | 95 | 97 | } from './ReactFiberHostConfig'; | 96 | 98 |
| 97 | 99 | import { | @@ -696,21 +698,34 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 696 | 698 | // Special case: There's nothing to work on. | 697 | 699 | if (existingCallbackNode !== null) { | 698 | 700 | cancelCallback(existingCallbackNode); | 699 | | - root.callbackNode = null; | 700 | | - root.callbackPriority = NoLanePriority; | 701 | 701 | } | | 702 | + root.callbackNode = null; | | 703 | + root.callbackPriority = NoLanePriority; | 702 | 704 | return; | 703 | 705 | } | 704 | 706 |
| 705 | 707 | // Check if there's an existing task. We may be able to reuse it. | 706 | | - if (existingCallbackNode !== null) { | 707 | | - const existingCallbackPriority = root.callbackPriority; | 708 | | - if (existingCallbackPriority === newCallbackPriority) { | 709 | | - // The priority hasn't changed. We can reuse the existing task. Exit. | 710 | | - return; | | 708 | + const existingCallbackPriority = root.callbackPriority; | | 709 | + if (existingCallbackPriority === newCallbackPriority) { | | 710 | + if (__DEV__) { | | 711 | + // If we're going to re-use an existing task, it needs to exist. | | 712 | + // Assume that discrete update microtasks are non-cancellable and null. | | 713 | + // TODO: Temporary until we confirm this warning is not fired. | | 714 | + if ( | | 715 | + existingCallbackNode == null && | | 716 | + existingCallbackPriority !== InputDiscreteLanePriority | | 717 | + ) { | | 718 | + console.error( | | 719 | + 'Expected scheduled callback to exist. This error is likely caused by a bug in React. Please file an issue.', | | 720 | + ); | | 721 | + } | 711 | 722 | } | 712 | | - // The priority changed. Cancel the existing callback. We'll schedule a new | 713 | | - // one below. | | 723 | + // The priority hasn't changed. We can reuse the existing task. Exit. | | 724 | + return; | | 725 | + } | | 726 | + | | 727 | + if (existingCallbackNode != null) { | | 728 | + // Cancel the existing callback. We'll schedule a new one below. | 714 | 729 | cancelCallback(existingCallbackNode); | 715 | 730 | } | 716 | 731 |
| @@ -719,6 +734,8 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 719 | 734 | if (newCallbackPriority === SyncLanePriority) { | 720 | 735 | // Special case: Sync React callbacks are scheduled on a special | 721 | 736 | // internal queue | | 737 | + | | 738 | + // TODO: After enableDiscreteEventMicroTasks lands, we can remove the fake node. | 722 | 739 | newCallbackNode = scheduleSyncCallback( | 723 | 740 | performSyncWorkOnRoot.bind(null, root), | 724 | 741 | ); | @@ -727,6 +744,12 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 727 | 744 | ImmediateSchedulerPriority, | 728 | 745 | performSyncWorkOnRoot.bind(null, root), | 729 | 746 | ); | | 747 | + } else if ( | | 748 | + enableDiscreteEventMicroTasks && | | 749 | + newCallbackPriority === InputDiscreteLanePriority | | 750 | + ) { | | 751 | + queueMicrotask(performSyncWorkOnRoot.bind(null, root)); | | 752 | + newCallbackNode = null; | 730 | 753 | } else { | 731 | 754 | const schedulerPriorityLevel = lanePriorityToSchedulerPriority( | 732 | 755 | newCallbackPriority, | @@ -1851,6 +1874,7 @@ function commitRootImpl(root, renderPriorityLevel) { | 1851 | 1874 | // commitRoot never returns a continuation; it always finishes synchronously. | 1852 | 1875 | // So we can clear these now to allow a new callback to be scheduled. | 1853 | 1876 | root.callbackNode = null; | | 1877 | + root.callbackPriority = NoLanePriority; | 1854 | 1878 |
| 1855 | 1879 | // Update the first and last pending times on this root. The new first | 1856 | 1880 | // pending time is whatever is left on the root fiber. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3528,6 +3528,7 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 3528 | 3528 | }); | 3529 | 3529 |
| 3530 | 3530 | // @gate enableCache | | 3531 | + // @gate !enableDiscreteEventMicroTasks | 3531 | 3532 | it('regression: empty render at high priority causes update to be dropped', async () => { | 3532 | 3533 | // Reproduces a bug where flushDiscreteUpdates starts a new (empty) render | 3533 | 3534 | // pass which cancels a scheduled timeout and causes the fallback never to | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-test-renderer/src/ReactTestHostConfig.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,7 +228,7 @@ export const queueMicrotask = | 228 | 228 | Promise.resolve(null) | 229 | 229 | .then(callback) | 230 | 230 | .catch(handleErrorInNextTick) | 231 | | - : scheduleTimeout; | | 231 | + : scheduleTimeout; // TODO: Determine the best fallback here. | 232 | 232 |
| 233 | 233 | function handleErrorInNextTick(error) { | 234 | 234 | setTimeout(() => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactFeatureFlags.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -152,3 +152,5 @@ export const disableSchedulerTimeoutInWorkLoop = false; | 152 | 152 |
| 153 | 153 | // Experiment to simplify/improve how transitions are scheduled | 154 | 154 | export const enableTransitionEntanglement = false; | | 155 | + | | 156 | +export const enableDiscreteEventMicroTasks = false; |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-fb.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,7 @@ export const enableUseRefAccessWarning = false; | 59 | 59 | export const enableRecursiveCommitTraversal = false; | 60 | 60 | export const disableSchedulerTimeoutInWorkLoop = false; | 61 | 61 | export const enableTransitionEntanglement = false; | | 62 | +export const enableDiscreteEventMicroTasks = false; | 62 | 63 |
| 63 | 64 | // Flow magic to verify the exports of this file match the original version. | 64 | 65 | // eslint-disable-next-line no-unused-vars | |
|