Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for vuex-persist #102

Merged
merged 14 commits into from Feb 2, 2020

Conversation

Kapulara
Copy link
Contributor

@Kapulara Kapulara commented Mar 8, 2019

Hello,

So when researching this I found this commit:
vuejs/vuex#837

It essentially allows you to preserve the state when registering a module dynamically.
Right now it just uses the default values and overrides the state. (which makes you unable to use vuex-persist for example. Might be others as well)

So now you can add preserveState to @Module as a option.

Also took the test from #47 hope you don't mind 😉 @JohnCampionJr

I believe this PR will fix/close:
#44
#46
#47
... maybe others

Thanks,
Bart

@codecov
Copy link

codecov bot commented Mar 8, 2019

Codecov Report

Merging #102 into master will decrease coverage by 11.64%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #102       +/-   ##
===========================================
- Coverage   96.32%   84.67%   -11.65%     
===========================================
  Files           9        9               
  Lines         136      124       -12     
  Branches       15       13        -2     
===========================================
- Hits          131      105       -26     
- Misses          4       16       +12     
- Partials        1        3        +2
Impacted Files Coverage Δ
src/module/index.ts 90.32% <100%> (-9.68%) ⬇️
src/module/staticGenerators.ts 57.89% <0%> (-42.11%) ⬇️
src/vuexmodule.ts 72.72% <0%> (-21.4%) ⬇️
src/helpers.ts 66.66% <0%> (-13.34%) ⬇️
src/module/stateFactory.ts 87.5% <0%> (-12.5%) ⬇️
src/action.ts 100% <0%> (ø) ⬆️
rollup.config.js 100% <0%> (ø) ⬆️
src/mutationaction.ts 85% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc2e2de...e84ac7b. Read the comment docs.

@kisonay
Copy link

kisonay commented Mar 12, 2019

it would be great if we could get this support

@albernhagen
Copy link

Can this be merged in?

...

-- @Module({ dynamic: true, store: store, name: 'mm' })
++ @Module({ dynamic: true, store: store, name: 'mm', preserveState: true })

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a diff of a diff... maybe a bad merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah this was intentional, this is so that the user can see which line to change. Could be removed. I thought it made it more clear.
Screenshot 2019-04-26 at 11 48 47

package.json Outdated
@@ -62,7 +63,8 @@
"typescript": "^3.2.2",
"vue": "^2.6.6",
"vuepress": "^0.14.8",
"vuex": "^3.1.0"
"vuex": "^3.1.0",
"vuex-persist": "^2.0.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is great, but I think tying this project to vuex-persist is an unnecessary coupling.

The change can very well live without any reference to vuex-persist and merely serve as an additional ability to set the preserveState option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, it's development only. So will only be installed once you want to develop it. Since I wrote integration tests between the two libraries. How would I go ahead and test it without the library?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kapulara I'm not sure, but you could install it as a peer dependency perhaps?

@sneko
Copy link

sneko commented Apr 18, 2019

I'm also interesting in using the power of both tools 😃

Can't wait to see that merged and released 👍

@ansien
Copy link

ansien commented Apr 24, 2019

Can this be merged in please?

@ansien
Copy link

ansien commented Apr 25, 2019

@championswimmer

@championswimmer
Copy link
Owner

championswimmer commented Apr 26, 2019 via email

@sneko
Copy link

sneko commented Apr 26, 2019

@championswimmer what would be the more appropriate way? Having kind of interfaces to plugin any persisting module behind the scene?

By the way, do you currently have a project where you combine both vuex-module-decorators and vuex-persist? Just to see if possible how you deal with this combo 😄

Thanks again for your work!

@ansien
Copy link

ansien commented Apr 26, 2019

@championswimmer If that's the case, then please make it possible to use dynamic modules in combination with vuex-persist. This does currently not work.

@Kapulara
Copy link
Contributor Author

Kapulara commented Apr 28, 2019

I am still apprehensive of shoving vuex-persist down the throats of everyone who uses vuex-module-decorators While I am proud of my own work on vuex-persist, many people use the alternate library vuex-persistedstate too. I would not want to drag them just because one of my libraries forces using another.

On Fri 26 Apr, 2019, 3:20 PM Bart van Vliet, @.***> wrote: @Kapulara commented on this pull request. ------------------------------ In package.json <#102 (comment)> : > @@ -62,7 +63,8 @@ "typescript": "^3.2.2", "vue": "^2.6.6", "vuepress": "^0.14.8", - "vuex": "^3.1.0" + "vuex": "^3.1.0", + "vuex-persist": "^2.0.0" First of all, it's development only. So will only be installed once you want to develop it. Since I wrote integration tests between the two libraries. How would I go ahead and test it without the library? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#102 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKD7SV3PTQGTSWI7MGOHPTPSLF5BANCNFSM4G4ZDJGA .

@championswimmer

You are right about this, however all of the wording I did in the documentation I referenced it with "e.g" so not really feeling like shoving it down everyone. Just giving an example in how this would be used.

Also I added it as development dependency so It's only required for development and wont be installed with the production flag. The only reason I installed it was because I wanted to make sure it works. If you feel like it should be removed and have this as a "preserveState" functionality just let me know. I'll change the pull request to match that instead.

However I wouldn't know how to test "preserveState".

Thanks for all of the hard work you put into this library.
Bart

@sneko
Copy link

sneko commented Jun 14, 2019

I'm still wondering what would you recommend @championswimmer?

From what I understand this PR uses vuex-persistonly for test purposes. If this is gone "behavior", could this PR be merged?

Thank you,

@proteus-aholt
Copy link

Would love to see this PR merged, or at the very least see @Module support preserveState

@Kapulara
Copy link
Contributor Author

Would love to see this PR merged, or at the very least see @Module support preserveState

What I could do is remove the tests (@championswimmer if that's ok)

So we can merge this finally.

@championswimmer
Copy link
Owner

championswimmer commented Jul 11, 2019 via email

@Kapulara
Copy link
Contributor Author

@championswimmer added it as peer dependency but now build seems to be failing since tests cannot be run. How would you like to approach?

@TheYves
Copy link

TheYves commented Jul 12, 2019

Sorry, but I don't get it. Why not adding vuex-persist as devDependency? This way, vuex-persist is only required for development of vuex-module-decoratos itself and not for the actual users who use the library. Users of vuex-module-decorators don't have to use vuex-persist nor do they have to install it in any way, because it's not in the dependency list.

You did an awesome job with both libraries @championswimmer , kudos for that. Let's now bring them together with this fix. :-)

@Kapulara
Copy link
Contributor Author

Hi guys.

I've started working on a project where I required these changes. So I published the changes to a new public package which everyone else can use.

@championswimmer I will deprecate the package if you approve this merge request.

Package: @bartvanvliet/vuex-module-decorators

@anorborg
Copy link

anorborg commented Aug 5, 2019

@Kapulara i tried your package, but doesn't seem to be working for me FYI

const vuexLocal = new VuexPersistence<ClientState>({
  strictMode: process.env.NODE_ENV === 'development',
  storage: window.sessionStorage
})

const storeOptions: StoreOptions<ClientState> = {
  plugins: [vuexLocal.plugin],
  strict: process.env.NODE_ENV === 'development'
}

@Module({
  dynamic: true,
  namespaced: true,
  name: Namespaces.ThingModule,
  stateFactory: true,
  store: store,
  preserveState: true
})
export class ThingModule extends VuexModule {
  myThings: string[] = []
// ...
}

myThings is undefined at runtime. Not sure if its because I'm using statefactory, butt just FYI. returning to the vuex-module-decorators works (obviously not persisting though).

@avxkim
Copy link

avxkim commented Aug 20, 2019

@championswimmer what's the current progress?

@avxkim
Copy link

avxkim commented Aug 24, 2019

I've used this workaround: https://stackoverflow.com/questions/55843052/vuex-not-loading-module-decorated-with-vuex-module-decorators

Seems to work.

@mrijken
Copy link

mrijken commented Aug 26, 2019

@webcoderkz that's not a workaround. That answer is using a static module; not a dynamic module.

@avxkim
Copy link

avxkim commented Aug 27, 2019

@mrijken won't it work with dynamic modules in the future?

@mrijken
Copy link

mrijken commented Aug 27, 2019

@webcoderkz only when this PR is merged.

@avxkim
Copy link

avxkim commented Aug 27, 2019

Why @championswimmer won't merge this PR?

Signed-off-by: Arnav Gupta <arnav@codingblocks.com>
@rystaf
Copy link

rystaf commented Nov 22, 2019

I was able to preserve the state of dynamic modules with vuex-persistedstate robinvdvleuten/vuex-persistedstate#225

@Tronix117
Copy link

Hey @championswimmer, when can we excpect the merge to occur ?
It's a simple additional option (one more line in the code) and the PR has been opened since march...

@oakromulo
Copy link

@championswimmer any chance for this to be merged this year?

@dnsritch
Copy link

dnsritch commented Jan 9, 2020

mabe I have a solution for this,I write a vuex plugin by hijacking the store.registerModule in vuex
here is the code
maybe someone can make a optimization for this
I think it could be a better solution

//#region [rgba(0,205,20,0.1)]
const dynamicRegisterPlugin = (store: Store<IRootState>) => {

  // Override dynamic module handling in vuex
  let origRegisterModule: any
  if (store.registerModule) {
    origRegisterModule = store.registerModule.bind(store)
    // your module interfaces
    type T = IAppState | IUserState | IPermissionState | ITagsViewState | ISettingsState | IHome
    //@ts-ignore
    store.registerModule = (
      path: string,
      module: Module<T, IRootState>,
      options?: ModuleOptions
    ) => {
      // console.log(store)
      if ((store.state as Object).hasOwnProperty(path)) {
        origRegisterModule(path, module, { preserveState: true })
      } else {
        origRegisterModule(path, module, options)
      }
      // origRegisterModule(path, module, options)
    }
  }
}
//#endregion


const vuexLocal = new VuexPersistence<IRootState>({
  storage: window.sessionStorage
})

// Declare empty store first, dynamically register all modules later.
export default new Vuex.Store<IRootState>({
  plugins: [dynamicRegisterPlugin, vuexLocal.plugin]
})

@championswimmer
Copy link
Owner

need the travis ci to pass, before merging :(

@aleksanderzubkov
Copy link

Can somebody merge it please?

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

Successfully merging this pull request may close these issues.

None yet