Skip to content

FCE-3288 use current callback references#526

Merged
czerwiukk merged 4 commits into
mainfrom
FCE-3288/use-current-callback-references
May 6, 2026
Merged

FCE-3288 use current callback references#526
czerwiukk merged 4 commits into
mainfrom
FCE-3288/use-current-callback-references

Conversation

@czerwiukk

@czerwiukk czerwiukk commented May 6, 2026

Copy link
Copy Markdown
Member

Description

Introduces useCurrentCallback hook that ensures the closure values are always latest.

Motivation and Context

Invoking methods one after another in useEffect was prone to stale closure issue.

Documentation impact

  • Documentation update required
  • Documentation updated in another PR
  • No documentation update required

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to
    not work as expected)

@czerwiukk czerwiukk self-assigned this May 6, 2026
@linear

linear Bot commented May 6, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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, useTrackManager callbacks, and useScreenShareManager.stopStreaming to use useCurrentCallback to avoid stale state in captured callbacks.
  • Adjusts the joined listener in useTrackManager to read deviceTrack via 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

  • initializeDataChannel is implemented as an async function (returns a Promise), but the public UseDataChannelResult type currently declares initializeDataChannel: () => 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.

Comment thread packages/react-client/src/hooks/internal/useTrackManager.ts Outdated
Comment thread packages/react-client/src/hooks/internal/useCurrentCallback.ts
Comment thread packages/react-client/src/hooks/internal/useCurrentCallback.ts
Comment thread packages/react-client/src/hooks/internal/useScreenshareManager.ts
Comment thread packages/react-client/src/hooks/internal/useCurrentCallback.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread packages/react-client/src/hooks/internal/useCurrentCallback.ts
Comment thread packages/react-client/src/hooks/internal/useTrackManager.ts
@czerwiukk czerwiukk force-pushed the FCE-3288/use-current-callback-references branch from 7ef055c to 0b325b4 Compare May 6, 2026 11:14
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@czerwiukk czerwiukk merged commit bb70155 into main May 6, 2026
2 checks passed
@czerwiukk czerwiukk deleted the FCE-3288/use-current-callback-references branch May 6, 2026 11:35
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.

3 participants