You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
40 lines
1.3 KiB
import { KEY_PREFIX } from './constants'; |
|
export default function getStoredState(config) { |
|
var transforms = config.transforms || []; |
|
var storageKey = "".concat(config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX).concat(config.key); |
|
var storage = config.storage; |
|
var debug = config.debug; |
|
var deserialize; |
|
|
|
if (config.deserialize === false) { |
|
deserialize = function deserialize(x) { |
|
return x; |
|
}; |
|
} else if (typeof config.deserialize === 'function') { |
|
deserialize = config.deserialize; |
|
} else { |
|
deserialize = defaultDeserialize; |
|
} |
|
|
|
return storage.getItem(storageKey).then(function (serialized) { |
|
if (!serialized) return undefined;else { |
|
try { |
|
var state = {}; |
|
var rawState = deserialize(serialized); |
|
Object.keys(rawState).forEach(function (key) { |
|
state[key] = transforms.reduceRight(function (subState, transformer) { |
|
return transformer.out(subState, key, rawState); |
|
}, deserialize(rawState[key])); |
|
}); |
|
return state; |
|
} catch (err) { |
|
if (process.env.NODE_ENV !== 'production' && debug) console.log("redux-persist/getStoredState: Error restoring data ".concat(serialized), err); |
|
throw err; |
|
} |
|
} |
|
}); |
|
} |
|
|
|
function defaultDeserialize(serial) { |
|
return JSON.parse(serial); |
|
} |