Repository navigation
fix: keep useRoom from disconnecting when sortParticipants changes identity #471
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
Closed
+149
−6
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@livekit/react-native': patch | ||
| --- | ||
|
|
||
| Fix `useRoom` disconnecting the room on every render when `sortParticipants` is an inline function |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import type { Participant, Room } from 'livekit-client'; | ||
| import { act, create } from 'react-test-renderer'; | ||
|
|
||
| import { useRoom } from '../useRoom'; | ||
|
|
||
| function fakeRoom() { | ||
| const room: any = { | ||
| remoteParticipants: new Map(), | ||
| localParticipant: {}, | ||
| disconnect: jest.fn(), | ||
| once: jest.fn(), | ||
| on: jest.fn(), | ||
| off: jest.fn(), | ||
| }; | ||
| room.on.mockReturnValue(room); | ||
| room.off.mockReturnValue(room); | ||
| return room as Room & { disconnect: jest.Mock }; | ||
| } | ||
|
|
||
| describe('useRoom', () => { | ||
| it('does not disconnect the room when it re-renders with a new sortParticipants', () => { | ||
| const room = fakeRoom(); | ||
| let renders = 0; | ||
| const Probe = () => { | ||
| // Without this guard, the unfixed hook re-renders forever. | ||
| if (++renders > 20) { | ||
| throw new Error('useRoom re-rendered in a loop'); | ||
| } | ||
| // An inline function has a new identity on every render. | ||
| useRoom(room, { sortParticipants: (_: Participant[]) => {} }); | ||
| return null; | ||
| }; | ||
|
|
||
| let renderer: ReturnType<typeof create>; | ||
| act(() => { | ||
| renderer = create(<Probe />); | ||
| }); | ||
| act(() => { | ||
| renderer.update(<Probe />); | ||
| }); | ||
| act(() => { | ||
| renderer.update(<Probe />); | ||
| }); | ||
|
|
||
| expect(room.disconnect).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('disconnects the room when the component unmounts', () => { | ||
| const room = fakeRoom(); | ||
| const Probe = () => { | ||
| useRoom(room); | ||
| return null; | ||
| }; | ||
|
|
||
| let renderer: ReturnType<typeof create>; | ||
| act(() => { | ||
| renderer = create(<Probe />); | ||
| }); | ||
| act(() => { | ||
| renderer.unmount(); | ||
| }); | ||
|
|
||
| expect(room.disconnect).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('uses the latest sortParticipants after a re-render', () => { | ||
| const room = fakeRoom(); | ||
| const first = jest.fn(); | ||
| const second = jest.fn(); | ||
| const Probe = ({ sort }: { sort: (p: Participant[]) => void }) => { | ||
| useRoom(room, { sortParticipants: sort }); | ||
| return null; | ||
| }; | ||
|
|
||
| let renderer: ReturnType<typeof create>; | ||
| act(() => { | ||
| renderer = create(<Probe sort={first} />); | ||
| }); | ||
| act(() => { | ||
| renderer.update(<Probe sort={second} />); | ||
| }); | ||
|
|
||
| const onParticipantsChanged = (room.on as jest.Mock).mock.calls.find( | ||
| ([event]) => event === 'reconnected' | ||
| )![1]; | ||
| act(() => { | ||
| onParticipantsChanged(); | ||
| }); | ||
|
|
||
| expect(second).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
|
|
||
| { | ||
| "extends": "./tsconfig", | ||
| "exclude": ["example", "ci"] | ||
| "exclude": ["example", "ci", "**/__tests__"] | ||
| } |
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
Oops, something went wrong.
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.
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.
🟡 Participant order stays stale after sort changes
When
sortParticipantschanges without a room event,useRoomleaves the existing participant order untouched.sortFuncRefaffects 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.