From 4248cdd10f2946a653c11b7287959bbac0e95cc5 Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski Date: Thu, 5 Oct 2023 14:51:46 +0200 Subject: [PATCH] fix: ensure stable sort --- packages/client/src/store/CallState.ts | 5 +++-- packages/client/src/store/__tests__/CallState.test.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/client/src/store/CallState.ts b/packages/client/src/store/CallState.ts index e02f20919e..19d788488d 100644 --- a/packages/client/src/store/CallState.ts +++ b/packages/client/src/store/CallState.ts @@ -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 }), ); diff --git a/packages/client/src/store/__tests__/CallState.test.ts b/packages/client/src/store/__tests__/CallState.test.ts index 48e34c470a..59617990a4 100644 --- a/packages/client/src/store/__tests__/CallState.test.ts +++ b/packages/client/src/store/__tests__/CallState.test.ts @@ -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', () => {