forked from elk-zone/elk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from maybeanerd/feat/update-from-upstream
fix: update from upstream v0.15.1
- Loading branch information
Showing
10 changed files
with
329 additions
and
248 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
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,7 +1,7 @@ | ||
{ | ||
"name": "@maybeanerd/crab", | ||
"type": "module", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"packageManager": "[email protected]", | ||
"license": "MIT", | ||
"homepage": "https://crab.bumscode.com/", | ||
|
Oops, something went wrong.