scriptPilot / app-framework

Applications for any device with HTML, CSS and JavaScript - free and open source!
MIT License
653 stars 143 forks source link

Use Vuex with persistent state in v2 #757

Open scriptPilot opened 6 years ago

scriptPilot commented 6 years ago

State management: https://vuex.vuejs.org/en/ Keep state: https://github.com/robinvdvleuten/vuex-persistedstate Native storage: https://github.com/TheCocoaProject/cordova-plugin-nativestorage

caiotarifa commented 6 years ago

Something like this?

export default function (key = 'vuex') {
  function getState (key) {
    return new Promise((resolve, reject) => {
      NativeStorage.getItem(key, resolve, reject)
    })
  }

  function setState (key, state) {
    return new Promise((resolve, reject) => {
      NativeStorage.setItem(key, state, resolve, reject)
    })
  }

  return function (store) {
    getState(key).then(state => {
      if (typeof state === 'object') {
        const merged = Object.assign({}, store.state, savedState)
        store.replaceState(merged)
      }
    })

    store.subscribe((mutation, state) => setState(key, state))
  }
}