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

v3.0 preview #135

Open
robinvdvleuten opened this issue Apr 15, 2018 · 5 comments
Open

v3.0 preview #135

robinvdvleuten opened this issue Apr 15, 2018 · 5 comments

Comments

@robinvdvleuten
Copy link
Owner

robinvdvleuten commented Apr 15, 2018

Just a small comment of what I am thinking about for the upcoming v3.0 release of this plugin. As I noticed that many people have many use cases for this plugin, I'll came up with some sort of HoC or middleware pattern to chain different functionality;

function persistedState(next) {
  const state = next();
  // Set the persisted state back to Vuex...
  console.log(state);
}

function withLocalStorage(next) {
  return function() {
    // Get persisted state from localStorage...
    return next({ foo: "bar" });
  };
}

function onlyPaths() {
  return function(state) {
    // Filter out only the paths you'll need...
    return { foo: "baz" };
  };
}

const store = new Vuex.Store({
  // ...
  plugins: [
    // I really like the readability of the chain here; it reads just like a sentence.
    persistedState(withLocalStorage(onlyPaths(/* array of paths to persist */))),
  ]
})

This way, you can have an optimized plugin configuration for your specific use case (filtered paths, modify before persisting, cookies, localStorage, etc.). To be continued!

@BigWillie
Copy link

BigWillie commented Jul 24, 2018

👍 This could also make it easier to handle a cache type scenario. I'm trying to build something where an ajax request is made - if the data isn't posted, its persisted until a connection becomes available. Its proving crazy hard...

@hitautodestruct
Copy link

persistedState(withLocalStorage(onlyPaths(/* array of paths to persist */))),
This pattern might lead to a bit of callback hell?

@robinvdvleuten
Copy link
Owner Author

@hitautodestruct not if your using something to compose it like;

persistedState(
  withLocalStorage(),
  onlyPaths(/* array of paths to persist */)
)

@hitautodestruct
Copy link

How about an array pattern?

persistedState({
  plugins: [ 'withLocalStorage',  onlyPaths(/* array of paths to persist */)]
})

@robinvdvleuten
Copy link
Owner Author

Please see the next branch for any updates on this.

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

No branches or pull requests

3 participants