Skip to content

Commit 1a63ed3

Browse files
author
chungheon_yi
committed
feat: add namespace deep copy
1 parent f2aca0f commit 1a63ed3

File tree

10 files changed

+37
-15
lines changed

10 files changed

+37
-15
lines changed

apps/core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @lodado/namespace-core
22

3+
## 0.1.3
4+
5+
### Patch Changes
6+
7+
- feat: add deepcopy for reset function
8+
39
## 0.1.2
410

511
### Patch Changes

apps/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@lodado/namespace-core",
33
"sideEffects": false,
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"license": "MIT",
66
"exports": {
77
".": {

apps/core/src/models/NamespaceStore.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,23 @@ export default class NamespaceStore<State extends Record<string | symbol, any>>
1212

1313
private publisher: EventPublisher<State>
1414

15+
protected deepClone(value: any) {
16+
// @ts-ignore
17+
return global.structuredClone ? structuredClone?.(value) : JSON.parse(JSON.stringify(value))
18+
}
19+
1520
constructor(initialState: State) {
21+
this.initState = this.deepClone(initialState)
22+
1623
this.publisher = new EventPublisher<State>()
1724
this.state = createStateProxy(initialState, this.publisher)
18-
19-
this.initState = { ...initialState }
2025
}
2126

2227
reset() {
23-
Object.keys(this.initState).forEach((key) => {
24-
this.state[key as keyof State] = this.initState[key]
28+
const deepCopiedState = this.deepClone(this.initState)
29+
30+
Object.keys(deepCopiedState).forEach((key) => {
31+
this.state[key as keyof State] = deepCopiedState[key]
2532
})
2633
}
2734

apps/docs/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @lodado/react-namespace-storybook-docs
22

3+
## 0.0.6
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- @lodado/namespace-core@0.1.3
9+
- @lodado/react-namespace@0.1.5
10+
311
## 0.0.5
412

513
### Patch Changes

apps/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lodado/react-namespace-storybook-docs",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"private": true,
55
"scripts": {
66
"dev": "storybook dev -p 6006",

apps/docs/setupTests.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ const ESTIMATED_SIZE = 1024
1414
window.Element.prototype.getBoundingClientRect = () => {
1515
return { height: ESTIMATED_SIZE, width: ESTIMATED_SIZE }
1616
}
17-

apps/docs/stories/scope/tictactoe/components/models/TicTacToe.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ export default class TicTacToe extends NamespaceStore<{
5454
this.state.turn += 1
5555
}
5656

57-
/**
58-
* Reset the game to its initial state.
59-
*/
60-
reset(): void {
61-
this.reset()
62-
}
63-
6457
/**
6558
* Check if there's a winner or if the game is a draw.
6659
*/

apps/react-namespace/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @lodado/react-namespace
22

3+
## 0.1.5
4+
5+
### Patch Changes
6+
7+
- feat: add deepcopy for reset function
8+
- Updated dependencies
9+
- @lodado/namespace-core@0.1.3
10+
311
## 0.1.4
412

513
### Patch Changes

apps/react-namespace/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lodado/react-namespace",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"sideEffects": false,
55
"license": "MIT",
66
"exports": {

apps/react-namespace/setupTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import '@testing-library/jest-dom'
2+

0 commit comments

Comments
 (0)