GeezerVison is showing github.com — Add unstable_concurrentUpdatesByDefault (#21227) · react/react@9e9dac6
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMRoot-test.js +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ let React = require('react'); | 13 | 13 | let ReactDOM = require('react-dom'); | 14 | 14 | let ReactDOMServer = require('react-dom/server'); | 15 | 15 | let Scheduler = require('scheduler'); | | 16 | +let act; | 16 | 17 |
| 17 | 18 | describe('ReactDOMRoot', () => { | 18 | 19 | let container; | @@ -24,6 +25,7 @@ describe('ReactDOMRoot', () => { | 24 | 25 | ReactDOM = require('react-dom'); | 25 | 26 | ReactDOMServer = require('react-dom/server'); | 26 | 27 | Scheduler = require('scheduler'); | | 28 | + act = require('react-dom/test-utils').unstable_concurrentAct; | 27 | 29 | }); | 28 | 30 |
| 29 | 31 | if (!__EXPERIMENTAL__) { | @@ -316,4 +318,37 @@ describe('ReactDOMRoot', () => { | 316 | 318 | {withoutStack: true}, | 317 | 319 | ); | 318 | 320 | }); | | 321 | + | | 322 | + // @gate experimental | | 323 | + it('opts-in to concurrent default updates', async () => { | | 324 | + const root = ReactDOM.unstable_createRoot(container, { | | 325 | + unstable_concurrentUpdatesByDefault: true, | | 326 | + }); | | 327 | + | | 328 | + function Foo({value}) { | | 329 | + Scheduler.unstable_yieldValue(value); | | 330 | + return <div>{value}</div>; | | 331 | + } | | 332 | + | | 333 | + await act(async () => { | | 334 | + root.render(<Foo value="a" />); | | 335 | + }); | | 336 | + | | 337 | + expect(container.textContent).toEqual('a'); | | 338 | + | | 339 | + await act(async () => { | | 340 | + root.render(<Foo value="b" />); | | 341 | + | | 342 | + expect(Scheduler).toHaveYielded(['a']); | | 343 | + expect(container.textContent).toEqual('a'); | | 344 | + | | 345 | + expect(Scheduler).toFlushAndYieldThrough(['b']); | | 346 | + if (gate(flags => flags.allowConcurrentByDefault)) { | | 347 | + expect(container.textContent).toEqual('a'); | | 348 | + } else { | | 349 | + expect(container.textContent).toEqual('b'); | | 350 | + } | | 351 | + }); | | 352 | + expect(container.textContent).toEqual('b'); | | 353 | + }); | 319 | 354 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMRoot.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ export type RootOptions = { | 28 | 28 | ... | 29 | 29 | }, | 30 | 30 | unstable_strictModeLevel?: number, | | 31 | + unstable_concurrentUpdatesByDefault?: boolean, | 31 | 32 | ... | 32 | 33 | }; | 33 | 34 |
| @@ -52,6 +53,7 @@ import { | 52 | 53 | } from 'react-reconciler/src/ReactFiberReconciler'; | 53 | 54 | import invariant from 'shared/invariant'; | 54 | 55 | import {ConcurrentRoot, LegacyRoot} from 'react-reconciler/src/ReactRootTags'; | | 56 | +import {allowConcurrentByDefault} from 'shared/ReactFeatureFlags'; | 55 | 57 |
| 56 | 58 | function ReactDOMRoot(container: Container, options: void | RootOptions) { | 57 | 59 | this._internalRoot = createRootImpl(container, ConcurrentRoot, options); | @@ -126,12 +128,21 @@ function createRootImpl( | 126 | 128 | ? options.unstable_strictModeLevel | 127 | 129 | : null; | 128 | 130 |
| | 131 | + let concurrentUpdatesByDefaultOverride = null; | | 132 | + if (allowConcurrentByDefault) { | | 133 | + concurrentUpdatesByDefaultOverride = | | 134 | + options != null && options.unstable_concurrentUpdatesByDefault != null | | 135 | + ? options.unstable_concurrentUpdatesByDefault | | 136 | + : null; | | 137 | + } | | 138 | + | 129 | 139 | const root = createContainer( | 130 | 140 | container, | 131 | 141 | tag, | 132 | 142 | hydrate, | 133 | 143 | hydrationCallbacks, | 134 | 144 | strictModeLevelOverride, | | 145 | + concurrentUpdatesByDefaultOverride, | 135 | 146 | ); | 136 | 147 | markContainerAsRoot(root.current, container); | 137 | 148 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactFabric.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -207,7 +207,7 @@ function render( | 207 | 207 | if (!root) { | 208 | 208 | // TODO (bvaughn): If we decide to keep the wrapper component, | 209 | 209 | // We could create a wrapper for containerTag as well to reduce special casing. | 210 | | - root = createContainer(containerTag, LegacyRoot, false, null, null); | | 210 | + root = createContainer(containerTag, LegacyRoot, false, null, null, null); | 211 | 211 | roots.set(containerTag, root); | 212 | 212 | } | 213 | 213 | updateContainer(element, root, null, callback); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactNativeRenderer.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -203,7 +203,7 @@ function render( | 203 | 203 | if (!root) { | 204 | 204 | // TODO (bvaughn): If we decide to keep the wrapper component, | 205 | 205 | // We could create a wrapper for containerTag as well to reduce special casing. | 206 | | - root = createContainer(containerTag, LegacyRoot, false, null, null); | | 206 | + root = createContainer(containerTag, LegacyRoot, false, null, null, null); | 207 | 207 | roots.set(containerTag, root); | 208 | 208 | } | 209 | 209 | updateContainer(element, root, null, callback); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.new.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,8 @@ import { | 24 | 24 | enableStrictEffects, | 25 | 25 | enableProfilerTimer, | 26 | 26 | enableScopeAPI, | | 27 | + enableSyncDefaultUpdates, | | 28 | + allowConcurrentByDefault, | 27 | 29 | } from 'shared/ReactFeatureFlags'; | 28 | 30 | import {NoFlags, Placement, StaticMask} from './ReactFiberFlags'; | 29 | 31 | import {ConcurrentRoot} from './ReactRootTags'; | @@ -68,6 +70,7 @@ import { | 68 | 70 | ProfileMode, | 69 | 71 | StrictLegacyMode, | 70 | 72 | StrictEffectsMode, | | 73 | + ConcurrentUpdatesByDefaultMode, | 71 | 74 | } from './ReactTypeOfMode'; | 72 | 75 | import { | 73 | 76 | REACT_FORWARD_REF_TYPE, | @@ -420,6 +423,7 @@ export function resetWorkInProgress(workInProgress: Fiber, renderLanes: Lanes) { | 420 | 423 | export function createHostRootFiber( | 421 | 424 | tag: RootTag, | 422 | 425 | strictModeLevelOverride: null | number, | | 426 | + concurrentUpdatesByDefaultOverride: null | boolean, | 423 | 427 | ): Fiber { | 424 | 428 | let mode; | 425 | 429 | if (tag === ConcurrentRoot) { | @@ -440,6 +444,15 @@ export function createHostRootFiber( | 440 | 444 | mode |= StrictLegacyMode; | 441 | 445 | } | 442 | 446 | } | | 447 | + if ( | | 448 | + // We only use this flag for our repo tests to check both behaviors. | | 449 | + // TODO: Flip this flag and rename it something like "forceConcurrentByDefaultForTesting" | | 450 | + !enableSyncDefaultUpdates || | | 451 | + // Only for internal experiments. | | 452 | + (allowConcurrentByDefault && concurrentUpdatesByDefaultOverride) | | 453 | + ) { | | 454 | + mode |= ConcurrentUpdatesByDefaultMode; | | 455 | + } | 443 | 456 | } else { | 444 | 457 | mode = NoMode; | 445 | 458 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.old.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,8 @@ import { | 24 | 24 | enableStrictEffects, | 25 | 25 | enableProfilerTimer, | 26 | 26 | enableScopeAPI, | | 27 | + enableSyncDefaultUpdates, | | 28 | + allowConcurrentByDefault, | 27 | 29 | } from 'shared/ReactFeatureFlags'; | 28 | 30 | import {NoFlags, Placement, StaticMask} from './ReactFiberFlags'; | 29 | 31 | import {ConcurrentRoot} from './ReactRootTags'; | @@ -68,6 +70,7 @@ import { | 68 | 70 | ProfileMode, | 69 | 71 | StrictLegacyMode, | 70 | 72 | StrictEffectsMode, | | 73 | + ConcurrentUpdatesByDefaultMode, | 71 | 74 | } from './ReactTypeOfMode'; | 72 | 75 | import { | 73 | 76 | REACT_FORWARD_REF_TYPE, | @@ -420,6 +423,7 @@ export function resetWorkInProgress(workInProgress: Fiber, renderLanes: Lanes) { | 420 | 423 | export function createHostRootFiber( | 421 | 424 | tag: RootTag, | 422 | 425 | strictModeLevelOverride: null | number, | | 426 | + concurrentUpdatesByDefaultOverride: null | boolean, | 423 | 427 | ): Fiber { | 424 | 428 | let mode; | 425 | 429 | if (tag === ConcurrentRoot) { | @@ -440,6 +444,15 @@ export function createHostRootFiber( | 440 | 444 | mode |= StrictLegacyMode; | 441 | 445 | } | 442 | 446 | } | | 447 | + if ( | | 448 | + // We only use this flag for our repo tests to check both behaviors. | | 449 | + // TODO: Flip this flag and rename it something like "forceConcurrentByDefaultForTesting" | | 450 | + !enableSyncDefaultUpdates || | | 451 | + // Only for internal experiments. | | 452 | + (allowConcurrentByDefault && concurrentUpdatesByDefaultOverride) | | 453 | + ) { | | 454 | + mode |= ConcurrentUpdatesByDefaultMode; | | 455 | + } | 443 | 456 | } else { | 444 | 457 | mode = NoMode; | 445 | 458 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.new.js +20-14Lines changed: 20 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,9 +39,10 @@ import { | 39 | 39 | enableCache, | 40 | 40 | enableSchedulingProfiler, | 41 | 41 | enableUpdaterTracking, | 42 | | - enableSyncDefaultUpdates, | | 42 | + allowConcurrentByDefault, | 43 | 43 | } from 'shared/ReactFeatureFlags'; | 44 | 44 | import {isDevToolsPresent} from './ReactFiberDevToolsHook.new'; | | 45 | +import {ConcurrentUpdatesByDefaultMode, NoMode} from './ReactTypeOfMode'; | 45 | 46 |
| 46 | 47 | export const SyncLanePriority: LanePriority = 12; | 47 | 48 |
| @@ -318,11 +319,12 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes { | 318 | 319 | } | 319 | 320 |
| 320 | 321 | if ( | 321 | | - // TODO: Check for root override, once that lands | 322 | | - enableSyncDefaultUpdates && | 323 | | - (nextLanes & InputContinuousLane) !== NoLanes | | 322 | + allowConcurrentByDefault && | | 323 | + (root.current.mode & ConcurrentUpdatesByDefaultMode) !== NoMode | 324 | 324 | ) { | 325 | | - // When updates are sync by default, we entangle continous priority updates | | 325 | + // Do nothing, use the lanes as they were assigned. | | 326 | + } else if ((nextLanes & InputContinuousLane) !== NoLanes) { | | 327 | + // When updates are sync by default, we entangle continuous priority updates | 326 | 328 | // and default updates, so they render in the same batch. The only reason | 327 | 329 | // they use separate lanes is because continuous updates should interrupt | 328 | 330 | // transitions, but default updates should not. | @@ -527,17 +529,21 @@ export function shouldTimeSlice(root: FiberRoot, lanes: Lanes) { | 527 | 529 | // finish rendering without yielding execution. | 528 | 530 | return false; | 529 | 531 | } | 530 | | - if (enableSyncDefaultUpdates) { | 531 | | - const SyncDefaultLanes = | 532 | | - InputContinuousHydrationLane | | 533 | | - InputContinuousLane | | 534 | | - DefaultHydrationLane | | 535 | | - DefaultLane; | 536 | | - // TODO: Check for root override, once that lands | 537 | | - return (lanes & SyncDefaultLanes) === NoLanes; | 538 | | - } else { | | 532 | + | | 533 | + if ( | | 534 | + allowConcurrentByDefault && | | 535 | + (root.current.mode & ConcurrentUpdatesByDefaultMode) !== NoMode | | 536 | + ) { | | 537 | + // Concurrent updates by default always use time slicing. | 539 | 538 | return true; | 540 | 539 | } | | 540 | + | | 541 | + const SyncDefaultLanes = | | 542 | + InputContinuousHydrationLane | | | 543 | + InputContinuousLane | | | 544 | + DefaultHydrationLane | | | 545 | + DefaultLane; | | 546 | + return (lanes & SyncDefaultLanes) === NoLanes; | 541 | 547 | } | 542 | 548 |
| 543 | 549 | export function isTransitionLane(lane: Lane) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.old.js +20-14Lines changed: 20 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,9 +39,10 @@ import { | 39 | 39 | enableCache, | 40 | 40 | enableSchedulingProfiler, | 41 | 41 | enableUpdaterTracking, | 42 | | - enableSyncDefaultUpdates, | | 42 | + allowConcurrentByDefault, | 43 | 43 | } from 'shared/ReactFeatureFlags'; | 44 | 44 | import {isDevToolsPresent} from './ReactFiberDevToolsHook.old'; | | 45 | +import {ConcurrentUpdatesByDefaultMode, NoMode} from './ReactTypeOfMode'; | 45 | 46 |
| 46 | 47 | export const SyncLanePriority: LanePriority = 12; | 47 | 48 |
| @@ -318,11 +319,12 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes { | 318 | 319 | } | 319 | 320 |
| 320 | 321 | if ( | 321 | | - // TODO: Check for root override, once that lands | 322 | | - enableSyncDefaultUpdates && | 323 | | - (nextLanes & InputContinuousLane) !== NoLanes | | 322 | + allowConcurrentByDefault && | | 323 | + (root.current.mode & ConcurrentUpdatesByDefaultMode) !== NoMode | 324 | 324 | ) { | 325 | | - // When updates are sync by default, we entangle continous priority updates | | 325 | + // Do nothing, use the lanes as they were assigned. | | 326 | + } else if ((nextLanes & InputContinuousLane) !== NoLanes) { | | 327 | + // When updates are sync by default, we entangle continuous priority updates | 326 | 328 | // and default updates, so they render in the same batch. The only reason | 327 | 329 | // they use separate lanes is because continuous updates should interrupt | 328 | 330 | // transitions, but default updates should not. | @@ -527,17 +529,21 @@ export function shouldTimeSlice(root: FiberRoot, lanes: Lanes) { | 527 | 529 | // finish rendering without yielding execution. | 528 | 530 | return false; | 529 | 531 | } | 530 | | - if (enableSyncDefaultUpdates) { | 531 | | - const SyncDefaultLanes = | 532 | | - InputContinuousHydrationLane | | 533 | | - InputContinuousLane | | 534 | | - DefaultHydrationLane | | 535 | | - DefaultLane; | 536 | | - // TODO: Check for root override, once that lands | 537 | | - return (lanes & SyncDefaultLanes) === NoLanes; | 538 | | - } else { | | 532 | + | | 533 | + if ( | | 534 | + allowConcurrentByDefault && | | 535 | + (root.current.mode & ConcurrentUpdatesByDefaultMode) !== NoMode | | 536 | + ) { | | 537 | + // Concurrent updates by default always use time slicing. | 539 | 538 | return true; | 540 | 539 | } | | 540 | + | | 541 | + const SyncDefaultLanes = | | 542 | + InputContinuousHydrationLane | | | 543 | + InputContinuousLane | | | 544 | + DefaultHydrationLane | | | 545 | + DefaultLane; | | 546 | + return (lanes & SyncDefaultLanes) === NoLanes; | 541 | 547 | } | 542 | 548 |
| 543 | 549 | export function isTransitionLane(lane: Lane) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.new.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -249,13 +249,15 @@ export function createContainer( | 249 | 249 | hydrate: boolean, | 250 | 250 | hydrationCallbacks: null | SuspenseHydrationCallbacks, | 251 | 251 | strictModeLevelOverride: null | number, | | 252 | + concurrentUpdatesByDefaultOverride: null | boolean, | 252 | 253 | ): OpaqueRoot { | 253 | 254 | return createFiberRoot( | 254 | 255 | containerInfo, | 255 | 256 | tag, | 256 | 257 | hydrate, | 257 | 258 | hydrationCallbacks, | 258 | 259 | strictModeLevelOverride, | | 260 | + concurrentUpdatesByDefaultOverride, | 259 | 261 | ); | 260 | 262 | } | 261 | 263 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.old.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -249,13 +249,15 @@ export function createContainer( | 249 | 249 | hydrate: boolean, | 250 | 250 | hydrationCallbacks: null | SuspenseHydrationCallbacks, | 251 | 251 | strictModeLevelOverride: null | number, | | 252 | + concurrentUpdatesByDefaultOverride: null | boolean, | 252 | 253 | ): OpaqueRoot { | 253 | 254 | return createFiberRoot( | 254 | 255 | containerInfo, | 255 | 256 | tag, | 256 | 257 | hydrate, | 257 | 258 | hydrationCallbacks, | 258 | 259 | strictModeLevelOverride, | | 260 | + concurrentUpdatesByDefaultOverride, | 259 | 261 | ); | 260 | 262 | } | 261 | 263 |
| |
|