createMigrate.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = createMigrate;
  4. var _constants = require("./constants");
  5. function createMigrate(migrations, config) {
  6. var _ref = config || {},
  7. debug = _ref.debug;
  8. return function (state, currentVersion) {
  9. if (!state) {
  10. if (process.env.NODE_ENV !== 'production' && debug) console.log('redux-persist: no inbound state, skipping migration');
  11. return Promise.resolve(undefined);
  12. }
  13. var inboundVersion = state._persist && state._persist.version !== undefined ? state._persist.version : _constants.DEFAULT_VERSION;
  14. if (inboundVersion === currentVersion) {
  15. if (process.env.NODE_ENV !== 'production' && debug) console.log('redux-persist: versions match, noop migration');
  16. return Promise.resolve(state);
  17. }
  18. if (inboundVersion > currentVersion) {
  19. if (process.env.NODE_ENV !== 'production') console.error('redux-persist: downgrading version is not supported');
  20. return Promise.resolve(state);
  21. }
  22. var migrationKeys = Object.keys(migrations).map(function (ver) {
  23. return parseInt(ver);
  24. }).filter(function (key) {
  25. return currentVersion >= key && key > inboundVersion;
  26. }).sort(function (a, b) {
  27. return a - b;
  28. });
  29. if (process.env.NODE_ENV !== 'production' && debug) console.log('redux-persist: migrationKeys', migrationKeys);
  30. try {
  31. var migratedState = migrationKeys.reduce(function (state, versionKey) {
  32. if (process.env.NODE_ENV !== 'production' && debug) console.log('redux-persist: running migration for versionKey', versionKey);
  33. return migrations[versionKey](state);
  34. }, state);
  35. return Promise.resolve(migratedState);
  36. } catch (err) {
  37. return Promise.reject(err);
  38. }
  39. };
  40. }