Skip to content

Commit 0bc1715

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add flushSync to the Fabric renderer (#38475)
Summary: ## Summary Adds `flushSync` to the Fabric renderer (`ReactFabric.flushSync`) so React Native can export it from the `react-native` package. It's needed for cases like reading layout right after an update, or applying updates from a `ResizeObserver` callback on the same frame as the size change. The implementation is a port of `flushSync` from `react-dom` (`ReactDOMFlushSync.js`), with the same semantics: - It runs the callback with no transition and at discrete priority, then restores both. - It flushes pending sync work in a `finally`, so updates are flushed even if the callback throws. - It returns the callback's value, or `undefined` if no callback is passed. - In DEV, it logs the same error as `react-dom` when called while React is already rendering. `ReactFabricType` declares the two overloads as an intersection (`(<R>(fn: () => R) => R) & ((fn: void) => void)`), because two entries with the same name in a Flow object type collapse to the last one. On the React Native side, the export will be gated behind a canary-only feature flag (`enableFlushSync`). ## How did you test this change? New tests in `ReactFabric-test.internal.js` cover: - flushing before returning; - the return value; - opting out of transitions and restoring the transition context; - restoring priority when the callback throws; - the DEV error when called during commit; - flushing pending discrete work with no callback. I deleted each part of the implementation in turn, and every deletion made at least one test fail. ``` yarn test packages/react-native-renderer # 41/41 yarn test --prod packages/react-native-renderer # 41/41 yarn test -r=experimental packages/react-native-renderer # 41/41 yarn flow fabric # no errors yarn flow dom-node # no errors yarn linc && yarn prettier # clean ``` I also synced the built renderer (`yarn build react-native-renderer/fabric --type=RN_OSS_DEV,RN_OSS_PROD,RN_OSS_PROFILING`) into a local React Native checkout by hand and ran Fantom integration tests against it: - the same behaviors tested end to end, with a real discrete native event for the no-callback case; - a `ResizeObserver` test showing that updates flushed with `flushSync` in the observer callback are mounted in the same layout pass, while updates without it land in a later one. DiffTrain build for [7d301100cea02e3232f92f180fb6853191d0d16d](react/react@7d30110) Reviewed By: javache Differential Revision: D135611937
1 parent 7c1e281 commit 0bc1715

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

‎packages/react-native/Libraries/Renderer/shims/ReactNativeTypes.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noformat
88
* @nolint
99
* @flow strict
10-
* @generated SignedSource<<e98f2799fc8b1c559fda763a0c36b39f>>
10+
* @generated SignedSource<<f37cc5cc99a5347fb443d0f0dc736152>>
1111
*/
1212

1313
import type {
@@ -163,6 +163,7 @@ export type ReactFabricType = {
163163
options: ?RenderRootOptions,
164164
): ?React.ElementRef<React.ElementType>,
165165
unmountComponentAtNode(containerTag: number): void,
166+
readonly flushSync: (<R>(fn: () => R) => R) & ((fn: void) => void),
166167
getNodeFromInternalInstanceHandle(
167168
internalInstanceHandle: InternalInstanceHandle,
168169
): ?Node,

‎packages/react-native/ReactNativeApi.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<d85687fceb0705d7e07b3a0b4529a17f>>
7+
* @generated SignedSource<<3118333c996721b3ee66b227c47a7b63>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -3960,6 +3960,7 @@ declare type RCTNetworkingEventDefinitions = {
39603960
readonly didSendNetworkData: [[number, number, number]]
39613961
}
39623962
declare type ReactFabricType = {
3963+
readonly flushSync: (<R>(fn: () => R) => R) & ((fn: void) => void)
39633964
dispatchCommand(
39643965
handle: HostInstance,
39653966
command: string,

0 commit comments

Comments
 (0)