From 88fa8475352cf9681886c710c0ca95cdda2e53bf Mon Sep 17 00:00:00 2001 From: iam4x Date: Tue, 18 Apr 2023 15:49:41 +0200 Subject: [PATCH] feat(utils): add clone --- src/store/store.base.ts | 3 ++- src/utils/clone.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 src/utils/clone.ts diff --git a/src/store/store.base.ts b/src/store/store.base.ts index 5a4faa3..7d6d8ea 100644 --- a/src/store/store.base.ts +++ b/src/store/store.base.ts @@ -5,6 +5,7 @@ import type { Ticker, WritableStoreData, } from '../types'; +import { clone } from '../utils/clone'; import type { Store } from './store.interface'; @@ -71,7 +72,7 @@ export class DefaultStore implements Store { }; reset = () => { - this.state = JSON.parse(JSON.stringify(defaultStore)); + this.state = clone(defaultStore); this.notify(); }; diff --git a/src/utils/clone.ts b/src/utils/clone.ts new file mode 100644 index 0000000..2634c1c --- /dev/null +++ b/src/utils/clone.ts @@ -0,0 +1,7 @@ +export const clone = >(obj: K): K => { + if (typeof structuredClone !== 'undefined') { + return structuredClone(obj); + } + + return JSON.parse(JSON.stringify(obj)); +};