-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOM] move
flushSync
out of the reconciler (#28500)
This PR moves `flushSync` out of the reconciler. there is still an internal implementation that is used when these semantics are needed for React methods such as `unmount` on roots. This new isomorphic `flushSync` is only used in builds that no longer support legacy mode. Additionally all the internal uses of flushSync in the reconciler have been replaced with more direct methods. There is a new `updateContainerSync` method which updates a container but forces it to the Sync lane and flushes passive effects if necessary. This combined with flushSyncWork can be used to replace flushSync for all instances of internal usage. We still maintain the original flushSync implementation as `flushSyncFromReconciler` because it will be used as the flushSync implementation for FB builds. This is because it has special legacy mode handling that the new isomorphic implementation does not need to consider. It will be removed from production OSS builds by closure though
- Loading branch information
1 parent
24ceaa9
commit f35b382
Showing
18 changed files
with
248 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {BatchConfig} from 'react/src/ReactCurrentBatchConfig'; | ||
|
||
import {disableLegacyMode} from 'shared/ReactFeatureFlags'; | ||
import {DiscreteEventPriority} from 'react-reconciler/src/ReactEventPriorities'; | ||
|
||
import ReactSharedInternals from 'shared/ReactSharedInternals'; | ||
const ReactCurrentBatchConfig: BatchConfig = | ||
ReactSharedInternals.ReactCurrentBatchConfig; | ||
|
||
import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals'; | ||
const ReactDOMCurrentDispatcher = | ||
ReactDOMSharedInternals.ReactDOMCurrentDispatcher; | ||
|
||
declare function flushSyncImpl<R>(fn: () => R): R; | ||
declare function flushSyncImpl(void): void; | ||
function flushSyncImpl<R>(fn: (() => R) | void): R | void { | ||
const previousTransition = ReactCurrentBatchConfig.transition; | ||
const previousUpdatePriority = | ||
ReactDOMSharedInternals.up; /* ReactDOMCurrentUpdatePriority */ | ||
|
||
try { | ||
ReactCurrentBatchConfig.transition = null; | ||
ReactDOMSharedInternals.up /* ReactDOMCurrentUpdatePriority */ = | ||
DiscreteEventPriority; | ||
if (fn) { | ||
return fn(); | ||
} else { | ||
return undefined; | ||
} | ||
} finally { | ||
ReactCurrentBatchConfig.transition = previousTransition; | ||
ReactDOMSharedInternals.up /* ReactDOMCurrentUpdatePriority */ = | ||
previousUpdatePriority; | ||
const wasInRender = ReactDOMCurrentDispatcher.current.flushSyncWork(); | ||
if (__DEV__) { | ||
if (wasInRender) { | ||
console.error( | ||
'flushSync was called from inside a lifecycle method. React cannot ' + | ||
'flush when React is already rendering. Consider moving this call to ' + | ||
'a scheduler task or micro task.', | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
declare function flushSyncErrorInBuildsThatSupportLegacyMode<R>(fn: () => R): R; | ||
declare function flushSyncErrorInBuildsThatSupportLegacyMode(void): void; | ||
function flushSyncErrorInBuildsThatSupportLegacyMode() { | ||
// eslint-disable-next-line react-internal/prod-error-codes | ||
throw new Error( | ||
'Expected this build of React to not support legacy mode but it does. This is a bug in React.', | ||
); | ||
} | ||
|
||
export const flushSync: typeof flushSyncImpl = disableLegacyMode | ||
? flushSyncImpl | ||
: flushSyncErrorInBuildsThatSupportLegacyMode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.