Skip to content

Commit 71c4ee4

Browse files
committed
feat: add flushSync API
1 parent 9931589 commit 71c4ee4

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

docs/API/additional-exports.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nav: 9
1010
| addTail | Adds a global callback which is called when rendering stops |
1111
| buildGraph | Collects nodes and materials from a THREE.Object3D |
1212
| flushGlobalEffects | Flushes global render-effects for when manually driving a loop |
13+
| flushSync | Force React to flush any updates synchronously and immediately |
1314
| invalidate | Forces view global invalidation |
1415
| advance | Advances the frameloop (given that it's set to 'never') |
1516
| extend | Extends the native-object catalogue |

packages/fiber/src/core/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,17 @@ function Portal({
597597
)
598598
}
599599

600+
/**
601+
* Force React to flush any updates inside the provided callback synchronously and immediately.
602+
* All the same caveats documented for react-dom's `flushSync` apply here (see https://react.dev/reference/react-dom/flushSync).
603+
* Nevertheless, sometimes one needs to render synchronously, for example to keep DOM and 3D changes in lock-step without
604+
* having to revert to a non-React solution.
605+
*/
606+
function flushSync<R>(fn: () => R): R {
607+
// `flushSync` implementation only takes one argument. I don't know what's up with the type declaration for it.
608+
return reconciler.flushSync(fn, undefined)
609+
}
610+
600611
reconciler.injectIntoDevTools({
601612
bundleType: process.env.NODE_ENV === 'production' ? 0 : 1,
602613
rendererPackageName: '@react-three/fiber',
@@ -622,6 +633,7 @@ export {
622633
addAfterEffect,
623634
addTail,
624635
flushGlobalEffects,
636+
flushSync,
625637
getRootState,
626638
act,
627639
buildGraph,

0 commit comments

Comments
 (0)