Skip to content

Commit

Permalink
feat(utils): add clone
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Apr 18, 2023
1 parent b7f318a commit 88fa847
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/store/store.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Ticker,
WritableStoreData,
} from '../types';
import { clone } from '../utils/clone';

import type { Store } from './store.interface';

Expand Down Expand Up @@ -71,7 +72,7 @@ export class DefaultStore implements Store {
};

reset = () => {
this.state = JSON.parse(JSON.stringify(defaultStore));
this.state = clone(defaultStore);
this.notify();
};

Expand Down
7 changes: 7 additions & 0 deletions src/utils/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const clone = <K extends Record<string, any>>(obj: K): K => {
if (typeof structuredClone !== 'undefined') {
return structuredClone(obj);
}

return JSON.parse(JSON.stringify(obj));
};

0 comments on commit 88fa847

Please sign in to comment.