Skip to content

Commit

Permalink
remove preloaders
Browse files Browse the repository at this point in the history
  • Loading branch information
rt2zz committed May 1, 2016
1 parent 6b1bd79 commit 001b8e5
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/getStoredState.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,28 @@ export default function getStoredState (config, onComplete) {
const deserialize = config.deserialize || defaultDeserialize
const transforms = config.transforms || []
const purgeMode = config.purgeMode || false
const preloaders = config.preloaders || []

// fallback getAllKeys to `keys` if present (LocalForage compatability)
if (storage.keys && !storage.getAllKeys) storage = {...storage, getAllKeys: storage.keys}

let restoredState = {}
let completionCount = 0

// @TODO skip promises if no preloaders, potentially make getAllKeys a default preloader for parallelism
Promise.all(preloaders.map((p) => p(config))).then(() => {
storage.getAllKeys((err, allKeys) => {
if (err && process.env.NODE_ENV !== 'production') { console.warn('Error in storage.getAllKeys') }
let persistKeys = allKeys.filter((key) => key.indexOf(constants.keyPrefix) === 0).map((key) => key.slice(constants.keyPrefix.length))
let keysToRestore = Array.isArray(purgeMode)
? persistKeys.filter((key) => purgeMode.indexOf(key) === -1)
: purgeMode === '*' ? [] : persistKeys
storage.getAllKeys((err, allKeys) => {
if (err && process.env.NODE_ENV !== 'production') { console.warn('Error in storage.getAllKeys') }
let persistKeys = allKeys.filter((key) => key.indexOf(constants.keyPrefix) === 0).map((key) => key.slice(constants.keyPrefix.length))
let keysToRestore = Array.isArray(purgeMode)
? persistKeys.filter((key) => purgeMode.indexOf(key) === -1)
: purgeMode === '*' ? [] : persistKeys

let restoreCount = keysToRestore.length
if (restoreCount === 0) complete(null, restoredState)
forEach(keysToRestore, (key) => {
storage.getItem(createStorageKey(key), (err, serialized) => {
if (err && process.env.NODE_ENV !== 'production') console.warn('Error restoring data for key:', key, err)
else restoredState[key] = rehydrate(key, serialized)
completionCount += 1
if (completionCount === restoreCount) complete(null, restoredState)
})
let restoreCount = keysToRestore.length
if (restoreCount === 0) complete(null, restoredState)
forEach(keysToRestore, (key) => {
storage.getItem(createStorageKey(key), (err, serialized) => {
if (err && process.env.NODE_ENV !== 'production') console.warn('Error restoring data for key:', key, err)
else restoredState[key] = rehydrate(key, serialized)
completionCount += 1
if (completionCount === restoreCount) complete(null, restoredState)
})
})
})
Expand Down

0 comments on commit 001b8e5

Please sign in to comment.