From f043d7afe0ef96eb1d68ae140470d8c30b563a1a Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Tue, 5 Nov 2024 14:31:16 -0500 Subject: [PATCH] feat: add createTauriSyncStore factory function for creating sync svelte store --- apps/desktop/src/lib/stores/appConfig.ts | 10 +++--- apps/desktop/src/lib/utils/sync-store.ts | 44 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 apps/desktop/src/lib/utils/sync-store.ts diff --git a/apps/desktop/src/lib/stores/appConfig.ts b/apps/desktop/src/lib/stores/appConfig.ts index 1eecad1..78ebcd0 100644 --- a/apps/desktop/src/lib/stores/appConfig.ts +++ b/apps/desktop/src/lib/stores/appConfig.ts @@ -1,12 +1,10 @@ import { getExtensionsFolder } from "@/constants" -import { themeConfigStore, updateTheme, type ThemeConfig } from "@kksh/svelte5" +import { createTauriSyncStore, type WithSyncStore } from "@/utils/sync-store" +import { updateTheme, type ThemeConfig } from "@kksh/svelte5" import { PersistedAppConfig, type AppConfig } from "@kksh/types" -import * as path from "@tauri-apps/api/path" -import { remove } from "@tauri-apps/plugin-fs" import { debug, error } from "@tauri-apps/plugin-log" import * as os from "@tauri-apps/plugin-os" import { load } from "@tauri-apps/plugin-store" -import { get, writable, type Writable } from "svelte/store" import * as v from "valibot" export const defaultAppConfig: AppConfig = { @@ -35,8 +33,8 @@ interface AppConfigAPI { setDevExtensionPath: (devExtensionPath: string | null) => void } -function createAppConfig(): Writable & AppConfigAPI { - const store = writable(defaultAppConfig) +function createAppConfig(): WithSyncStore & AppConfigAPI { + const store = createTauriSyncStore("app-config", defaultAppConfig) async function init() { debug("Initializing app config") diff --git a/apps/desktop/src/lib/utils/sync-store.ts b/apps/desktop/src/lib/utils/sync-store.ts new file mode 100644 index 0000000..667f6a5 --- /dev/null +++ b/apps/desktop/src/lib/utils/sync-store.ts @@ -0,0 +1,44 @@ +import * as evt from "@tauri-apps/api/event" +import { writable, type Writable } from "svelte/store" + +export function buildEventName(storeName: string) { + return `app://sync-store-${storeName}` +} + +export type WithSyncStore = Writable & { + listen: () => void + unlisten: evt.UnlistenFn | undefined +} + +export function createTauriSyncStore(storeName: string, initialValue: T): WithSyncStore { + const store = writable(initialValue) + let unlisten: evt.UnlistenFn | undefined + + async function listen() { + console.log("[listen] start", storeName) + if (unlisten) { + console.log("[listen] already listening, skip") + return + } + const _unlisten = await evt.listen<{ value: T }>(buildEventName(storeName), (evt) => { + console.log(`[listen] update from tauri event`, storeName, evt.payload.value) + store.set(evt.payload.value) + }) + const unsubscribe = store.subscribe((value) => { + console.log("[subscribe] got update, emit data", storeName, value) + evt.emit(buildEventName(storeName), { value }) + }) + unlisten = () => { + _unlisten() + unsubscribe() + unlisten = undefined + } + return unlisten + } + + return { + ...store, + listen, + unlisten + } +}