vuejs / vuex

🗃️ Centralized State Management for Vue.js.
https://vuex.vuejs.org
MIT License
28.42k stars 9.58k forks source link

Is it possible to make the plugin interface async? #636

Closed beckler closed 7 years ago

beckler commented 7 years ago

I have a custom plugin to auto save and load the state, but I prefer to use localForage over localStorage. Since localForage only relies on async methods, the views have usually fully mounted before the plugin has finished loading any data to reset the store. This mostly causes problems when I'm trying to do some operations when the page initially loads. Like previously stored auth info.

I would imagine this would be pretty similar to how vue-router handles hooks, something like the following:

const myPlugin = (store, done) => {
  // called when the store is initialized
  store.subscribe((mutation, state) => {
    // called after every mutation.
    // The mutation comes in the format of { type, payload }.
  })
  return done()
}

I'm not sure of the difficulty of it, or if it goes against a mantra. I figured I would just ask. ¯_(ツ)_/¯

ktsn commented 7 years ago

This should be handled by each plugin. You can provide the interface of such callback by defining plugin factory.

function pluginFactory (done) {
  return function plugin (store) {
    // ...
  }
}

new Vuex.Store({
  plugins: [pluginFactory(() => { /* ... */ })]
})