-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements flushSync in react-dom without relying on the reconciler. …
…This will only be available in builds that no longer support legacy mode because the reconciler flushSync has special logic for legacy mode which is not necessary for concurrent roots. This is one option for how we might recover existing flushSync priorities with an external flushSync implementation. To reduce hidden class checks we need to read the batch config once which requires inlining the requestCurrentTransition implementation. Moves the legacy implementation of flushSync to the fb entrypoint The cost of the separate file is not really warranted. Will use feature flag to scope build specific implementations Moved error to fiber config. The reconciler implementation should be DCE'd in builds that still support legacy mode Exposes an updateContainerSync implementation so we can avoid depending on flushSyncFromReconciler in hot reloading Removes flushSyncFromReconciler from ReactDOMRoot Removes flushSyncFromReconciler from ReactDOMUpdateBatching Removes flushSyncFromReconciler use from ReactFiberReconciler Removes flushSyncFromReconciler from ReactART Rather than use event priority or lane type semantics for the batch config it now uses a boolean. There really is only a binary of sync or not sync so we don't need to express this concept as something overly specific to Fiber. simplifies the during render case to in dev to avoid a bit of extra work. There is no harm in returning true in prod even if there is nothing to react to it.
- Loading branch information
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.