Repository navigation
Commit 0bc1715
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: D1356119371 parent 7c1e281 commit 0bc1715
2 files changed
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
166 | 167 | | |
167 | 168 | | |
168 | 169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
3960 | 3960 | | |
3961 | 3961 | | |
3962 | 3962 | | |
| 3963 | + | |
3963 | 3964 | | |
3964 | 3965 | | |
3965 | 3966 | | |
| |||
0 commit comments