Skip to content
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

Add clone deep #906

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-colts-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Use a deepClone util function for CreateLocalTrackOptions
5 changes: 2 additions & 3 deletions src/room/track/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cloneDeep } from '../../utils/cloneDeep';
import { isSafari, sleep } from '../utils';
import { Track } from './Track';
import type {
Expand All @@ -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 = {};

Expand Down
54 changes: 54 additions & 0 deletions src/utils/cloneDeep.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
Comment on lines +5 to +9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks strange, but apparently this is the only workaround
jsdom/jsdom#3363

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, well, then the unit test is exclusively testing for JSON serialization 😅
Thanks for looking into it!


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);
});
});
11 changes: 11 additions & 0 deletions src/utils/cloneDeep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function cloneDeep<T>(value: T) {
if (typeof value === 'undefined') {
return;
}

if (typeof structuredClone === 'function') {
return structuredClone(value);
} else {
return JSON.parse(JSON.stringify(value)) as T;
}
}
Loading