Skip to content

Commit 4d13aff

Browse files
authored
Ensure that sortPresets does NOT mutate values passed in via options into Room (#1979)
1 parent 814c2f7 commit 4d13aff

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

.changeset/tired-views-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'livekit-client': patch
3+
---
4+
5+
Ensure that sortPresets does NOT mutate values passed in via options into Room

src/room/participant/publishUtils.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ describe('customSimulcastLayers', () => {
174174
expect(sortedPresets[1].encoding.maxFramerate).toBe(15);
175175
expect(sortedPresets[2].encoding.maxFramerate).toBe(20);
176176
});
177+
it('does not mutate the passed-in array', () => {
178+
const presets = [VideoPresets.h1440, VideoPresets.h360, VideoPresets.h1080, VideoPresets.h90];
179+
const original = [...presets];
180+
sortPresets(presets);
181+
expect(presets).toEqual(original);
182+
});
177183
});
178184

179185
describe('screenShareSimulcastDefaults', () => {

src/room/participant/publishUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ function encodingsFromPresets(
419419
/** @internal */
420420
export function sortPresets(presets: Array<VideoPreset> | undefined) {
421421
if (!presets) return;
422-
return presets.sort((a, b) => {
422+
// Sort a copy so we don't mutate the caller's array in place. Mutating the
423+
// passed-in simulcast layers can cause consumers that compare options by value
424+
// (e.g. components-react's useLiveKitRoom) to detect a spurious change.
425+
return presets.slice().sort((a, b) => {
423426
const { encoding: aEnc } = a;
424427
const { encoding: bEnc } = b;
425428

0 commit comments

Comments
 (0)