Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const migrations = {
device: undefined
}
},
1: (state) => {
// Async functions are allowed
1: async (state) => {
// migration to keep only device state
return {
device: state.device
Expand Down
29 changes: 14 additions & 15 deletions src/createMigrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export default function createMigrate(
config?: { debug: boolean }
): (state: PersistedState, currentVersion: number) => Promise<PersistedState> {
const { debug } = config || {}
return function(
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 Promise.resolve(undefined)
return undefined
}

const inboundVersion: number =
Expand All @@ -25,7 +25,7 @@ export default function createMigrate(
if (inboundVersion === currentVersion) {
if (process.env.NODE_ENV !== 'production' && debug)
console.log('redux-persist: versions match, noop migration')
return Promise.resolve(state)
return state
}
if (inboundVersion > currentVersion) {
if (process.env.NODE_ENV !== 'production')
Expand All @@ -40,18 +40,17 @@ export default function createMigrate(

if (process.env.NODE_ENV !== 'production' && debug)
console.log('redux-persist: migrationKeys', migrationKeys)
try {
const migratedState: any = migrationKeys.reduce((state: any, versionKey) => {
if (process.env.NODE_ENV !== 'production' && debug)
console.log(
'redux-persist: running migration for versionKey',
versionKey
)
return migrations[versionKey](state)
}, state)
return Promise.resolve(migratedState)
} catch (err) {
return Promise.reject(err)
let migratedState: any = state

for (const versionKey of migrationKeys) {
if (process.env.NODE_ENV !== 'production' && debug)
console.log(
'redux-persist: running migration for versionKey',
versionKey
)
migratedState = await migrations[versionKey](migratedState)
}

return migratedState
}
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export interface WebStorage extends Storage {
}

export interface MigrationManifest {
[key: string]: (state: PersistedState) => PersistedState;
[key: string]:
| ((state: PersistedState) => PersistedState)
| ((state: PersistedState) => Promise<PersistedState>)
}

/**
Expand Down