feat(dom): add useThrottledCallback hook + fix useSyncExternalStore stale closure#36343
Open
srpatcha wants to merge 3 commits into
Open
feat(dom): add useThrottledCallback hook + fix useSyncExternalStore stale closure#36343srpatcha wants to merge 3 commits into
srpatcha wants to merge 3 commits into
Conversation
6ed3fef to
60106f2
Compare
Add throttled callback hook with: - Configurable delay with leading/trailing edge options - Cancel and flush API for pending invocations - Pending state tracking - Proper cleanup on unmount (timer cancellation) - Uses refs to always invoke latest callback without re-creating throttled fn - TypeScript-compatible JSDoc annotations Signed-off-by: Srikanth Patchava <spatchava@meta.com>
Add isSubscribed guard to handleStoreChange in subscribeToStore to prevent the callback from firing after the component has conceptually unmounted but before the passive effect cleanup runs. The cleanup now sets isSubscribed=false before calling unsubscribe, closing the timing window where handleStoreChange could operate on a stale fiber reference. Signed-off-by: Srikanth Patchava <spatchava@meta.com>
Test coverage includes: - Leading edge immediate invocation - Throttling within delay window - Leading-only and trailing-only modes - Cancel API prevents pending trailing invocations - Flush API triggers immediate invocation - Latest callback ref is used without re-creating throttled fn - Timer cleanup on unmount prevents stale invocations Signed-off-by: Srikanth Patchava <spatchava@meta.com>
60106f2 to
171ed8c
Compare
Author
|
Cleaned up + rebased onto current main (commit
Ready for re-review! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two React improvements:
useThrottledCallbackhook (new) —packages/react-dom/src/hooks/useThrottledCallback.jscancel()andflush()API for pending invocationsuseSyncExternalStorestale-closure fix —packages/react-reconciler/src/ReactFiberHooks.jsisSubscribedguard tohandleStoreChangeto prevent the callback from firing after the component has conceptually unmounted but before the passive-effect cleanup runsisSubscribed=falsebefore callingunsubscribe, closing the timing window wherehandleStoreChangecould operate on a stale fiber referenceJest tests —
packages/react-dom/src/__tests__/useThrottledCallback-test.js(331 lines)cancel()prevents pending trailing invocationsflush()triggers immediate invocationScope
Was previously titled "fix: handle null-prototype objects in typeName helper" — that commit's actual code change was lost during an earlier squash, leaving only file-permission changes that conflicted with upstream deletions. Dropped the empty-content commit and re-scoped this PR to the 3 substantive hooks/test changes.
Files
packages/react-dom/src/hooks/useThrottledCallback.jspackages/react-dom/src/__tests__/useThrottledCallback-test.jspackages/react-reconciler/src/ReactFiberHooks.js