Skip to content
Snippets Groups Projects
Commit ce6fb915 authored by lain's avatar lain
Browse files

Throttle saving data, once every 5 seconds is enough.

parent ebf9fe6a
No related branches found
No related tags found
No related merge requests found
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) => {
......@@ -33,6 +34,10 @@ const defaultStorage = (() => {
return new InternalStorage()
})()
const defaultSetState = (key, state, storage) => {
return storage.setItem(key, JSON.stringify(state))
}
export default function createPersistedState ({
key = 'vuex',
paths = [],
......@@ -40,7 +45,7 @@ export default function createPersistedState ({
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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment