-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(core): stabilize autosave debounce and cancel on unmount in useForm #7083
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
base: main
Are you sure you want to change the base?
fix(core): stabilize autosave debounce and cancel on unmount in useForm #7083
Conversation
🦋 Changeset detectedLatest commit: 7316925 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
✅ Deploy Preview for refine-doc-live-previews ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
a10df67 to
38d322d
Compare
| const onFinishRef = React.useRef(onFinish); | ||
| React.useEffect(() => { | ||
| onFinishRef.current = onFinish; | ||
| }, [onFinish]); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
38d322d to
7316925
Compare
onFinishAutoSaveto avoid re-creating it on each render..cancel()on unmount.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.