Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(persist): fix async migrate on persist middleware #2877

Merged
merged 7 commits into from
Dec 4, 2024
Merged
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
15 changes: 8 additions & 7 deletions src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,14 @@ const persistImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
deserializedStorageValue.version !== options.version
) {
if (options.migrate) {
return [
true,
options.migrate(
deserializedStorageValue.state,
deserializedStorageValue.version,
),
] as const
const migration = options.migrate(
deserializedStorageValue.state,
deserializedStorageValue.version,
)
if (migration instanceof Promise) {
return migration.then((result) => [true, result] as const)
}
return [true, migration] as const
}
console.error(
`State loaded from storage couldn't be migrated since no migrate function was provided`,
Expand Down
4 changes: 2 additions & 2 deletions tests/persistAsync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ describe('persist middleware with async configuration', () => {
})
})

it('can migrate persisted state', async () => {
it('can async migrate persisted state', async () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => ({ count: 99 }))
const migrateSpy = vi.fn(() => Promise.resolve({ count: 99 }))

const storage = {
getItem: async () =>
Expand Down
2 changes: 1 addition & 1 deletion tests/persistSync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('persist middleware with sync configuration', () => {
expect(onRehydrateStorageSpy2).toBeCalledWith({ count: 42 }, undefined)
})

it('can migrate persisted state', () => {
it('can non-async migrate persisted state', () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => ({ count: 99 }))
Expand Down
Loading