Skip to content

Conversation

@a28689604
Copy link

@a28689604 a28689604 commented Oct 19, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

Bugs / Features

What is the current behavior?

A new debounced onFinishAutoSave is created on re-renders, which resets the debounce window and can cancel pending autosaves; in rapid re-renders (e.g., typing), autosave may not fire reliably. Pending timers aren’t explicitly cancelled on unmount.

What is the new behavior?

onFinishAutoSave is memoized (one instance per debounce delay), invokes the latest onFinish via a ref, and cancels pending work on unmount. The debounce window is preserved across renders, making autosave reliable and preventing stray timers.

fixes #7082

Notes for reviewers

This is my first open-source contribution to the project, happy to adjust anything based on your feedback.

@a28689604 a28689604 requested a review from a team as a code owner October 19, 2025 12:33
@changeset-bot
Copy link

changeset-bot bot commented Oct 19, 2025

🦋 Changeset detected

Latest commit: 7316925

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@refinedev/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Oct 19, 2025

Deploy Preview for refine-doc-live-previews ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 7316925
🔍 Latest deploy log https://app.netlify.com/projects/refine-doc-live-previews/deploys/68f91a95ff8d330008da9f6f
😎 Deploy Preview https://deploy-preview-7083--refine-doc-live-previews.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@a28689604 a28689604 force-pushed the fix/core-useform-stable-autosave-debounce branch from a10df67 to 38d322d Compare October 19, 2025 12:56
const onFinishRef = React.useRef(onFinish);
React.useEffect(() => {
onFinishRef.current = onFinish;
}, [onFinish]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea to prevent asyncDebounce from using a stale onFinish. But, if I'm not wrong, useEffect will be triggered on every (re-)render since onFinish is not memoized, so this could be simplified to onFinishRef.current = onFinish if you want to go with this pattern. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!
I think it’s safer to keep the onFinishRef.current assignment inside a useEffect. This ensures the ref is only updated after commit, so it always reflects the latest committed onFinish.
If we assign it directly during render, we could risk writing an incorrect value in certain concurrent rendering scenarios, like when a render starts but later gets discarded. Wrapping it in useEffect avoids that and keeps the ref in sync with the committed tree.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn’t aware of that. Thanks for clarifying.

- Memoize the asyncDebounce instance for `onFinishAutoSave` to avoid re-creating it on each render.
- Add cleanup to call `.cancel()` on unmount.
- Improves autosave reliability by preserving the debounce window across renders and avoiding stray timers.

Signed-off-by: Eric Chen <[email protected]>
@a28689604 a28689604 force-pushed the fix/core-useform-stable-autosave-debounce branch from 38d322d to 7316925 Compare October 22, 2025 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] useForm autoSave debounce leads to unbatched updates

2 participants