Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js
index 67ecc1ea..1e953361 100644
--- a/src/lib/persisted_state.js
+++ b/src/lib/persisted_state.js
@@ -1,65 +1,70 @@
import merge from 'lodash.merge'
import objectPath from 'object-path'
+import { throttle } from 'lodash'
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()
})()
+const defaultSetState = (key, state, storage) => {
+ return storage.setItem(key, JSON.stringify(state))
+}
+
export default function createPersistedState ({
key = 'vuex',
paths = [],
getState = (key, storage) => {
const value = storage.getItem(key)
return value && value !== 'undefined' ? JSON.parse(value) : undefined
},
- setState = (key, state, storage) => storage.setItem(key, JSON.stringify(state)),
+ setState = throttle(defaultSetState, 5000),
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)
}
})
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 5:48 AM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39641
Default Alt Text
(2 KB)

Event Timeline