You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't add new keys to persisted stores - feature request for different defaults handing #748
The initial value of store2 is being immediately overwritten by the value from localStorage that is saved by the first persisted store with the name x. You can add new keys all right - you just cannot rely on them being there when initialized from a storage that does contain a version without them.
This seems to make it difficult to develop the component later. If I add new store properties I can't rely on then being initialised. Why not merge the stored object with the initial one, if both are present?
changed the title [-]Can't add new keys to persisted stores[/-][+]Can't add new keys to persisted stores - feature request for different defaults handing[/+]on Feb 8, 2025
Since the initialization is handled by createSignal/Store, there is nothing I can do about it. However, I can add a defaults value to the options that performs the same workaround in a more convenient manner; the rest would be a question of documentation.
Another option would be not to use reconcile inside set, which means you would pick up the normal object merging behaviour of stores. Do I understand correctly that your persisted stores lose this behaviour?
Maybe the simple fix is just to make the reconcile behaviour optional?
OK the change is a bit more convoluted than I thought because the merge needs to happen after deserialisation, so inside set, and set is also used to support the sync option. Not a big deal but not a one-liner.
For now I'm using a forked version that doesn't reconcile. What do you think about making that optional? What am I missing out on without that?
I don't want to work around this in my application code because that would mean unchanged initial values would be persisted which I would rather avoid.
Activity
atk commentedon Feb 8, 2025
The initial value of
store2is being immediately overwritten by the value from localStorage that is saved by the first persisted store with the namex. You can add new keys all right - you just cannot rely on them being there when initialized from a storage that does contain a version without them.tslocke commentedon Feb 8, 2025
This seems to make it difficult to develop the component later. If I add new store properties I can't rely on then being initialised. Why not merge the stored object with the initial one, if both are present?
atk commentedon Feb 8, 2025
That's an interesting and valid argument and I will think about how to handle the issue. In the meantime, there's a workaround:
Additional defaults would need to be added after persistence,. Alternatively, you could also split your state.
[-]Can't add new keys to persisted stores[/-][+]Can't add new keys to persisted stores - feature request for different defaults handing[/+]tslocke commentedon Feb 23, 2025
Did you come to any decision about this @atk ?
atk commentedon Feb 23, 2025
Since the initialization is handled by createSignal/Store, there is nothing I can do about it. However, I can add a defaults value to the options that performs the same workaround in a more convenient manner; the rest would be a question of documentation.
tslocke commentedon Feb 28, 2025
The change I was proposing would be inside
makePersisted. The function hasto load the persisted data, which is then written to the store using your local
setfunction, overwriting any default values.I'm suggesting you instead do something like:
Another option would be not to use
reconcileinsideset, which means you would pick up the normal object merging behaviour of stores. Do I understand correctly that your persisted stores lose this behaviour?Maybe the simple fix is just to make the
reconcilebehaviour optional?tslocke commentedon Feb 28, 2025
Oops posted without testing - that's all wrong! 🙈 Working on it...
tslocke commentedon Feb 28, 2025
OK the change is a bit more convoluted than I thought because the merge needs to happen after deserialisation, so inside
set, andsetis also used to support thesyncoption. Not a big deal but not a one-liner.For now I'm using a forked version that doesn't reconcile. What do you think about making that optional? What am I missing out on without that?
I don't want to work around this in my application code because that would mean unchanged initial values would be persisted which I would rather avoid.
Azq2 commentedon Mar 7, 2025
This package is missing two important features:
makePersisted.ts - enhanced makePersisted implementation
createStoreWithApi.ts - zustand-like persistent store
Example - persistent store
Example - persistent with api