Add ability to destroy stores
See original GitHub issueWhat problem is this solving
Sometimes we need to define stores dynamically - e.g. to be able to use 2 instances of the same store that don’t share the same state.
In such a case, in Vuex, I create a factory function that takes dependencies and return an initialized instance of a Vuex store. Then, when I no longer need that store I can call unregisterModule()
and destroy that store.
In Pinia I didn’t find such an option.
Proposed solution
<script>
import createMyStore from '@/store/createMyStore';
import { onUnmounted } from '@vue/composition-api';
export default {
name: 'App',
setup() {
const useStore = createMyStore({
id: 'myId',
useSettings: () => ({
value: 1,
})
});
const { combinedValue, $destroy } = useStore();
onUnmounted(() => {
$destroy();
})
return {
combinedValue,
}
},
}
</script>
import { defineStore } from 'pinia'
export default ({ useSettings, id }) => {
const mainId = 'myStore'
return defineStore({
id: `${mainId}_${id}`,
state: () => ({
value: 1,
}),
getters: {
combinedValue() {
const settings = useSettings();
return this.value + settings.value;
},
},
})
}
Describe alternatives you’ve considered
I don’t think there’s any alternative. We can call $reset()
to restore the state - this will free-up the memory, but it won’t destroy the instance entirely.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Add ability to destroy stores · Issue #557 · vuejs/pinia - GitHub
Sometimes we need to define stores dynamically - e.g. to be able to use 2 instances of the same store that don't share...
Read more >c# - How i can destroy an GameObject but also store the ...
What you can do is hold a reference to that object and then store it somewhere and make it invisible. Its called object...
Read more >Innovation Killers: How Financial Tools Destroy Your Capacity ...
When a company is looking at adding capacity that is identical to existing capacity, it makes sense to compare the mar-ginal cost of...
Read more >Kanai's Cube - Game Guide - Diablo III - Blizzard Entertainment
With Kanai's Cube, you can destroy a Legendary item and store its power. Most Legendary powers can be extracted—these are highlighted in orange...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Example
Let’s assume that:
user
store has a dependency on the settings storeInitial approach (no dynamic stores).
_Source: https://github.com/wujekbogdan/pinia-dynamic-stores/tree/feature/no_dynamic_stores_
In the given example the
user
store depends on a settings store that may have 2 implementations:localSettings
andremoteSettings
. They both share the same interface but fetch the data differently.This example is oversimplified. In reality there might be much more going on in each implementation. That’s why they are separate stores.
In this example it’s the
user
’s store responsibility to initialise a proper settings store based on theinit
action parameter, but things may get more complex if there are more dependencies.Dependency injection-based approach (dynamic stores)
_Source: https://github.com/wujekbogdan/pinia-dynamic-stores/tree/feature/dynamic_stores_
Let’s imagine that we need several components on the page that use the
user
store but each component requires its own state. The solution is to initialize multiple instances of theuser
store and inject the settings store as a dependency using a factory pattern.This pattern not only allows for having multiple instances of the same store, but also simplifies the logic. The
user
store no longer needs to worry about resolving dependencies. In case of just one dependency it doesn’t look like a big advantage, but in case of multiple dependencies the code can get really messy.The factory pattern allows for moving that logic away from a store and let the wrapping module handle dependency resolution/injection. IRL it doesn’t need to be a Vue component - it could be a dedicated module that performs the initialization.
Another use case for dynamically created stores are non-SPA applications. I’ve been working on a project with a “classic” PHP backend. The majority of of the views were rendered on the backend, but all dynamic components were rendered with Vue. Each dynamic widget was a separate Vue application. Each widget was responsible for registering Vuex stores on its own and destroying these stores when they were no longer needed. On one page there could be multiple widgets that use the same store, but don’t share the state - they were using separate instances of the same store.
Usually, you create one store and add 100 items to it instead.
Even in Vuex, the store was still there. We could still expose a tree shakeable function that clears up the traces of the store though