Skip to content

Commit

Permalink
fix(useStorage): read object only when it's serialized differently (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
chenglu authored and chenglu4343 committed May 16, 2023
1 parent 7da7c4e commit d49736d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/core/useStorage/index.test.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion packages/core/useStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ export function useStorage<T extends(string | number | boolean | object | null)>

pauseWatch()
try {
data.value = read(event)
const newData = read(event)
if (typeof newData !== 'object' || serializer.write(newData) !== serializer.write(data.value))
data.value = read(event)
}
catch (e) {
onError(e)
Expand Down

0 comments on commit d49736d

Please sign in to comment.