Skip to content

Commit

Permalink
configurable debounce, whitelist cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rt2zz committed Oct 21, 2015
1 parent 42ff9cb commit b783029
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/persistStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function persistStore (store, config, cb) {
const deserialize = config.deserialize || defaultDeserialize
const transforms = config.transforms || []
const storage = config.storage || defaultStorage
const debounce = config.debounce || 33

// initialize values
let timeIterator = null
Expand All @@ -24,9 +25,7 @@ module.exports = function persistStore (store, config, cb) {
let storesToProcess = []

forEach(lastState, (s, key) => {
// check blacklist, and whitelist if set
if (whitelist && whitelist.indexOf(key) === -1) { return }
if (blacklist.indexOf(key) !== -1) { return }
if (whitelistBlacklistCheck(key)) { return }
restoreCount += 1
setImmediate(() => {
restoreKey(key, () => {
Expand All @@ -37,11 +36,6 @@ module.exports = function persistStore (store, config, cb) {
})
if (restoreCount === 0) { rehydrationComplete() }

function rehydrationComplete () {
store.dispatch(completeAction())
cb && cb()
}

// store state to disk
store.subscribe(() => {
// clear unfinished timeIterator if exists
Expand All @@ -51,15 +45,13 @@ module.exports = function persistStore (store, config, cb) {

let state = store.getState()
forEach(state, function (subState, key) {
if (whitelist && whitelist.indexOf(key) === -1) { return }
if (blacklist.indexOf(key) !== -1) { return }
// only store keys that have changed
if (whitelistBlacklistCheck(key)) { return }
if (lastState[key] === state[key]) { return }
if (storesToProcess.indexOf(key) !== -1) { return }
storesToProcess.push(key)
})

// time iterator runs every 33ms (30fps)
// time iterator (read: debounce)
timeIterator = setInterval(() => {
if (storesToProcess.length === 0) {
clearInterval(timeIterator)
Expand All @@ -75,11 +67,17 @@ module.exports = function persistStore (store, config, cb) {
storage.setItem(key, serial, warnIfSetError(key))
}
storesToProcess.shift()
}, 33)
}, debounce)

lastState = state
})

function whitelistBlacklistCheck (key) {
if (whitelist && whitelist.indexOf(key) === -1) { return true }
if (blacklist.indexOf(key) !== -1) { return true }
return false
}

function restoreKey (key, cb) {
storage.getItem(createStorageKey(key), function (err, serialized) {
if (err && process.env.NODE_ENV !== 'production') console.warn('Error restoring data for key:', key, err)
Expand All @@ -106,6 +104,11 @@ module.exports = function persistStore (store, config, cb) {
cb()
}

function rehydrationComplete () {
store.dispatch(completeAction())
cb && cb()
}

return {
rehydrate: rehydrate,
purge: (keys) => {
Expand Down

0 comments on commit b783029

Please sign in to comment.