Skip to content

Commit dea415f

Browse files
committed
update readme, config
1 parent b4dfa76 commit dea415f

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ export const CounterStore = signalStore(
4040
```ts
4141
export interface Config<T> {
4242
/**
43-
* Function that gets executed on a storage error (get/set)
44-
* @param error the error that occurred
43+
* These keys will not get saved to storage
4544
*/
46-
error: (error: any) => void
45+
excludeKeys: Array<keyof T>
4746

4847
/**
4948
* Serializer for the state, by default it uses `JSON.stringify()`
@@ -64,8 +63,37 @@ export interface Config<T> {
6463
saveIf: (state: T) => boolean
6564

6665
/**
67-
* These keys will not get saved to storage
66+
* Function that gets executed on a storage error (get/set)
67+
* @param error the error that occurred
6868
*/
69-
excludeKeys: Array<keyof T>
69+
error: (error: any) => void
7070
}
7171
```
72+
73+
## Common Issues
74+
75+
Whenever you get errors this is most likely due to serialization / deserialization of the state.
76+
77+
Objects like `Map` and `Set` are not serializable so you might need to implement your own serialize / deserialize function.
78+
79+
### Example (Set)
80+
81+
Lets say you have a `Set` in your store, then you need a custom serialize / deserialize function to convert from `Set` to `Array` (serialize) and from `Array` to `Set` (deserialize)
82+
83+
```ts
84+
export const MyStore = signalStore(
85+
withState({
86+
unique: new Set([1, 1, 3, 3])
87+
}),
88+
withStorage('myKey', sessionStorage, {
89+
serialize: (state) => JSON.stringify({ ...state, unique: Array.from(state.unique) }),
90+
deserialize: (stateString) => {
91+
const state = JSON.parse(stateString)
92+
return {
93+
...state,
94+
unique: new Set(state.unique)
95+
}
96+
}
97+
})
98+
)
99+
```

projects/ngrx-signals-storage/src/lib/config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
export interface Config<T> {
22
/**
3-
* Function that gets executed on a storage error (get/set)
4-
* @param error the error that occurred
3+
* These keys will not get saved to storage
54
*/
6-
error: (error: any) => void
5+
excludeKeys: Array<keyof T>
76

87
/**
98
* Serializer for the state, by default it uses `JSON.stringify()`
@@ -24,19 +23,20 @@ export interface Config<T> {
2423
saveIf: (state: T) => boolean
2524

2625
/**
27-
* These keys will not get saved to storage
26+
* Function that gets executed on a storage error (get/set)
27+
* @param error the error that occurred
2828
*/
29-
excludeKeys: Array<keyof T>
29+
error: (error: any) => void
3030
}
3131

3232
export const defaultConfig: Config<any> = {
33-
error: (error: any) => console.error(error),
33+
excludeKeys: [],
3434

3535
serialize: (state: any) => JSON.stringify(state),
3636

3737
deserialize: (state: string) => JSON.parse(state),
3838

3939
saveIf: (state: any) => true,
4040

41-
excludeKeys: []
41+
error: (error: any) => console.error(error)
4242
}

src/app/app.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ <h1>Counter</h1>
33

44
<h1>Date</h1>
55
<div>{{ store.date() }}</div>
6+
7+
<h1>Unique numbers</h1>
8+
<div>{{ uniqueNumbers }}</div>

src/app/app.component.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ export const CounterStore = signalStore(
99
withState({
1010
count: 100,
1111
test: 5,
12-
date: new Date()
12+
date: new Date(),
13+
unique: new Set([1, 1, 3, 3])
14+
}),
15+
withStorage('state', sessionStorage, {
16+
excludeKeys: ['test'],
17+
serialize: (state) => JSON.stringify({ ...state, unique: Array.from(state.unique) }),
18+
deserialize: (stateString) => {
19+
const state = JSON.parse(stateString)
20+
return {
21+
...state,
22+
unique: new Set(state.unique)
23+
}
24+
}
1325
}),
14-
withStorage('state', sessionStorage, { excludeKeys: ['test'] }),
1526
withMethods(({ count, ...store }) => ({
1627
setDate(date: Date) {
1728
patchState(store, { date })
@@ -36,4 +47,8 @@ export class AppComponent {
3647
constructor() {
3748
setTimeout(() => this.store.increment(100), 3000)
3849
}
50+
51+
get uniqueNumbers() {
52+
return Array.from(this.store.unique())
53+
}
3954
}

0 commit comments

Comments
 (0)