-
-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change page reload and account switch logic (#2975)
- Loading branch information
Showing
9 changed files
with
328 additions
and
253 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
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 |
---|---|---|
@@ -1,72 +1,75 @@ | ||
import type { MaybeRefOrGetter, RemovableRef } from '@vueuse/core' | ||
import type { Ref } from 'vue' | ||
import type { UseIDBOptions } from '@vueuse/integrations/useIDBKeyval' | ||
import { del, get, set, update } from '~/utils/elk-idb' | ||
|
||
const isIDBSupported = !process.test && typeof indexedDB !== 'undefined' | ||
export interface UseAsyncIDBKeyvalReturn<T> { | ||
set: (value: T) => Promise<void> | ||
readIDB: () => Promise<T | undefined> | ||
} | ||
|
||
export async function useAsyncIDBKeyval<T>( | ||
key: IDBValidKey, | ||
initialValue: MaybeRefOrGetter<T>, | ||
options: UseIDBOptions = {}, | ||
source?: Ref<T>, | ||
): Promise<RemovableRef<T>> { | ||
source: RemovableRef<T>, | ||
options: Omit<UseIDBOptions, 'shallow'> = {}, | ||
): Promise<UseAsyncIDBKeyvalReturn<T>> { | ||
const { | ||
flush = 'pre', | ||
deep = true, | ||
shallow, | ||
writeDefaults = true, | ||
onError = (e: unknown) => { | ||
console.error(e) | ||
}, | ||
} = options | ||
|
||
const data = source ?? (shallow ? shallowRef : ref)(initialValue) as Ref<T> | ||
|
||
const rawInit: T = toValue<T>(initialValue) | ||
|
||
async function read() { | ||
if (!isIDBSupported) | ||
return | ||
try { | ||
const rawValue = await get<T>(key) | ||
if (rawValue === undefined) { | ||
if (rawInit !== undefined && rawInit !== null) | ||
await set(key, rawInit) | ||
} | ||
else { | ||
data.value = rawValue | ||
try { | ||
const rawValue = await get<T>(key) | ||
if (rawValue === undefined) { | ||
if (rawInit !== undefined && rawInit !== null && writeDefaults) { | ||
await set(key, rawInit) | ||
source.value = rawInit | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
else { | ||
source.value = rawValue | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
} | ||
|
||
await read() | ||
|
||
async function write() { | ||
if (!isIDBSupported) | ||
return | ||
async function write(data: T) { | ||
try { | ||
if (data.value == null) { | ||
if (data == null) { | ||
await del(key) | ||
} | ||
else { | ||
// IndexedDB does not support saving proxies, convert from proxy before saving | ||
if (Array.isArray(data.value)) | ||
await update(key, () => (JSON.parse(JSON.stringify(data.value)))) | ||
else if (typeof data.value === 'object') | ||
await update(key, () => ({ ...data.value })) | ||
else | ||
await update(key, () => (data.value)) | ||
await update(key, () => toRaw(data)) | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
} | ||
} | ||
|
||
watch(data, () => write(), { flush, deep }) | ||
const { | ||
pause: pauseWatch, | ||
resume: resumeWatch, | ||
} = watchPausable(source, data => write(data), { flush, deep }) | ||
|
||
async function setData(value: T): Promise<void> { | ||
pauseWatch() | ||
try { | ||
await write(value) | ||
source.value = value | ||
} | ||
finally { | ||
resumeWatch() | ||
} | ||
} | ||
|
||
return data as RemovableRef<T> | ||
return { set: setData, readIDB: () => get<T>(key) } | ||
} |
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
Oops, something went wrong.