Skip to content

Commit 0140bee

Browse files
committed
Queue discrete events in microtask
1 parent 39bd8c3 commit 0140bee

13 files changed

+47
-7
lines changed

packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,15 @@ describe('ChangeEventPlugin', () => {
730730

731731
// Flush callbacks.
732732
// 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+
}
735742
});
736743

737744
// @gate experimental

packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,24 @@ describe('SimpleEventPlugin', function() {
470470
'High-pri count: 7, Low-pri count: 0',
471471
]);
472472

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+
}
478491
expect(button.textContent).toEqual('High-pri count: 8, Low-pri count: 8');
479492
});
480493
});

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import {
9191
warnsIfNotActing,
9292
afterActiveInstanceBlur,
9393
clearContainer,
94+
queueMicrotask,
9495
} from './ReactFiberHostConfig';
9596

9697
import {
@@ -214,6 +215,7 @@ import {
214215
syncNestedUpdateFlag,
215216
} from './ReactProfilerTimer.new';
216217

218+
import {enableDiscreteEventMicroTasks} from 'shared/ReactFeatureFlags';
217219
// DEV stuff
218220
import getComponentName from 'shared/getComponentName';
219221
import ReactStrictModeWarnings from './ReactStrictModeWarnings.new';
@@ -726,6 +728,13 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
726728
ImmediateSchedulerPriority,
727729
performSyncWorkOnRoot.bind(null, root),
728730
);
731+
} else if (
732+
enableDiscreteEventMicroTasks &&
733+
queueMicrotask != null &&
734+
newCallbackPriority === InputDiscreteLanePriority
735+
) {
736+
queueMicrotask(performSyncWorkOnRoot.bind(null, root));
737+
newCallbackNode = null;
729738
} else {
730739
const schedulerPriorityLevel = lanePriorityToSchedulerPriority(
731740
newCallbackPriority,

packages/shared/ReactFeatureFlags.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,5 @@ export const enableUseRefAccessWarning = false;
143143
export const enableRecursiveCommitTraversal = false;
144144

145145
export const disableSchedulerTimeoutInWorkLoop = false;
146+
147+
export const enableDiscreteEventMicroTasks = false;

packages/shared/forks/ReactFeatureFlags.native-fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const enableUseRefAccessWarning = false;
5757

5858
export const enableRecursiveCommitTraversal = false;
5959
export const disableSchedulerTimeoutInWorkLoop = false;
60+
export const enableDiscreteEventMicroTasks = false;
6061

6162
// Flow magic to verify the exports of this file match the original version.
6263
// eslint-disable-next-line no-unused-vars

packages/shared/forks/ReactFeatureFlags.native-oss.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const enableUseRefAccessWarning = false;
5656

5757
export const enableRecursiveCommitTraversal = false;
5858
export const disableSchedulerTimeoutInWorkLoop = false;
59+
export const enableDiscreteEventMicroTasks = false;
5960

6061
// Flow magic to verify the exports of this file match the original version.
6162
// eslint-disable-next-line no-unused-vars

packages/shared/forks/ReactFeatureFlags.test-renderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const enableUseRefAccessWarning = false;
5656

5757
export const enableRecursiveCommitTraversal = false;
5858
export const disableSchedulerTimeoutInWorkLoop = false;
59+
export const enableDiscreteEventMicroTasks = false;
5960

6061
// Flow magic to verify the exports of this file match the original version.
6162
// eslint-disable-next-line no-unused-vars

packages/shared/forks/ReactFeatureFlags.test-renderer.native.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const enableUseRefAccessWarning = false;
5656

5757
export const enableRecursiveCommitTraversal = false;
5858
export const disableSchedulerTimeoutInWorkLoop = false;
59+
export const enableDiscreteEventMicroTasks = false;
5960

6061
// Flow magic to verify the exports of this file match the original version.
6162
// eslint-disable-next-line no-unused-vars

packages/shared/forks/ReactFeatureFlags.test-renderer.www.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const enableUseRefAccessWarning = false;
5656

5757
export const enableRecursiveCommitTraversal = false;
5858
export const disableSchedulerTimeoutInWorkLoop = false;
59+
export const enableDiscreteEventMicroTasks = false;
5960

6061
// Flow magic to verify the exports of this file match the original version.
6162
// eslint-disable-next-line no-unused-vars

packages/shared/forks/ReactFeatureFlags.testing.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const enableUseRefAccessWarning = false;
5656

5757
export const enableRecursiveCommitTraversal = false;
5858
export const disableSchedulerTimeoutInWorkLoop = false;
59+
export const enableDiscreteEventMicroTasks = false;
5960

6061
// Flow magic to verify the exports of this file match the original version.
6162
// eslint-disable-next-line no-unused-vars

0 commit comments

Comments
 (0)