forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function guessSerializerType<T extends(string | number | boolean | object | null)>(rawInit: T) { | ||
return rawInit == null | ||
? 'any' | ||
: rawInit instanceof Set | ||
? 'set' | ||
: rawInit instanceof Map | ||
? 'map' | ||
: typeof rawInit === 'boolean' | ||
? 'boolean' | ||
: typeof rawInit === 'string' | ||
? 'string' | ||
: typeof rawInit === 'object' | ||
? 'object' | ||
: Array.isArray(rawInit) | ||
? 'object' | ||
: !Number.isNaN(rawInit) | ||
? 'number' | ||
: 'any' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
category: State | ||
--- | ||
|
||
# useStorageAsync | ||
|
||
Reactive Storage in with async support. | ||
|
||
Prefer to `useStorage`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { watchWithFilter, MaybeRef, RemovableRef } from '@vueuse/shared' | ||
import { ref, Ref, unref, shallowRef } from 'vue-demi' | ||
import { SerializerAsync, StorageLikeAsync, StorageOptions, StorageSerializers } from '../useStorage' | ||
import { useEventListener } from '../useEventListener' | ||
import { guessSerializerType } from '../useStorage/guess' | ||
import { defaultWindow } from '../_configurable' | ||
|
||
export interface StorageAsyncOptions<T> extends Omit<StorageOptions<T>, 'serializer'> { | ||
/** | ||
* Custom data serialization | ||
*/ | ||
serializer?: SerializerAsync<T> | ||
} | ||
|
||
export function useStorageAsync(key: string, initialValue: MaybeRef<string>, storage?: StorageLikeAsync, options?: StorageAsyncOptions<string>): RemovableRef<string> | ||
export function useStorageAsync(key: string, initialValue: MaybeRef<boolean>, storage?: StorageLikeAsync, options?: StorageAsyncOptions<boolean>): RemovableRef<boolean> | ||
export function useStorageAsync(key: string, initialValue: MaybeRef<number>, storage?: StorageLikeAsync, options?: StorageAsyncOptions<number>): RemovableRef<number> | ||
export function useStorageAsync<T> (key: string, initialValue: MaybeRef<T>, storage?: StorageLikeAsync, options?: StorageAsyncOptions<T>): RemovableRef<T> | ||
export function useStorageAsync<T = unknown> (key: string, initialValue: MaybeRef<null>, storage?: StorageLikeAsync, options?: StorageAsyncOptions<T>): RemovableRef<T> | ||
|
||
/** | ||
* Reactive Storage in with async support. | ||
* | ||
* @see https://vueuse.org/useStorageAsync | ||
* @param key | ||
* @param initialValue | ||
* @param storage | ||
* @param options | ||
*/ | ||
export function useStorageAsync<T extends(string|number|boolean|object|null)> ( | ||
key: string, | ||
initialValue: MaybeRef<T>, | ||
storage: StorageLikeAsync | undefined = defaultWindow?.localStorage, | ||
options: StorageAsyncOptions<T> = {}, | ||
): RemovableRef<T> { | ||
const { | ||
flush = 'pre', | ||
deep = true, | ||
listenToStorageChanges = true, | ||
writeDefaults = true, | ||
shallow, | ||
window = defaultWindow, | ||
eventFilter, | ||
onError = (e) => { | ||
console.error(e) | ||
}, | ||
} = options | ||
|
||
const rawInit: T = unref(initialValue) | ||
const type = guessSerializerType<T>(rawInit) | ||
|
||
const data = (shallow ? shallowRef : ref)(initialValue) as Ref<T> | ||
const serializer = options.serializer ?? StorageSerializers[type] | ||
|
||
async function read(event?: StorageEvent) { | ||
if (!storage || (event && event.key !== key)) | ||
return | ||
|
||
try { | ||
const rawValue = event ? event.newValue : await storage.getItem(key) | ||
if (rawValue == null) { | ||
data.value = rawInit | ||
if (writeDefaults && rawInit !== null) | ||
await storage.setItem(key, await serializer.write(rawInit)) | ||
} | ||
else { | ||
data.value = await serializer.read(rawValue) | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
} | ||
} | ||
|
||
read() | ||
|
||
if (window && listenToStorageChanges) | ||
useEventListener(window, 'storage', e => setTimeout(() => read(e), 0)) | ||
|
||
if (storage) { | ||
watchWithFilter( | ||
data, | ||
async() => { | ||
try { | ||
if (data.value == null) | ||
await storage.removeItem(key) | ||
else | ||
await storage.setItem(key, await serializer.write(data.value)) | ||
} | ||
catch (e) { | ||
onError(e) | ||
} | ||
}, | ||
{ | ||
flush, | ||
deep, | ||
eventFilter, | ||
}, | ||
) | ||
} | ||
|
||
return data as RemovableRef<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters