summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop/src/utils
diff options
context:
space:
mode:
authorAdam <[email protected]>2025-12-17 13:10:57 -0600
committerAdam <[email protected]>2025-12-17 13:11:02 -0600
commit4a3ba58f65d12b4f7c7a97b42a5bb3bc0eb5f88b (patch)
treeabae119fe310b1c73fc0d70d9d0d81985c5def42 /packages/desktop/src/utils
parent2a3a8a1ec2d71ae27730226cfac37830c7a5dfd7 (diff)
downloadopencode-4a3ba58f65d12b4f7c7a97b42a5bb3bc0eb5f88b.tar.gz
opencode-4a3ba58f65d12b4f7c7a97b42a5bb3bc0eb5f88b.zip
chore: localStorage -> tauri store
Diffstat (limited to 'packages/desktop/src/utils')
-rw-r--r--packages/desktop/src/utils/persist.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/packages/desktop/src/utils/persist.ts b/packages/desktop/src/utils/persist.ts
new file mode 100644
index 000000000..12b334f9f
--- /dev/null
+++ b/packages/desktop/src/utils/persist.ts
@@ -0,0 +1,26 @@
+import { usePlatform } from "@/context/platform"
+import { makePersisted } from "@solid-primitives/storage"
+import { createResource, type Accessor } from "solid-js"
+import type { SetStoreFunction, Store } from "solid-js/store"
+
+type InitType = Promise<string> | string | null
+type PersistedWithReady<T> = [Store<T>, SetStoreFunction<T>, InitType, Accessor<boolean>]
+
+export function persisted<T>(key: string, store: [Store<T>, SetStoreFunction<T>]): PersistedWithReady<T> {
+ const platform = usePlatform()
+ const [state, setState, init] = makePersisted(store, { name: key, storage: platform.storage?.() ?? localStorage })
+
+ // Create a resource that resolves when the store is initialized
+ // This integrates with Suspense and provides a ready signal
+ const isAsync = init instanceof Promise
+ const [ready] = createResource(
+ () => init,
+ async (initValue) => {
+ if (initValue instanceof Promise) await initValue
+ return true
+ },
+ { initialValue: !isAsync },
+ )
+
+ return [state, setState, init, () => ready() === true]
+}