From bbefb77a4c8d2062fc095bddcb9b520e40669040 Mon Sep 17 00:00:00 2001 From: vas11yev1work Date: Thu, 26 Oct 2023 01:16:49 +0600 Subject: [PATCH 1/2] Add clone deep --- src/room/track/utils.ts | 5 ++-- src/utils/cloneDeep.test.ts | 54 +++++++++++++++++++++++++++++++++++++ src/utils/cloneDeep.ts | 11 ++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/utils/cloneDeep.test.ts create mode 100644 src/utils/cloneDeep.ts diff --git a/src/room/track/utils.ts b/src/room/track/utils.ts index d6faacabcd..bb0349b813 100644 --- a/src/room/track/utils.ts +++ b/src/room/track/utils.ts @@ -1,3 +1,4 @@ +import { cloneDeep } from '../../utils/cloneDeep'; import { isSafari, sleep } from '../utils'; import { Track } from './Track'; import type { @@ -13,9 +14,7 @@ export function mergeDefaultOptions( audioDefaults?: AudioCaptureOptions, videoDefaults?: VideoCaptureOptions, ): CreateLocalTracksOptions { - const opts: CreateLocalTracksOptions = { - ...options, - }; + const opts: CreateLocalTracksOptions = cloneDeep(options) ?? {}; if (opts.audio === true) opts.audio = {}; if (opts.video === true) opts.video = {}; diff --git a/src/utils/cloneDeep.test.ts b/src/utils/cloneDeep.test.ts new file mode 100644 index 0000000000..a0bf2bde63 --- /dev/null +++ b/src/utils/cloneDeep.test.ts @@ -0,0 +1,54 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { cloneDeep } from './cloneDeep'; + +describe('cloneDeep', () => { + beforeEach(() => { + global.structuredClone = vi.fn((val) => { + return JSON.parse(JSON.stringify(val)); + }); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('should clone a simple object', () => { + const structuredCloneSpy = vi.spyOn(global, 'structuredClone'); + + const original = { name: 'John', age: 30 }; + const cloned = cloneDeep(original); + + expect(cloned).toEqual(original); + expect(cloned).not.toBe(original); + expect(structuredCloneSpy).toHaveBeenCalledTimes(1); + }); + + it('should clone an object with nested properties', () => { + const structuredCloneSpy = vi.spyOn(global, 'structuredClone'); + + const original = { name: 'John', age: 30, children: [{ name: 'Mark', age: 7 }] }; + const cloned = cloneDeep(original); + + expect(cloned).toEqual(original); + expect(cloned).not.toBe(original); + expect(cloned?.children).not.toBe(original.children); + expect(structuredCloneSpy).toHaveBeenCalledTimes(1); + }); + + it('should use JSON namespace as a fallback', () => { + const structuredCloneSpy = vi.spyOn(global, 'structuredClone'); + const serializeSpy = vi.spyOn(JSON, 'stringify'); + const deserializeSpy = vi.spyOn(JSON, 'parse'); + + global.structuredClone = undefined as any; + + const original = { name: 'John', age: 30 }; + const cloned = cloneDeep(original); + + expect(cloned).toEqual(original); + expect(cloned).not.toBe(original); + expect(structuredCloneSpy).not.toHaveBeenCalled(); + expect(serializeSpy).toHaveBeenCalledTimes(1); + expect(deserializeSpy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/utils/cloneDeep.ts b/src/utils/cloneDeep.ts new file mode 100644 index 0000000000..1d6f5d8a34 --- /dev/null +++ b/src/utils/cloneDeep.ts @@ -0,0 +1,11 @@ +export function cloneDeep(value: T) { + if (typeof value === 'undefined') { + return; + } + + if (typeof structuredClone === 'function') { + return structuredClone(value); + } else { + return JSON.parse(JSON.stringify(value)) as T; + } +} From 1187ead91e930498a61ac33a9a77d51e631344e9 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Thu, 26 Oct 2023 19:42:12 +0200 Subject: [PATCH 2/2] Create cuddly-colts-perform.md --- .changeset/cuddly-colts-perform.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cuddly-colts-perform.md diff --git a/.changeset/cuddly-colts-perform.md b/.changeset/cuddly-colts-perform.md new file mode 100644 index 0000000000..6e3889e199 --- /dev/null +++ b/.changeset/cuddly-colts-perform.md @@ -0,0 +1,5 @@ +--- +"livekit-client": patch +--- + +Use a deepClone util function for CreateLocalTrackOptions