2017-02-21 04:25:19 +11:00
|
|
|
import merge from 'lodash.merge'
|
|
|
|
import objectPath from 'object-path'
|
2017-02-21 04:38:18 +11:00
|
|
|
import { throttle } from 'lodash'
|
2017-02-21 04:54:09 +11:00
|
|
|
import { inflate, deflate } from 'pako'
|
2017-02-21 04:25:19 +11:00
|
|
|
|
|
|
|
const defaultReducer = (state, paths) => (
|
|
|
|
paths.length === 0 ? state : paths.reduce((substate, path) => {
|
|
|
|
objectPath.set(substate, path, objectPath.get(state, path))
|
|
|
|
return substate
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultStorage = (() => {
|
|
|
|
const hasLocalStorage = typeof window !== 'undefined' && window.localStorage
|
|
|
|
if (hasLocalStorage) {
|
|
|
|
return window.localStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
class InternalStorage {
|
|
|
|
setItem (key, item) {
|
|
|
|
this[key] = item
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
getItem (key) {
|
|
|
|
return this[key]
|
|
|
|
}
|
|
|
|
removeItem (key) {
|
|
|
|
delete this[key]
|
|
|
|
}
|
|
|
|
clear () {
|
|
|
|
Object.keys(this).forEach(key => delete this[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new InternalStorage()
|
|
|
|
})()
|
|
|
|
|
2017-02-21 04:38:18 +11:00
|
|
|
const defaultSetState = (key, state, storage) => {
|
2017-02-21 04:54:09 +11:00
|
|
|
return storage.setItem(key, deflate(JSON.stringify(state), { to: 'string' }))
|
2017-02-21 04:38:18 +11:00
|
|
|
}
|
|
|
|
|
2017-02-21 04:25:19 +11:00
|
|
|
export default function createPersistedState ({
|
|
|
|
key = 'vuex',
|
|
|
|
paths = [],
|
|
|
|
getState = (key, storage) => {
|
2017-02-21 04:54:09 +11:00
|
|
|
let value = storage.getItem(key)
|
|
|
|
try {
|
|
|
|
value = inflate(value, { to: 'string' })
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Couldn't inflate value... Maybe upgrading")
|
|
|
|
}
|
2017-02-21 04:25:19 +11:00
|
|
|
return value && value !== 'undefined' ? JSON.parse(value) : undefined
|
|
|
|
},
|
2017-02-22 20:32:09 +11:00
|
|
|
setState = throttle(defaultSetState, 60000),
|
2017-02-21 04:25:19 +11:00
|
|
|
reducer = defaultReducer,
|
|
|
|
storage = defaultStorage,
|
|
|
|
subscriber = store => handler => store.subscribe(handler)
|
|
|
|
} = {}) {
|
|
|
|
return store => {
|
|
|
|
const savedState = getState(key, storage)
|
|
|
|
if (typeof savedState === 'object') {
|
|
|
|
store.replaceState(
|
|
|
|
merge({}, store.state, savedState)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriber(store)((mutation, state) => {
|
|
|
|
try {
|
|
|
|
setState(key, reducer(state, paths), storage)
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Couldn't persist state:")
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|