Changeset View
Changeset View
Standalone View
Standalone View
web/redux/create-async-migrate.js
- This file was added.
// @flow | ||||||||||
import { DEFAULT_VERSION } from 'redux-persist/es/constants.js'; | ||||||||||
import type { PersistState } from 'redux-persist/es/types.js'; | ||||||||||
type MigrationManifest = { | ||||||||||
[number | string]: (PersistedState) => Promise<PersistedState>, | ||||||||||
}; | ||||||||||
type PersistedState = { | ||||||||||
_persist: PersistState, | ||||||||||
... | ||||||||||
} | void; | ||||||||||
ashoat: Inputs should be read-only | ||||||||||
function creatAsyncMigrate( | ||||||||||
ashoatUnsubmitted Not Done Inline Actions
Typo ashoat: Typo | ||||||||||
migrations: MigrationManifest, | ||||||||||
config: { debug: boolean }, | ||||||||||
ashoatUnsubmitted Not Done Inline ActionsInputs should be read-only ashoat: Inputs should be read-only | ||||||||||
): (state: PersistedState, currentVersion: number) => Promise<PersistedState> { | ||||||||||
const debug = !!config?.debug; | ||||||||||
return async function ( | ||||||||||
state: PersistedState, | ||||||||||
currentVersion: number, | ||||||||||
): Promise<PersistedState> { | ||||||||||
if (!state) { | ||||||||||
if (process.env.NODE_ENV !== 'production' && debug) { | ||||||||||
console.log('redux-persist: no inbound state, skipping migration'); | ||||||||||
} | ||||||||||
return undefined; | ||||||||||
tomekUnsubmitted Not Done Inline ActionsIs this correct to skip this? I may be wrong, but some redux functions accept State | void and it might be valid for a migration to receive undefined and return something. Not a big issue though, as that shouldn't happen in our case. tomek: Is this correct to skip this? I may be wrong, but some redux functions accept `State | void`… | ||||||||||
kamilAuthorUnsubmitted Done Inline ActionsIt might happen in our case, so let's discuss it in place where it will be introduced (D7668) kamil: It might happen in our case, so let's discuss it in place where it will be introduced (D7668) | ||||||||||
} | ||||||||||
const inboundVersion: number = | ||||||||||
state._persist && state._persist.version !== undefined | ||||||||||
? state._persist.version | ||||||||||
: DEFAULT_VERSION; | ||||||||||
if (inboundVersion === currentVersion) { | ||||||||||
if (process.env.NODE_ENV !== 'production' && debug) { | ||||||||||
console.log('redux-persist: versions match, noop migration'); | ||||||||||
} | ||||||||||
return state; | ||||||||||
} | ||||||||||
if (inboundVersion > currentVersion) { | ||||||||||
if (process.env.NODE_ENV !== 'production') { | ||||||||||
console.error('redux-persist: downgrading version is not supported'); | ||||||||||
} | ||||||||||
return state; | ||||||||||
} | ||||||||||
const migrationKeys = Object.keys(migrations) | ||||||||||
.map(ver => parseInt(ver)) | ||||||||||
.filter(key => currentVersion >= key && key > inboundVersion) | ||||||||||
.sort((a, b) => a - b); | ||||||||||
tomekUnsubmitted Not Done Inline ActionsIn MigrationManifest we allow string as a key. Is it a good idea to sort them like that? tomek: In `MigrationManifest` we allow `string` as a key. Is it a good idea to sort them like that? | ||||||||||
if (process.env.NODE_ENV !== 'production' && debug) { | ||||||||||
console.log('redux-persist: migrationKeys', migrationKeys); | ||||||||||
} | ||||||||||
let migratedState: PersistedState = state; | ||||||||||
for (const versionKey of migrationKeys) { | ||||||||||
if (process.env.NODE_ENV !== 'production' && debug) { | ||||||||||
console.log( | ||||||||||
'redux-persist: running migration for versionKey', | ||||||||||
versionKey, | ||||||||||
); | ||||||||||
} | ||||||||||
if (versionKey) { | ||||||||||
tomekUnsubmitted Not Done Inline ActionsCan this be falsy? tomek: Can this be falsy? | ||||||||||
migratedState = await migrations[versionKey](migratedState); | ||||||||||
tomekUnsubmitted Not Done Inline ActionsWhat is the plan for handling exceptions here? Do we want them to be just thrown which would mean that all the migrations failed? tomek: What is the plan for handling exceptions here? Do we want them to be just thrown which would… | ||||||||||
} | ||||||||||
} | ||||||||||
return migratedState; | ||||||||||
}; | ||||||||||
} | ||||||||||
export { creatAsyncMigrate }; |
Inputs should be read-only