Throttle saving data, once every 5 seconds is enough.

This commit is contained in:
Roger Braun 2017-02-20 18:38:18 +01:00
parent ebf9fe6a98
commit ce6fb91501

View file

@ -1,5 +1,6 @@
import merge from 'lodash.merge' import merge from 'lodash.merge'
import objectPath from 'object-path' import objectPath from 'object-path'
import { throttle } from 'lodash'
const defaultReducer = (state, paths) => ( const defaultReducer = (state, paths) => (
paths.length === 0 ? state : paths.reduce((substate, path) => { paths.length === 0 ? state : paths.reduce((substate, path) => {
@ -33,6 +34,10 @@ const defaultStorage = (() => {
return new InternalStorage() return new InternalStorage()
})() })()
const defaultSetState = (key, state, storage) => {
return storage.setItem(key, JSON.stringify(state))
}
export default function createPersistedState ({ export default function createPersistedState ({
key = 'vuex', key = 'vuex',
paths = [], paths = [],
@ -40,7 +45,7 @@ export default function createPersistedState ({
const value = storage.getItem(key) const value = storage.getItem(key)
return value && value !== 'undefined' ? JSON.parse(value) : undefined return value && value !== 'undefined' ? JSON.parse(value) : undefined
}, },
setState = (key, state, storage) => storage.setItem(key, JSON.stringify(state)), setState = throttle(defaultSetState, 5000),
reducer = defaultReducer, reducer = defaultReducer,
storage = defaultStorage, storage = defaultStorage,
subscriber = store => handler => store.subscribe(handler) subscriber = store => handler => store.subscribe(handler)