vuejs / vuex

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

Add a subscribeModule instance method #1503

Open ianwalter opened 5 years ago

ianwalter commented 5 years ago

What problem does this feature solve?

Vuex doesn't provide a way for plugins to be notified when modules are added to the store dynamically through registerModule. This means that there can be changes in the state tree that are impossible for plugins to know about through the existing subscription methods like subscribe and subscribeAction. A common use case would be a plugin that saves the Vuex state to local storage. When a module is added through registerModule, the local storage plugin will not be able to store it's initial state to local storage.

What does the proposed API look like?

subscribeModule(handler: Function)

Subscribe to registerModule/unregisterModule events. The handler is called with a module object and a namespace when the module is registered. The module object is null when unregistered:

store.subscribeModule((mod, namespace) => {
  if (mod) {
    console.log(mod)
  } else {
    console.log(namespace)
  }
})
kiaking commented 4 years ago

Relates to #1193.