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.
1 line
12 KiB
1 line
12 KiB
2 years ago
|
{"ast":null,"code":"import { KEY_PREFIX, REHYDRATE } from './constants'; // @TODO remove once flow < 0.63 support is no longer required.\n\nexport default function createPersistoid(config) {\n // defaults\n var blacklist = config.blacklist || null;\n var whitelist = config.whitelist || null;\n var transforms = config.transforms || [];\n var throttle = config.throttle || 0;\n var storageKey = \"\".concat(config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX).concat(config.key);\n var storage = config.storage;\n var serialize;\n\n if (config.serialize === false) {\n serialize = function serialize(x) {\n return x;\n };\n } else if (typeof config.serialize === 'function') {\n serialize = config.serialize;\n } else {\n serialize = defaultSerialize;\n }\n\n var writeFailHandler = config.writeFailHandler || null; // initialize stateful values\n\n var lastState = {};\n var stagedState = {};\n var keysToProcess = [];\n var timeIterator = null;\n var writePromise = null;\n\n var update = function update(state) {\n // add any changed keys to the queue\n Object.keys(state).forEach(function (key) {\n if (!passWhitelistBlacklist(key)) return; // is keyspace ignored? noop\n\n if (lastState[key] === state[key]) return; // value unchanged? noop\n\n if (keysToProcess.indexOf(key) !== -1) return; // is key already queued? noop\n\n keysToProcess.push(key); // add key to queue\n }); //if any key is missing in the new state which was present in the lastState,\n //add it for processing too\n\n Object.keys(lastState).forEach(function (key) {\n if (state[key] === undefined && passWhitelistBlacklist(key) && keysToProcess.indexOf(key) === -1 && lastState[key] !== undefined) {\n keysToProcess.push(key);\n }\n }); // start the time iterator if not running (read: throttle)\n\n if (timeIterator === null) {\n timeIterator = setInterval(processNextKey, throttle);\n }\n\n lastState = state;\n };\n\n function processNextKey() {\n if (keysToProcess.length === 0) {\n if (timeIterator) clearInterval(timeIterator);\n timeIterator = null;\n return;\n }\n\n var key = keysToProcess.shift();\n var endState = transforms.reduce(function (subState, transformer) {\n return transformer.in(subState, key, lastState);\n }, lastState[key]);\n\n if (endState !== undefined) {\n try {\n stagedState[key] = serialize(endState);\n } catch (err) {\n console.error('redux-persist/createPersistoid: error serializing state', err);\n }\n } else {\n //if the endState is undefined, no need to persist the existing serialized content\n delete stagedState[key];\n }\n\n if (keysToProcess.length === 0) {\n writeStagedState();\n }\n }\n\n function writeStagedState() {\n // cleanup any removed keys just before write.\n Object.keys(stagedState).forEach(function (key) {\n if (lastState[key] === undefined) {\n delete stagedState[key];\n }\n });\n writePromise = storage.setItem(storageKey, serialize(stagedState)).catch(onWriteFail);\n }\n\n function passWhitelistBlacklist(key) {\n if (whitelist && whitelist.indexOf(key) === -1 && key !== '_persist') return false;\n if (blacklist && blacklist.indexOf(key) !== -1) return false;\n return true;\n }\n\n function onWriteFail(err) {\n // @TODO add fail handlers (typically storage full)\n if (writeFailHandler) writeFailHandler(err);\n\n if (err && process.env.NODE_ENV !== 'production') {\n console.error('Error storing data', err);\n }\n }\n\n var flush = function flush() {\n while (keysToProcess.length !== 0) {\n processNextKey();\n }\n\n return writePromise || Promise.resolve();\n }; // return `persistoid`\n\n\n return {\n update: update,\n flush: flush\n };\n} // @NOTE in the future this may be exposed via config\n\nfunction defaultSerialize(data) {\n return JSON.stringify(data);\n}","map":{"version":3,"names":["KEY_PREFIX","REHYDRATE","createPersistoid","conf
|