FCE-3288 use current callback references#526
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new internal React hook, useCurrentCallback, and migrates several callback-heavy hooks/managers to use it so that callers can keep a stable function identity while always executing with the latest closure values (reducing stale-closure behavior when callbacks are captured by effects or event listeners).
Changes:
- Introduces
useCurrentCallback(stable function reference + “latest closure” semantics). - Updates
useDataChannel.initialize,useTrackManagercallbacks, anduseScreenShareManager.stopStreamingto useuseCurrentCallbackto avoid stale state in captured callbacks. - Adjusts the
joinedlistener inuseTrackManagerto readdeviceTrackvia a live getter instead of capturing it in the effect closure.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| packages/react-client/src/hooks/useDataChannel.ts | Switches initialization callback to useCurrentCallback to avoid stale connection/ready state. |
| packages/react-client/src/hooks/internal/useTrackManager.ts | Replaces multiple memoized callbacks with useCurrentCallback and updates joined-room listener to read live deviceTrack. |
| packages/react-client/src/hooks/internal/useScreenshareManager.ts | Switches stopStreaming to useCurrentCallback to avoid stale peerStatus/state during stop flows. |
| packages/react-client/src/hooks/internal/useCurrentCallback.ts | Adds the new hook implementing stable identity with updated handler via a ref. |
Comments suppressed due to low confidence (1)
packages/react-client/src/hooks/useDataChannel.ts:60
initializeDataChannelis implemented as anasyncfunction (returns a Promise), but the publicUseDataChannelResulttype currently declaresinitializeDataChannel: () => void. This makes the API surface misleading and prevents consumers from intentionally awaiting initialization. Consider updating the public type/docs to() => Promise<void>(or making the implementation non-async and handling the promise internally).
const initialize = useCurrentCallback(async () => {
if (loading || ready) return;
if (peerStatus !== "connected") {
setError(new Error("Peer is not connected"));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
MiloszFilimowski
approved these changes
May 6, 2026
7ef055c to
0b325b4
Compare
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Description
Introduces
useCurrentCallbackhook that ensures the closure values are always latest.Motivation and Context
Invoking methods one after another in
useEffectwas prone to stale closure issue.Documentation impact
Types of changes
not work as expected)