Repository navigation
fix: keep useRoom from disconnecting when sortParticipants changes identity - #471
RaphaelFakhri wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 5b008cb 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 |
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| room.disconnect(); | ||
| }; | ||
| }, [room, sortFunc]); | ||
| }, [room]); |
There was a problem hiding this comment.
🟡 Participant order stays stale after sort changes
When sortParticipants changes without a room event, useRoom leaves the existing participant order untouched. sortFuncRef affects only future event callbacks, so the new ordering never appears until another room event.
Learn more
The hook stores the sorted participant list in React state. Changing the sorting callback previously reran the effect, which called onSubscribedTrackChanged() and rebuilt that list. The new effect dependency excludes the callback, so changing it alone does not update the visible order. A room with no subsequent events retains its previous ordering indefinitely.
Example: A call view initially sorts Alice before Bob. The user selects a reverse-order control that replaces sortParticipants, but the view still shows Alice first until someone joins, leaves, or generates another subscribed event.
Recommended fix: Separate participant-list recalculation from room subscription and disconnect cleanup. Recalculate the list when the sorting callback changes without tearing down the room, while preserving the stable event subscription for inline callbacks.
Was this helpful? React with 👍 or 👎 to provide feedback.
Fixes
useRoomdisconnecting the room and re-rendering in a loop whensortParticipantsis an inline function.Problem
useRoomused the sort function as a dependency of its effect, and the effect cleanup callsroom.disconnect(). A function passed inline, such asuseRoom(room, { sortParticipants: (p) => p.reverse() }), has a new identity on every render. Each render therefore ran the cleanup, disconnected the room, and re-ran the effect. The effect also sets a newaudioTracksarray, which triggers another render, so the hook renders in a loop.Change
Store the current sort function in a ref that
useRoomupdates on every render, and depend only onroomin the effect. The hook always uses the latest sort function and disconnects only when the room changes or the component unmounts.Tests
src/__tests__/useRoom.test.tsxcovers three cases:sortParticipantsdoes not disconnect the room.Without the change, the first test fails because the hook re-renders in a loop. With it, all three pass.
tscandeslintare clean.The commit also adds
react-test-rendereras a dev dependency and excludes__tests__fromtsconfig.build.json, so that hook tests can run and stay out of the build.Changeset included.
Fixes #470