Skip to content

Commit

Permalink
fix: ensure stable sort (#1130)
Browse files Browse the repository at this point in the history
Ensure Stable Sorting by mutating the source participant array.
  • Loading branch information
oliverlaz authored Oct 5, 2023
1 parent 465b87e commit f96e1af
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/client/src/store/CallState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ export class CallState {
constructor() {
this.logger = getLogger(['CallState']);
this.participants$ = this.participantsSubject.asObservable().pipe(
// TODO: replace with Array.toSorted once available
map((ps) => [...ps].sort(this.sortParticipantsBy)),
// maintain stable-sort by mutating the participants stored
// in the original subject
map((ps) => ps.sort(this.sortParticipantsBy)),
shareReplay({ bufferSize: 1, refCount: true }),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/store/__tests__/CallState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('CallState', () => {

const ps2 = state.participants;
// should resolve in initial - non-mutated state as set at the beginning
expect(ps2.map((p) => p.name)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
expect(ps2.map((p) => p.name)).toEqual(['F', 'B', 'E', 'A', 'C', 'D']);
});

it('should support custom sorting', () => {
Expand Down

0 comments on commit f96e1af

Please sign in to comment.