vuex-persistedstate icon indicating copy to clipboard operation
vuex-persistedstate copied to clipboard

v3.0 preview

Open robinvdvleuten opened this issue 6 years ago • 5 comments

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!

robinvdvleuten avatar Apr 15 '18 12:04 robinvdvleuten

👍 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...

BigWillie avatar Jul 24 '18 08:07 BigWillie

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

hitautodestruct avatar Aug 08 '18 07:08 hitautodestruct

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

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

robinvdvleuten avatar Aug 08 '18 07:08 robinvdvleuten

How about an array pattern?

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

hitautodestruct avatar Aug 13 '18 05:08 hitautodestruct

Please see the next branch for any updates on this.

robinvdvleuten avatar Sep 10 '18 09:09 robinvdvleuten