Skip to content

Commit

Permalink
fix(useStorage): read object only when it's serialized differently (#…
Browse files Browse the repository at this point in the history
…2782) (#3091)

Co-authored-by: chenglu <jiale.qiu@unionman.com.cn>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people committed Aug 25, 2023
1 parent c2f641d commit 6fabe9b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/core/useStorage/index.test.ts
@@ -1,5 +1,5 @@
import { debounceFilter, promiseTimeout } from '@vueuse/shared'
import { isVue3, ref } from 'vue-demi'
import { isVue3, ref, toRaw } from 'vue-demi'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTwoTick, useSetup } from '../../.test'
import { StorageSerializers, customStorageEventName, useStorage } from '.'
Expand Down Expand Up @@ -164,10 +164,12 @@ describe('useStorage', () => {
data: 123,
})

const storeRaw = toRaw(store.value)
store.value.name = 'b'
await nextTwoTick()

expect(storage.setItem).toBeCalledWith(KEY, '{"name":"b","data":123}')
expect(storeRaw).toBe(toRaw(store.value))

store.value.data = 321
await nextTwoTick()
Expand Down
3 changes: 2 additions & 1 deletion packages/core/useStorage/index.ts
Expand Up @@ -254,7 +254,8 @@ export function useStorage<T extends(string | number | boolean | object | null)>

pauseWatch()
try {
data.value = read(event)
if (event?.newValue !== serializer.write(data.value))
data.value = read(event)
}
catch (e) {
onError(e)
Expand Down

0 comments on commit 6fabe9b

Please sign in to comment.