Skip to content

Commit 6bd87b9

Browse files
committed
added polyfill for structuredClone
1 parent 0e2440a commit 6bd87b9

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

mod.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/// <reference path="./global.d.ts" />
22

3+
let clone = structuredClone;
4+
5+
if ('clone' in window === false) {
6+
clone = (await import('https://esm.sh/lodash-es@4.17.21')).cloneDeep;
7+
}
8+
39
export interface Observer<T> {
410
update(subject: Store<T>): void;
511
}
@@ -43,9 +49,9 @@ export class Store<T> implements Subject<T> {
4349

4450
public set(options: T | ((prevState: T) => T)): void {
4551
if (typeof options === "function") {
46-
this.state = (options as (prevState: T) => T)(structuredClone(this.state) as T);
52+
this.state = (options as (prevState: T) => T)(clone(this.state) as T);
4753
} else {
48-
this.state = structuredClone(options) as T;
54+
this.state = clone(options) as T;
4955
}
5056

5157
this.notify();

0 commit comments

Comments
 (0)