Created
October 30, 2023 11:05
-
-
Save johannschopplich/488d89e4c431475f3192f2fbb2004bfc to your computer and use it in GitHub Desktop.
Capacitor Preferences binding for VueUse `useStorageAsync`
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
import { Preferences } from "@capacitor/preferences"; | |
import type { | |
MaybeRefOrGetter, | |
RemovableRef, | |
StorageLikeAsync, | |
UseStorageAsyncOptions, | |
} from "@vueuse/core"; | |
const capacitorPreferenceStorage: StorageLikeAsync = { | |
async getItem(key: string): Promise<string | null> { | |
const { value } = await Preferences.get({ key }); | |
return value; | |
}, | |
setItem(key: string, value: string): Promise<void> { | |
return Preferences.set({ key, value }); | |
}, | |
removeItem(key: string): Promise<void> { | |
return Preferences.remove({ key }); | |
}, | |
}; | |
export function useCapacitorPreference( | |
key: string, | |
defaults: MaybeRefOrGetter<string>, | |
options?: UseStorageAsyncOptions<string>, | |
): RemovableRef<string>; | |
export function useCapacitorPreference( | |
key: string, | |
defaults: MaybeRefOrGetter<boolean>, | |
options?: UseStorageAsyncOptions<boolean>, | |
): RemovableRef<boolean>; | |
export function useCapacitorPreference( | |
key: string, | |
defaults: MaybeRefOrGetter<number>, | |
options?: UseStorageAsyncOptions<number>, | |
): RemovableRef<number>; | |
export function useCapacitorPreference<T>( | |
key: string, | |
defaults: MaybeRefOrGetter<T>, | |
options?: UseStorageAsyncOptions<T>, | |
): RemovableRef<T>; | |
export function useCapacitorPreference<T = unknown>( | |
key: string, | |
defaults: MaybeRefOrGetter<null>, | |
options?: UseStorageAsyncOptions<T>, | |
): RemovableRef<T>; | |
export function useCapacitorPreference< | |
T extends string | number | boolean | object | null, | |
>( | |
key: string, | |
defaults: MaybeRefOrGetter<T>, | |
options: UseStorageAsyncOptions<T> = {}, | |
): RemovableRef<T> { | |
return useStorageAsync<T>(key, defaults, capacitorPreferenceStorage, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment