Skip to content

Commit

Permalink
Fix syncStateWithLocalStorage (#10182)
Browse files Browse the repository at this point in the history
  • Loading branch information
speigg committed May 17, 2024
1 parent 91efd00 commit b52f880
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions packages/hyperflux/functions/StateFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,12 @@ export function syncStateWithLocalStorage<S, E extends Identifiable>(
if (storedValue !== null && storedValue !== 'undefined') state[key].set(JSON.parse(storedValue))
}
},
onSet: (state, desc, rootState) => {
for (const key of keys) {
if (rootState[key].value === undefined)
localStorage.removeItem(`${stateNamespaceKey}.${rootState.identifier}.${key}`)
else
localStorage.setItem(
`${stateNamespaceKey}.${rootState.identifier}.${key}`,
JSON.stringify(rootState[key].get({ noproxy: true }))
)
onSet: (state, desc) => {
const key = '' + desc.path[0]
if (keys.includes(key)) {
const storageKey = `${stateNamespaceKey}.${state.identifier}.${key}`
if (!state[key] || !state[key].get(NO_PROXY)) localStorage.removeItem(storageKey)
else localStorage.setItem(storageKey, JSON.stringify(state[key].get(NO_PROXY)))
}
}
}
Expand Down

0 comments on commit b52f880

Please sign in to comment.