Skip to content

Commit

Permalink
fix(persist): fix async migrate on persist middleware (#2877)
Browse files Browse the repository at this point in the history
* fix(perist): add support for async migrate on persist middleware

* feat(vitest): rename vitest.config.ts to vitest.config.mts

* fix: fix minor issues

* fix: fix minor issues

* feat: minor changes

* feat: revert vitest changes

---------

Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
dbritto-dev and dai-shi authored Dec 4, 2024
1 parent af7ff21 commit 9bae069
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
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

0 comments on commit 9bae069

Please sign in to comment.