Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Handle dynamically registered modules #225

Conversation

boris-graeff
Copy link
Contributor

We could handle dynamically registered modules just getting saved state during plugin initialization:

diff --git a/index.js b/index.js
index 257a9ed..16578f4 100644
--- a/index.js
+++ b/index.js
@@ -52,8 +52,9 @@ export default function(options, storage, key) {
     throw new Error('Invalid storage instance given');
   }
 
+  const savedState = shvl.get(options, 'getState', getState)(key, storage);
+
   return function(store) {
-    const savedState = shvl.get(options, 'getState', getState)(key, storage);
 
     if (typeof savedState === 'object' && savedState !== null) {
       store.replaceState(merge(store.state, savedState, {

We just have to initialize the plugin before store creation, then bind plugin after :

import createPersistedState from 'vuex-persistedstate'
const persistatedStore = createPersistedState(someOptions)

const store = new Vuex.Store({ ... })
store.registerModule('someModule', someModule)
persistatedStore(store)

@boris-graeff boris-graeff changed the title Get saved state before store binding function Get saved state before binding to store Oct 30, 2019
@boris-graeff boris-graeff changed the title Get saved state before binding to store Handle dynamically registered modules Oct 30, 2019
@robinvdvleuten
Copy link
Owner

@boris-graeff thanks for the PR! Can you think of any usecases where this breaks current setups?

@boris-graeff
Copy link
Contributor Author

@robinvdvleuten I can't tell ...
To avoid any issue, we can make this behaviour optional

const fetchSavedState = () => shvl.get(options, 'getState', getState)(key, storage);
let savedState

if (options.prefetchSavedState) savedState = fetchSavedState()

return function(store) {
    if (!options.prefetchSavedState) savedState = fetchSavedState()

@robinvdvleuten
Copy link
Owner

I think it won't cause any issues. Thanks for your work!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants