Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flushSync API #3308

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/API/additional-exports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ nav: 9
| addTail | Adds a global callback which is called when rendering stops |
| buildGraph | Collects nodes and materials from a THREE.Object3D |
| flushGlobalEffects | Flushes global render-effects for when manually driving a loop |
| flushSync | Force React to flush any updates synchronously and immediately |
| invalidate | Forces view global invalidation |
| advance | Advances the frameloop (given that it's set to 'never') |
| extend | Extends the native-object catalogue |
Expand Down
12 changes: 12 additions & 0 deletions packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,17 @@ function Portal({
)
}

/**
* Force React to flush any updates inside the provided callback synchronously and immediately.
* All the same caveats documented for react-dom's `flushSync` apply here (see https://react.dev/reference/react-dom/flushSync).
* Nevertheless, sometimes one needs to render synchronously, for example to keep DOM and 3D changes in lock-step without
* having to revert to a non-React solution.
*/
function flushSync<R>(fn: () => R): R {
// `flushSync` implementation only takes one argument. I don't know what's up with the type declaration for it.
return reconciler.flushSync(fn, undefined)
}

reconciler.injectIntoDevTools({
bundleType: process.env.NODE_ENV === 'production' ? 0 : 1,
rendererPackageName: '@react-three/fiber',
Expand All @@ -622,6 +633,7 @@ export {
addAfterEffect,
addTail,
flushGlobalEffects,
flushSync,
getRootState,
act,
buildGraph,
Expand Down