Derived Store - To use or not to use
From #dev-lounge - I'm thinking of rewriting my monolithic class-based stores in Item Piles to something more maintainable - when I create a derived store based on a value in another large store, is that faster/better than just creating multiple stores for the properties of the large store?
1 Reply
So, on a cursory review of src/stores I do think a refactor will help quite a bit. The benefit of changing to
propertyStore
via storing data in a private object with properties of that object turned into stores will allow direct access to this data internally without the many uses of get()
from svelte/store
.
Other aspects since you are using ESM is just getting you up to speed on JSDoc and typing your code and adding comments for future you! You can get all the same coverage and benefits w/ JSDoc with a little knowhow. Keep in mind too that you can use TS files to define types when JSDoc is more cumbersome and still import / use them in JSDoc / ESM code.
Now that you have spent several years doing a little bit of prototyping and experimenting, but are now more narrowly focusing on a handful of modules like Item Piles, Sequencer, etc. improving the code for maintenance is a great idea.
-----
One of the most complex / compound stores in TRL is TJSPosition.
The following is general pseudocode for the general pattern:
Heh... I sort of ran out of characters in the post for the above store example. You can add accessors too and they are one way reactive.
If you make accessors for all of the internal store properties like that and add a set
and subscribe
methods for the store contract in MyCompoundStore
you can also make it a store that can be subscribed to for any property change. This is what TJSPosition
does, but it's just a tad more complex to write out in pseudocode.
Theoretically this approach IE having accessors exposed for properties within will somewhat be compatible with a Svelte 5 transition to $state
. Instead of accessing / destructuring the .stores
accessor you'll be able to transition over to using the individual property accessors for two way reactivity.