Replies: 2 comments 5 replies
-
Custom |
Beta Was this translation helpful? Give feedback.
-
💯 really surprised this is the default and only way I can see to do it since the entire state is passed to the getter and setter of storage. It would be great if we can store on an individual level, because:
Imagine a ChatGPT and you have to write every chat state on any chat change. Unfeasibly inefficient.
We may want to store some items in local/session/indexedDB storage, or secure/insecure in the case of mobile. Secure storage on mobile also has a limit of 2048kb per key. Splitting a store up arbitrarily for this requirement isn't ideal. I think it stems from redux style state being immutable - if something changes, everything up the tree changes and the whole store is considered changed. It's a bit of a sledgehammer in this age of fine grained reactivity but more in line with React's philosophy. I guess something like Legend State is more suited to this kind of behavior. |
Beta Was this translation helpful? Give feedback.
-
Hi, this perhaps might not be such a great idea, so I'm open to discussion about how its not feasible, or doesn't make sense.
Question:
Why are we serializing/deserializing and writing the entire state object from a store when we persist as opposed to saving them individually to separate key-value records in the storage?
I understand it is far less complex to just simply serialize the entire state (+ version) and throw that into localStorage. However, I have adopted using IndexedDB with a custom storage (a version where i don't serialize or deserialize, but rather filter out any functions from the state and then save the entire state to IndexedDB.
However, I am using this in a situation where I will have multiple writes of data per second for an increasingly large data set (think procedural generation of a tile map in a game with a game store). Putting aside that I could optimize so that it doesn't write multiple times a second... In my mind, I would really like to have each state property be its own IndexedDB key-value record.
This would enable me to only write updates that pertain to the tile map, instead of the entire state object for that store. Now I understand I probably should have a separation of concerns and move the map stuff to its own store (and now that I think about it, that's not a terrible idea). However, this question still somewhat applies as even then I would have to put just the tilemap in its own store in order to only write to that item when it gets updated.
I haven't done any benchmarking on my custom storage solution, so what I'm doing might be pretty slow and I may just opt for saving the specific state items to the IndexedDB in a set item in an action just for those ones and persist the rest.. 🤷♂️
Also, if I were to use the slices pattern where I add it all to one global store, this seems the like problem would compound (I haven't actually tested that yet) since you are now persisting everything together.
Thoughts? (and sorry for the ramble)
Beta Was this translation helpful? Give feedback.
All reactions