GeezerVison is showing github.com — [DOM] move `flushSync` out of the reconciler (#28500) · react/react@4c12339
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-art/src/ReactART.js +8-11Lines changed: 8 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,9 +10,9 @@ import ReactVersion from 'shared/ReactVersion'; | 10 | 10 | import {LegacyRoot, ConcurrentRoot} from 'react-reconciler/src/ReactRootTags'; | 11 | 11 | import { | 12 | 12 | createContainer, | 13 | | - updateContainer, | | 13 | + updateContainerSync, | 14 | 14 | injectIntoDevTools, | 15 | | - flushSync, | | 15 | + flushSyncWork, | 16 | 16 | } from 'react-reconciler/src/ReactFiberReconciler'; | 17 | 17 | import Transform from 'art/core/transform'; | 18 | 18 | import Mode from 'art/modes/current'; | @@ -78,9 +78,8 @@ class Surface extends React.Component { | 78 | 78 | ); | 79 | 79 | // We synchronously flush updates coming from above so that they commit together | 80 | 80 | // and so that refs resolve before the parent life cycles. | 81 | | - flushSync(() => { | 82 | | - updateContainer(this.props.children, this._mountNode, this); | 83 | | - }); | | 81 | + updateContainerSync(this.props.children, this._mountNode, this); | | 82 | + flushSyncWork(); | 84 | 83 | } | 85 | 84 |
| 86 | 85 | componentDidUpdate(prevProps, prevState) { | @@ -92,9 +91,8 @@ class Surface extends React.Component { | 92 | 91 |
| 93 | 92 | // We synchronously flush updates coming from above so that they commit together | 94 | 93 | // and so that refs resolve before the parent life cycles. | 95 | | - flushSync(() => { | 96 | | - updateContainer(this.props.children, this._mountNode, this); | 97 | | - }); | | 94 | + updateContainerSync(this.props.children, this._mountNode, this); | | 95 | + flushSyncWork(); | 98 | 96 |
| 99 | 97 | if (this._surface.render) { | 100 | 98 | this._surface.render(); | @@ -104,9 +102,8 @@ class Surface extends React.Component { | 104 | 102 | componentWillUnmount() { | 105 | 103 | // We synchronously flush updates coming from above so that they commit together | 106 | 104 | // and so that refs resolve before the parent life cycles. | 107 | | - flushSync(() => { | 108 | | - updateContainer(null, this._mountNode, this); | 109 | | - }); | | 105 | + updateContainerSync(null, this._mountNode, this); | | 106 | + flushSyncWork(); | 110 | 107 | } | 111 | 108 |
| 112 | 109 | render() { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,6 +90,7 @@ import { | 90 | 90 | enableScopeAPI, | 91 | 91 | enableTrustedTypesIntegration, | 92 | 92 | enableAsyncActions, | | 93 | + disableLegacyMode, | 93 | 94 | } from 'shared/ReactFeatureFlags'; | 94 | 95 | import { | 95 | 96 | HostComponent, | @@ -100,6 +101,7 @@ import { | 100 | 101 | import {listenToAllSupportedEvents} from '../events/DOMPluginEventSystem'; | 101 | 102 | import {validateLinkPropsForStyleResource} from '../shared/ReactDOMResourceValidation'; | 102 | 103 | import escapeSelectorAttributeValueInsideDoubleQuotes from './escapeSelectorAttributeValueInsideDoubleQuotes'; | | 104 | +import {flushSyncWork as flushSyncWorkOnAllRoots} from 'react-reconciler/src/ReactFiberWorkLoop'; | 103 | 105 |
| 104 | 106 | import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals'; | 105 | 107 | const ReactDOMCurrentDispatcher = | @@ -1924,6 +1926,9 @@ function getDocumentFromRoot(root: HoistableRoot): Document { | 1924 | 1926 |
| 1925 | 1927 | const previousDispatcher = ReactDOMCurrentDispatcher.current; | 1926 | 1928 | ReactDOMCurrentDispatcher.current = { | | 1929 | + flushSyncWork: disableLegacyMode | | 1930 | + ? flushSyncWork | | 1931 | + : previousDispatcher.flushSyncWork, | 1927 | 1932 | prefetchDNS, | 1928 | 1933 | preconnect, | 1929 | 1934 | preload, | @@ -1933,6 +1938,20 @@ ReactDOMCurrentDispatcher.current = { | 1933 | 1938 | preinitModuleScript, | 1934 | 1939 | }; | 1935 | 1940 |
| | 1941 | +function flushSyncWork() { | | 1942 | + if (disableLegacyMode) { | | 1943 | + const previousWasRendering = previousDispatcher.flushSyncWork(); | | 1944 | + const wasRendering = flushSyncWorkOnAllRoots(); | | 1945 | + // Since multiple dispatchers can flush sync work during a single flushSync call | | 1946 | + // we need to return true if any of them were rendering. | | 1947 | + return previousWasRendering || wasRendering; | | 1948 | + } else { | | 1949 | + throw new Error( | | 1950 | + 'flushSyncWork should not be called from builds that support legacy mode. This is a bug in React.', | | 1951 | + ); | | 1952 | + } | | 1953 | +} | | 1954 | + | 1936 | 1955 | // We expect this to get inlined. It is a function mostly to communicate the special nature of | 1937 | 1956 | // how we resolve the HoistableRoot for ReactDOM.pre*() methods. Because we support calling | 1938 | 1957 | // these methods outside of render there is no way to know which Document or ShadowRoot is 'scoped' | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom-bindings/src/events/ReactDOMUpdateBatching.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ import { | 13 | 13 | import { | 14 | 14 | batchedUpdates as batchedUpdatesImpl, | 15 | 15 | discreteUpdates as discreteUpdatesImpl, | 16 | | - flushSync as flushSyncImpl, | | 16 | + flushSyncWork, | 17 | 17 | } from 'react-reconciler/src/ReactFiberReconciler'; | 18 | 18 |
| 19 | 19 | // Used as a way to call batchedUpdates when we don't have a reference to | @@ -36,7 +36,9 @@ function finishEventHandler() { | 36 | 36 | // bails out of the update without touching the DOM. | 37 | 37 | // TODO: Restore state in the microtask, after the discrete updates flush, | 38 | 38 | // instead of early flushing them here. | 39 | | - flushSyncImpl(); | | 39 | + // @TODO Should move to flushSyncWork once legacy mode is removed but since this flushSync | | 40 | + // flushes passive effects we can't do this yet. | | 41 | + flushSyncWork(); | 40 | 42 | restoreStateIfNeeded(); | 41 | 43 | } | 42 | 44 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom-bindings/src/server/ReactDOMFlightServerHostDispatcher.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ const ReactDOMCurrentDispatcher = | 28 | 28 |
| 29 | 29 | const previousDispatcher = ReactDOMCurrentDispatcher.current; | 30 | 30 | ReactDOMCurrentDispatcher.current = { | | 31 | + flushSyncWork: previousDispatcher.flushSyncWork, | 31 | 32 | prefetchDNS, | 32 | 33 | preconnect, | 33 | 34 | preload, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,6 +88,7 @@ const ReactDOMCurrentDispatcher = | 88 | 88 |
| 89 | 89 | const previousDispatcher = ReactDOMCurrentDispatcher.current; | 90 | 90 | ReactDOMCurrentDispatcher.current = { | | 91 | + flushSyncWork: previousDispatcher.flushSyncWork, | 91 | 92 | prefetchDNS, | 92 | 93 | preconnect, | 93 | 94 | preload, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/ReactDOMSharedInternals.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ type InternalsType = { | 29 | 29 | function noop() {} | 30 | 30 |
| 31 | 31 | const DefaultDispatcher: HostDispatcher = { | | 32 | + flushSyncWork: noop, | 32 | 33 | prefetchDNS: noop, | 33 | 34 | preconnect: noop, | 34 | 35 | preload: noop, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOM.js +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,16 +14,18 @@ import type { | 14 | 14 | CreateRootOptions, | 15 | 15 | } from './ReactDOMRoot'; | 16 | 16 |
| | 17 | +import {disableLegacyMode} from 'shared/ReactFeatureFlags'; | 17 | 18 | import { | 18 | 19 | createRoot as createRootImpl, | 19 | 20 | hydrateRoot as hydrateRootImpl, | 20 | 21 | isValidContainer, | 21 | 22 | } from './ReactDOMRoot'; | 22 | 23 | import {createEventHandle} from 'react-dom-bindings/src/client/ReactDOMEventHandle'; | 23 | 24 | import {runWithPriority} from 'react-dom-bindings/src/client/ReactDOMUpdatePriority'; | | 25 | +import {flushSync as flushSyncIsomorphic} from '../shared/ReactDOMFlushSync'; | 24 | 26 |
| 25 | 27 | import { | 26 | | - flushSync as flushSyncWithoutWarningIfAlreadyRendering, | | 28 | + flushSyncFromReconciler as flushSyncWithoutWarningIfAlreadyRendering, | 27 | 29 | isAlreadyRendering, | 28 | 30 | injectIntoDevTools, | 29 | 31 | findHostInstance, | @@ -123,11 +125,11 @@ function hydrateRoot( | 123 | 125 |
| 124 | 126 | // Overload the definition to the two valid signatures. | 125 | 127 | // Warning, this opts-out of checking the function body. | 126 | | -declare function flushSync<R>(fn: () => R): R; | | 128 | +declare function flushSyncFromReconciler<R>(fn: () => R): R; | 127 | 129 | // eslint-disable-next-line no-redeclare | 128 | | -declare function flushSync(): void; | | 130 | +declare function flushSyncFromReconciler(): void; | 129 | 131 | // eslint-disable-next-line no-redeclare | 130 | | -function flushSync<R>(fn: (() => R) | void): R | void { | | 132 | +function flushSyncFromReconciler<R>(fn: (() => R) | void): R | void { | 131 | 133 | if (__DEV__) { | 132 | 134 | if (isAlreadyRendering()) { | 133 | 135 | console.error( | @@ -140,6 +142,10 @@ function flushSync<R>(fn: (() => R) | void): R | void { | 140 | 142 | return flushSyncWithoutWarningIfAlreadyRendering(fn); | 141 | 143 | } | 142 | 144 |
| | 145 | +const flushSync: typeof flushSyncIsomorphic = disableLegacyMode | | 146 | + ? flushSyncIsomorphic | | 147 | + : flushSyncFromReconciler; | | 148 | + | 143 | 149 | function findDOMNode( | 144 | 150 | componentOrElement: React$Component<any, any>, | 145 | 151 | ): null | Element | Text { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMRoot.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -93,7 +93,8 @@ import { | 93 | 93 | createContainer, | 94 | 94 | createHydrationContainer, | 95 | 95 | updateContainer, | 96 | | - flushSync, | | 96 | + updateContainerSync, | | 97 | + flushSyncWork, | 97 | 98 | isAlreadyRendering, | 98 | 99 | defaultOnUncaughtError, | 99 | 100 | defaultOnCaughtError, | @@ -161,9 +162,8 @@ ReactDOMHydrationRoot.prototype.unmount = ReactDOMRoot.prototype.unmount = | 161 | 162 | ); | 162 | 163 | } | 163 | 164 | } | 164 | | - flushSync(() => { | 165 | | - updateContainer(null, root, null, null); | 166 | | - }); | | 165 | + updateContainerSync(null, root, null, null); | | 166 | + flushSyncWork(); | 167 | 167 | unmarkContainerAsRoot(container); | 168 | 168 | } | 169 | 169 | }; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMRootFB.js +12-15Lines changed: 12 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,8 @@ import { | 49 | 49 | createHydrationContainer, | 50 | 50 | findHostInstanceWithNoPortals, | 51 | 51 | updateContainer, | 52 | | - flushSync, | | 52 | + updateContainerSync, | | 53 | + flushSyncWork, | 53 | 54 | getPublicRootInstance, | 54 | 55 | findHostInstance, | 55 | 56 | findHostInstanceWithWarning, | @@ -247,7 +248,7 @@ function legacyCreateRootFromDOMContainer( | 247 | 248 | // $FlowFixMe[incompatible-call] | 248 | 249 | listenToAllSupportedEvents(rootContainerElement); | 249 | 250 |
| 250 | | - flushSync(); | | 251 | + flushSyncWork(); | 251 | 252 | return root; | 252 | 253 | } else { | 253 | 254 | // First clear any existing content. | @@ -282,9 +283,8 @@ function legacyCreateRootFromDOMContainer( | 282 | 283 | listenToAllSupportedEvents(rootContainerElement); | 283 | 284 |
| 284 | 285 | // Initial mount should not be batched. | 285 | | - flushSync(() => { | 286 | | - updateContainer(initialChildren, root, parentComponent, callback); | 287 | | - }); | | 286 | + updateContainerSync(initialChildren, root, parentComponent, callback); | | 287 | + flushSyncWork(); | 288 | 288 |
| 289 | 289 | return root; | 290 | 290 | } | @@ -485,6 +485,8 @@ export function unmountComponentAtNode(container: Container): boolean { | 485 | 485 | } | 486 | 486 |
| 487 | 487 | if (container._reactRootContainer) { | | 488 | + const root = container._reactRootContainer; | | 489 | + | 488 | 490 | if (__DEV__) { | 489 | 491 | const rootEl = getReactRootElementInContainer(container); | 490 | 492 | const renderedByDifferentReact = rootEl && !getInstanceFromNode(rootEl); | @@ -496,16 +498,11 @@ export function unmountComponentAtNode(container: Container): boolean { | 496 | 498 | } | 497 | 499 | } | 498 | 500 |
| 499 | | - // Unmount should not be batched. | 500 | | - flushSync(() => { | 501 | | - legacyRenderSubtreeIntoContainer(null, null, container, false, () => { | 502 | | - // $FlowFixMe[incompatible-type] This should probably use `delete container._reactRootContainer` | 503 | | - container._reactRootContainer = null; | 504 | | - unmarkContainerAsRoot(container); | 505 | | - }); | 506 | | - }); | 507 | | - // If you call unmountComponentAtNode twice in quick succession, you'll | 508 | | - // get `true` twice. That's probably fine? | | 501 | + updateContainerSync(null, root, null, null); | | 502 | + flushSyncWork(); | | 503 | + // $FlowFixMe[incompatible-type] This should probably use `delete container._reactRootContainer` | | 504 | + container._reactRootContainer = null; | | 505 | + unmarkContainerAsRoot(container); | 509 | 506 | return true; | 510 | 507 | } else { | 511 | 508 | if (__DEV__) { | |
Collapse file Copy file name to clipboard +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. | | 3 | + * | | 4 | + * This source code is licensed under the MIT license found in the | | 5 | + * LICENSE file in the root directory of this source tree. | | 6 | + * | | 7 | + * @flow | | 8 | + */ | | 9 | + | | 10 | +import type {BatchConfig} from 'react/src/ReactCurrentBatchConfig'; | | 11 | + | | 12 | +import {disableLegacyMode} from 'shared/ReactFeatureFlags'; | | 13 | +import {DiscreteEventPriority} from 'react-reconciler/src/ReactEventPriorities'; | | 14 | + | | 15 | +import ReactSharedInternals from 'shared/ReactSharedInternals'; | | 16 | +const ReactCurrentBatchConfig: BatchConfig = | | 17 | + ReactSharedInternals.ReactCurrentBatchConfig; | | 18 | + | | 19 | +import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals'; | | 20 | +const ReactDOMCurrentDispatcher = | | 21 | + ReactDOMSharedInternals.ReactDOMCurrentDispatcher; | | 22 | + | | 23 | +declare function flushSyncImpl<R>(fn: () => R): R; | | 24 | +declare function flushSyncImpl(void): void; | | 25 | +function flushSyncImpl<R>(fn: (() => R) | void): R | void { | | 26 | + const previousTransition = ReactCurrentBatchConfig.transition; | | 27 | + const previousUpdatePriority = | | 28 | + ReactDOMSharedInternals.up; /* ReactDOMCurrentUpdatePriority */ | | 29 | + | | 30 | + try { | | 31 | + ReactCurrentBatchConfig.transition = null; | | 32 | + ReactDOMSharedInternals.up /* ReactDOMCurrentUpdatePriority */ = | | 33 | + DiscreteEventPriority; | | 34 | + if (fn) { | | 35 | + return fn(); | | 36 | + } else { | | 37 | + return undefined; | | 38 | + } | | 39 | + } finally { | | 40 | + ReactCurrentBatchConfig.transition = previousTransition; | | 41 | + ReactDOMSharedInternals.up /* ReactDOMCurrentUpdatePriority */ = | | 42 | + previousUpdatePriority; | | 43 | + const wasInRender = ReactDOMCurrentDispatcher.current.flushSyncWork(); | | 44 | + if (__DEV__) { | | 45 | + if (wasInRender) { | | 46 | + console.error( | | 47 | + 'flushSync was called from inside a lifecycle method. React cannot ' + | | 48 | + 'flush when React is already rendering. Consider moving this call to ' + | | 49 | + 'a scheduler task or micro task.', | | 50 | + ); | | 51 | + } | | 52 | + } | | 53 | + } | | 54 | +} | | 55 | + | | 56 | +declare function flushSyncErrorInBuildsThatSupportLegacyMode<R>(fn: () => R): R; | | 57 | +declare function flushSyncErrorInBuildsThatSupportLegacyMode(void): void; | | 58 | +function flushSyncErrorInBuildsThatSupportLegacyMode() { | | 59 | + // eslint-disable-next-line react-internal/prod-error-codes | | 60 | + throw new Error( | | 61 | + 'Expected this build of React to not support legacy mode but it does. This is a bug in React.', | | 62 | + ); | | 63 | +} | | 64 | + | | 65 | +export const flushSync: typeof flushSyncImpl = disableLegacyMode | | 66 | + ? flushSyncImpl | | 67 | + : flushSyncErrorInBuildsThatSupportLegacyMode; |
|