Skip to content

Commit

Permalink
Enable createMigrate to handle async migrations rt2zz#866
Browse files Browse the repository at this point in the history
  • Loading branch information
retyui committed Jul 1, 2020
1 parent d7efde9 commit ad03c29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 13 additions & 1 deletion docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ const migrations = {
device: undefined
}
},
1: (state) => {
// You can use also `async () => {}` function
// For example to move users to other storage
1: async ({ users, ...state }) => {
try {
await insertToOtherStorage(users)

return state
} catch (e) {
// Don't forget about exceptions
return state
}
},
2: (state) => {
// migration to keep only device state
return {
device: state.device
Expand Down
9 changes: 5 additions & 4 deletions src/createMigrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ export default function createMigrate(
if (process.env.NODE_ENV !== 'production' && debug)
console.log('redux-persist: migrationKeys', migrationKeys)
try {
let migratedState = migrationKeys.reduce((state, versionKey) => {
return migrationKeys.reduce((promiseState, 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)
return promiseState.then(state =>
Promise.resolve(migrations[versionKey](state))
)
}, Promise.resolve(state))
} catch (err) {
return Promise.reject(err)
}
Expand Down

0 comments on commit ad03c29

Please sign in to comment.