Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/storage/src/tauri.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { AsyncStorage } from "./persisted.js";
import type { StoreOptions } from "@tauri-apps/plugin-store";
import { AsyncStorage } from "@solid-primitives/storage";

/**
* tauriStorage: an asynchronous Storage API based on tauri-plugin-store
*
* requires store permissions to be set: https://beta.tauri.app/features/store/
* requires store permissions to be set: https://tauri.app/plugin/store/
*
* In order to use in a isomorphic setting (web/tauri), use:
* ```ts
* const isFallback = !window.__TAURI_INTERNALS__;
* const storage = isFallback ? localStorage : tauriStorage();
* ````
*/
export function tauriStorage(name = "solid-storage.dat") {
export function tauriStorage(
name = "solid-storage.dat",
options: StoreOptions = {},
) {
const api: AsyncStorage = {
_store: null,
_getStore: async () =>
// @ts-ignore
api._store || (api._store = new (await import("@tauri-apps/plugin-store")).Store(name)),
_getStore: async () => {
if (!api._store) {
// @ts-ignore
const store = await import("@tauri-apps/plugin-store");
api._store = await store.load(name, options);
}
return api._store;
},
getItem: async (key: string) => (await (await api._getStore()).get(key)) ?? null,
setItem: async (key: string, value: string) => await (await api._getStore()).set(key, value),
removeItem: async (key: string) => await (await api._getStore()).delete(key),
Expand Down