GeezerVison is showing github.com — Make time-slicing opt-in (#21072) · react/react@933880b
File treeExpand file treeCollapse file tree Open diff view settings create-subscription/src/__tests__ react-test-renderer/src/__tests__ use-subscription/src/__tests__
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/create-subscription/src/__tests__/createSubscription-test.js +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -268,6 +268,7 @@ describe('createSubscription', () => { | 268 | 268 | expect(Scheduler).toFlushAndYield(['b-1']); | 269 | 269 | }); | 270 | 270 |
| | 271 | + // @gate experimental || !enableSyncDefaultUpdates | 271 | 272 | it('should ignore values emitted by a new subscribable until the commit phase', () => { | 272 | 273 | const log = []; | 273 | 274 |
| @@ -325,7 +326,13 @@ describe('createSubscription', () => { | 325 | 326 | expect(log).toEqual(['Parent.componentDidMount']); | 326 | 327 |
| 327 | 328 | // Start React update, but don't finish | 328 | | - ReactNoop.render(<Parent observed={observableB} />); | | 329 | + if (gate(flags => flags.enableSyncDefaultUpdates)) { | | 330 | + React.unstable_startTransition(() => { | | 331 | + ReactNoop.render(<Parent observed={observableB} />); | | 332 | + }); | | 333 | + } else { | | 334 | + ReactNoop.render(<Parent observed={observableB} />); | | 335 | + } | 329 | 336 | expect(Scheduler).toFlushAndYieldThrough(['Subscriber: b-0']); | 330 | 337 | expect(log).toEqual(['Parent.componentDidMount']); | 331 | 338 |
| @@ -355,6 +362,7 @@ describe('createSubscription', () => { | 355 | 362 | ]); | 356 | 363 | }); | 357 | 364 |
| | 365 | + // @gate experimental || !enableSyncDefaultUpdates | 358 | 366 | it('should not drop values emitted between updates', () => { | 359 | 367 | const log = []; | 360 | 368 |
| @@ -412,7 +420,13 @@ describe('createSubscription', () => { | 412 | 420 | expect(log).toEqual(['Parent.componentDidMount']); | 413 | 421 |
| 414 | 422 | // Start React update, but don't finish | 415 | | - ReactNoop.render(<Parent observed={observableB} />); | | 423 | + if (gate(flags => flags.enableSyncDefaultUpdates)) { | | 424 | + React.unstable_startTransition(() => { | | 425 | + ReactNoop.render(<Parent observed={observableB} />); | | 426 | + }); | | 427 | + } else { | | 428 | + ReactNoop.render(<Parent observed={observableB} />); | | 429 | + } | 416 | 430 | expect(Scheduler).toFlushAndYieldThrough(['Subscriber: b-0']); | 417 | 431 | expect(log).toEqual(['Parent.componentDidMount']); | 418 | 432 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-art/src/__tests__/ReactART-test.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -360,6 +360,7 @@ describe('ReactART', () => { | 360 | 360 | expect(onClick2).toBeCalled(); | 361 | 361 | }); | 362 | 362 |
| | 363 | + // @gate !enableSyncDefaultUpdates | 363 | 364 | it('can concurrently render with a "primary" renderer while sharing context', () => { | 364 | 365 | const CurrentRendererContext = React.createContext(null); | 365 | 366 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -284,6 +284,46 @@ describe('ReactDOMNativeEventHeuristic-test', () => { | 284 | 284 | expect(container.textContent).toEqual('hovered'); | 285 | 285 | }); | 286 | 286 |
| | 287 | + // @gate experimental | | 288 | + it('continuous native events flush as expected', async () => { | | 289 | + const root = ReactDOM.unstable_createRoot(container); | | 290 | + | | 291 | + const target = React.createRef(null); | | 292 | + function Foo({hovered}) { | | 293 | + const hoverString = hovered ? 'hovered' : 'not hovered'; | | 294 | + Scheduler.unstable_yieldValue(hoverString); | | 295 | + return <div ref={target}>{hoverString}</div>; | | 296 | + } | | 297 | + | | 298 | + await act(async () => { | | 299 | + root.render(<Foo hovered={false} />); | | 300 | + }); | | 301 | + expect(container.textContent).toEqual('not hovered'); | | 302 | + | | 303 | + await act(async () => { | | 304 | + // Note: React does not use native mouseenter/mouseleave events | | 305 | + // but we should still correctly determine their priority. | | 306 | + const mouseEnterEvent = document.createEvent('MouseEvents'); | | 307 | + mouseEnterEvent.initEvent('mouseover', true, true); | | 308 | + target.current.addEventListener('mouseover', () => { | | 309 | + root.render(<Foo hovered={true} />); | | 310 | + }); | | 311 | + dispatchAndSetCurrentEvent(target.current, mouseEnterEvent); | | 312 | + | | 313 | + // Since mouse end is not discrete, should not have updated yet | | 314 | + expect(Scheduler).toHaveYielded(['not hovered']); | | 315 | + expect(container.textContent).toEqual('not hovered'); | | 316 | + | | 317 | + expect(Scheduler).toFlushAndYieldThrough(['hovered']); | | 318 | + if (gate(flags => flags.enableSyncDefaultUpdates)) { | | 319 | + expect(container.textContent).toEqual('hovered'); | | 320 | + } else { | | 321 | + expect(container.textContent).toEqual('not hovered'); | | 322 | + } | | 323 | + }); | | 324 | + expect(container.textContent).toEqual('hovered'); | | 325 | + }); | | 326 | + | 287 | 327 | // @gate experimental | 288 | 328 | it('should batch inside native events', async () => { | 289 | 329 | const root = ReactDOM.unstable_createRoot(container); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js +15-5Lines changed: 15 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1865,11 +1865,21 @@ describe('ReactDOMServerPartialHydration', () => { | 1865 | 1865 | suspend = true; | 1866 | 1866 |
| 1867 | 1867 | await act(async () => { | 1868 | | - root.render(<App />); | 1869 | | - expect(Scheduler).toFlushAndYieldThrough(['Before']); | 1870 | | - // This took a long time to render. | 1871 | | - Scheduler.unstable_advanceTime(1000); | 1872 | | - expect(Scheduler).toFlushAndYield(['After']); | | 1868 | + if (gate(flags => flags.enableSyncDefaultUpdates)) { | | 1869 | + React.unstable_startTransition(() => { | | 1870 | + root.render(<App />); | | 1871 | + }); | | 1872 | + | | 1873 | + expect(Scheduler).toFlushAndYieldThrough(['Before', 'After']); | | 1874 | + } else { | | 1875 | + root.render(<App />); | | 1876 | + | | 1877 | + expect(Scheduler).toFlushAndYieldThrough(['Before']); | | 1878 | + // This took a long time to render. | | 1879 | + Scheduler.unstable_advanceTime(1000); | | 1880 | + expect(Scheduler).toFlushAndYield(['After']); | | 1881 | + } | | 1882 | + | 1873 | 1883 | // This will cause us to skip the second row completely. | 1874 | 1884 | }); | 1875 | 1885 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/__tests__/DOMPluginEventSystem-test.internal.js +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1934,7 +1934,13 @@ describe('DOMPluginEventSystem', () => { | 1934 | 1934 | log.length = 0; | 1935 | 1935 |
| 1936 | 1936 | // Increase counter | 1937 | | - root.render(<Test counter={1} />); | | 1937 | + if (gate(flags => flags.enableSyncDefaultUpdates)) { | | 1938 | + React.unstable_startTransition(() => { | | 1939 | + root.render(<Test counter={1} />); | | 1940 | + }); | | 1941 | + } else { | | 1942 | + root.render(<Test counter={1} />); | | 1943 | + } | 1938 | 1944 | // Yield before committing | 1939 | 1945 | expect(Scheduler).toFlushAndYieldThrough(['Test']); | 1940 | 1946 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.new.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ export const NoLane: Lane = /* */ 0b0000000000000000000 | 52 | 52 |
| 53 | 53 | export const SyncLane: Lane = /* */ 0b0000000000000000000000000000001; | 54 | 54 |
| 55 | | -const InputContinuousHydrationLane: Lane = /* */ 0b0000000000000000000000000000010; | | 55 | +export const InputContinuousHydrationLane: Lane = /* */ 0b0000000000000000000000000000010; | 56 | 56 | export const InputContinuousLane: Lanes = /* */ 0b0000000000000000000000000000100; | 57 | 57 |
| 58 | 58 | export const DefaultHydrationLane: Lane = /* */ 0b0000000000000000000000000001000; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.old.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ export const NoLane: Lane = /* */ 0b0000000000000000000 | 52 | 52 |
| 53 | 53 | export const SyncLane: Lane = /* */ 0b0000000000000000000000000000001; | 54 | 54 |
| 55 | | -const InputContinuousHydrationLane: Lane = /* */ 0b0000000000000000000000000000010; | | 55 | +export const InputContinuousHydrationLane: Lane = /* */ 0b0000000000000000000000000000010; | 56 | 56 | export const InputContinuousLane: Lanes = /* */ 0b0000000000000000000000000000100; | 57 | 57 |
| 58 | 58 | export const DefaultHydrationLane: Lane = /* */ 0b0000000000000000000000000001000; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +34-2Lines changed: 34 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ import { | 32 | 32 | disableSchedulerTimeoutInWorkLoop, | 33 | 33 | enableStrictEffects, | 34 | 34 | skipUnmountedBoundaries, | | 35 | + enableSyncDefaultUpdates, | 35 | 36 | enableUpdaterTracking, | 36 | 37 | } from 'shared/ReactFeatureFlags'; | 37 | 38 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | @@ -138,6 +139,10 @@ import { | 138 | 139 | NoLanes, | 139 | 140 | NoLane, | 140 | 141 | SyncLane, | | 142 | + DefaultLane, | | 143 | + DefaultHydrationLane, | | 144 | + InputContinuousLane, | | 145 | + InputContinuousHydrationLane, | 141 | 146 | NoTimestamp, | 142 | 147 | claimNextTransitionLane, | 143 | 148 | claimNextRetryLane, | @@ -433,6 +438,13 @@ export function requestUpdateLane(fiber: Fiber): Lane { | 433 | 438 | // TODO: Move this type conversion to the event priority module. | 434 | 439 | const updateLane: Lane = (getCurrentUpdatePriority(): any); | 435 | 440 | if (updateLane !== NoLane) { | | 441 | + if ( | | 442 | + enableSyncDefaultUpdates && | | 443 | + (updateLane === InputContinuousLane || | | 444 | + updateLane === InputContinuousHydrationLane) | | 445 | + ) { | | 446 | + return DefaultLane; | | 447 | + } | 436 | 448 | return updateLane; | 437 | 449 | } | 438 | 450 |
| @@ -443,6 +455,13 @@ export function requestUpdateLane(fiber: Fiber): Lane { | 443 | 455 | // use that directly. | 444 | 456 | // TODO: Move this type conversion to the event priority module. | 445 | 457 | const eventLane: Lane = (getCurrentEventPriority(): any); | | 458 | + if ( | | 459 | + enableSyncDefaultUpdates && | | 460 | + (eventLane === InputContinuousLane || | | 461 | + eventLane === InputContinuousHydrationLane) | | 462 | + ) { | | 463 | + return DefaultLane; | | 464 | + } | 446 | 465 | return eventLane; | 447 | 466 | } | 448 | 467 |
| @@ -695,7 +714,16 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 695 | 714 |
| 696 | 715 | // Schedule a new callback. | 697 | 716 | let newCallbackNode; | 698 | | - if (newCallbackPriority === SyncLane) { | | 717 | + if ( | | 718 | + enableSyncDefaultUpdates && | | 719 | + (newCallbackPriority === DefaultLane || | | 720 | + newCallbackPriority === DefaultHydrationLane) | | 721 | + ) { | | 722 | + newCallbackNode = scheduleCallback( | | 723 | + ImmediateSchedulerPriority, | | 724 | + performSyncWorkOnRoot.bind(null, root), | | 725 | + ); | | 726 | + } else if (newCallbackPriority === SyncLane) { | 699 | 727 | // Special case: Sync React callbacks are scheduled on a special | 700 | 728 | // internal queue | 701 | 729 | scheduleSyncCallback(performSyncWorkOnRoot.bind(null, root)); | @@ -1030,7 +1058,11 @@ function performSyncWorkOnRoot(root) { | 1030 | 1058 | const finishedWork: Fiber = (root.current.alternate: any); | 1031 | 1059 | root.finishedWork = finishedWork; | 1032 | 1060 | root.finishedLanes = lanes; | 1033 | | - commitRoot(root); | | 1061 | + if (enableSyncDefaultUpdates && !includesSomeLane(lanes, SyncLane)) { | | 1062 | + finishConcurrentRender(root, exitStatus, lanes); | | 1063 | + } else { | | 1064 | + commitRoot(root); | | 1065 | + } | 1034 | 1066 |
| 1035 | 1067 | // Before exiting, make sure there's a callback scheduled for the next | 1036 | 1068 | // pending level. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +34-2Lines changed: 34 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ import { | 32 | 32 | disableSchedulerTimeoutInWorkLoop, | 33 | 33 | enableStrictEffects, | 34 | 34 | skipUnmountedBoundaries, | | 35 | + enableSyncDefaultUpdates, | 35 | 36 | enableUpdaterTracking, | 36 | 37 | } from 'shared/ReactFeatureFlags'; | 37 | 38 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | @@ -138,6 +139,10 @@ import { | 138 | 139 | NoLanes, | 139 | 140 | NoLane, | 140 | 141 | SyncLane, | | 142 | + DefaultLane, | | 143 | + DefaultHydrationLane, | | 144 | + InputContinuousLane, | | 145 | + InputContinuousHydrationLane, | 141 | 146 | NoTimestamp, | 142 | 147 | claimNextTransitionLane, | 143 | 148 | claimNextRetryLane, | @@ -433,6 +438,13 @@ export function requestUpdateLane(fiber: Fiber): Lane { | 433 | 438 | // TODO: Move this type conversion to the event priority module. | 434 | 439 | const updateLane: Lane = (getCurrentUpdatePriority(): any); | 435 | 440 | if (updateLane !== NoLane) { | | 441 | + if ( | | 442 | + enableSyncDefaultUpdates && | | 443 | + (updateLane === InputContinuousLane || | | 444 | + updateLane === InputContinuousHydrationLane) | | 445 | + ) { | | 446 | + return DefaultLane; | | 447 | + } | 436 | 448 | return updateLane; | 437 | 449 | } | 438 | 450 |
| @@ -443,6 +455,13 @@ export function requestUpdateLane(fiber: Fiber): Lane { | 443 | 455 | // use that directly. | 444 | 456 | // TODO: Move this type conversion to the event priority module. | 445 | 457 | const eventLane: Lane = (getCurrentEventPriority(): any); | | 458 | + if ( | | 459 | + enableSyncDefaultUpdates && | | 460 | + (eventLane === InputContinuousLane || | | 461 | + eventLane === InputContinuousHydrationLane) | | 462 | + ) { | | 463 | + return DefaultLane; | | 464 | + } | 446 | 465 | return eventLane; | 447 | 466 | } | 448 | 467 |
| @@ -695,7 +714,16 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 695 | 714 |
| 696 | 715 | // Schedule a new callback. | 697 | 716 | let newCallbackNode; | 698 | | - if (newCallbackPriority === SyncLane) { | | 717 | + if ( | | 718 | + enableSyncDefaultUpdates && | | 719 | + (newCallbackPriority === DefaultLane || | | 720 | + newCallbackPriority === DefaultHydrationLane) | | 721 | + ) { | | 722 | + newCallbackNode = scheduleCallback( | | 723 | + ImmediateSchedulerPriority, | | 724 | + performSyncWorkOnRoot.bind(null, root), | | 725 | + ); | | 726 | + } else if (newCallbackPriority === SyncLane) { | 699 | 727 | // Special case: Sync React callbacks are scheduled on a special | 700 | 728 | // internal queue | 701 | 729 | scheduleSyncCallback(performSyncWorkOnRoot.bind(null, root)); | @@ -1030,7 +1058,11 @@ function performSyncWorkOnRoot(root) { | 1030 | 1058 | const finishedWork: Fiber = (root.current.alternate: any); | 1031 | 1059 | root.finishedWork = finishedWork; | 1032 | 1060 | root.finishedLanes = lanes; | 1033 | | - commitRoot(root); | | 1061 | + if (enableSyncDefaultUpdates && !includesSomeLane(lanes, SyncLane)) { | | 1062 | + finishConcurrentRender(root, exitStatus, lanes); | | 1063 | + } else { | | 1064 | + commitRoot(root); | | 1065 | + } | 1034 | 1066 |
| 1035 | 1067 | // Before exiting, make sure there's a callback scheduled for the next | 1036 | 1068 | // pending level. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactDisableSchedulerTimeoutBasedOnReactExpirationTime-test.internal.js +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,7 @@ describe('ReactSuspenseList', () => { | 45 | 45 | return Component; | 46 | 46 | } | 47 | 47 |
| | 48 | + // @gate experimental || !enableSyncDefaultUpdates | 48 | 49 | it('appends rendering tasks to the end of the priority queue', async () => { | 49 | 50 | const A = createAsyncText('A'); | 50 | 51 | const B = createAsyncText('B'); | @@ -63,7 +64,13 @@ describe('ReactSuspenseList', () => { | 63 | 64 | root.render(<App show={false} />); | 64 | 65 | expect(Scheduler).toFlushAndYield([]); | 65 | 66 |
| 66 | | - root.render(<App show={true} />); | | 67 | + if (gate(flags => flags.enableSyncDefaultUpdates)) { | | 68 | + React.unstable_startTransition(() => { | | 69 | + root.render(<App show={true} />); | | 70 | + }); | | 71 | + } else { | | 72 | + root.render(<App show={true} />); | | 73 | + } | 67 | 74 | expect(Scheduler).toFlushAndYield([ | 68 | 75 | 'Suspend! [A]', | 69 | 76 | 'Suspend! [B]', | |
|