Replies: 2 comments 8 replies
-
This is a good idea for a plugin, similar to https://github.com/posva/pinia-plugin-debounce/. Given that disposing of stores is an edge case, this is preferred to be added as a plugin to not impact other users. FYI, setup stores function is the created hook: defineStore('id', () => {
// subscribe to stuff here
}) |
Beta Was this translation helpful? Give feedback.
-
Are stores ever Naively, I would have expected that unsetting or replacing the active Pinia would dispose all stores, but that doesn't appear to be the case — after I monkey-patch |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What problem is this solving
The suggestion is to provide
created
anddisposed
lifecycle hooks in store setup options to access store instance as soon as it's created. Since a store isn't tightly coupled with Vue component lifecycle, there may be no need match it closely.This would allow to keep code that is relevant to a store together. Currently this requires to scatter store logic across the application and move to a place where a store can be accessed - entry point, root component, a plugin. The latter is less suitable for this because it applies to all stores and needs to be additionally controlled through store options.
It's possible to run code on store instance creation in setup function but it's designed to be low-level. This makes setup options lacking in a features and requires to rewrite them to a function.
For example, subscribe to the same store:
Link store
A
toB
changes, withB
being unaware ofA
:Proposed solution
Describe alternatives you've considered
In order to avoid scattering store logic across many files and avoid unnecessary maintenance it's possible custom wrappers to access store instance:
It would be good if the library provided this out of the box without a risk to break a store.
$dispose
already can be overriden by devtools.Beta Was this translation helpful? Give feedback.
All reactions