From 65bc72098b737bee12f388215913808fa1629a4d Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Wed, 31 Dec 2025 12:00:39 -0600 Subject: fix(desktop): more defensive access --- packages/desktop/src/index.tsx | 89 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 16 deletions(-) (limited to 'packages/desktop/src') diff --git a/packages/desktop/src/index.tsx b/packages/desktop/src/index.tsx index 6c2106935..4d3c8b36d 100644 --- a/packages/desktop/src/index.tsx +++ b/packages/desktop/src/index.tsx @@ -57,19 +57,71 @@ const platform: Platform = { }, openLink(url: string) { - shellOpen(url) + void shellOpen(url).catch(() => undefined) }, storage: (name = "default.dat") => { - const api: AsyncStorage = { + type StoreLike = { + get(key: string): Promise + set(key: string, value: string): Promise + delete(key: string): Promise + clear(): Promise + keys(): Promise + length(): Promise + } + + const memory = () => { + const data = new Map() + const store: StoreLike = { + get: async (key) => data.get(key), + set: async (key, value) => { + data.set(key, value) + }, + delete: async (key) => { + data.delete(key) + }, + clear: async () => { + data.clear() + }, + keys: async () => Array.from(data.keys()), + length: async () => data.size, + } + return store + } + + const api: AsyncStorage & { _store: Promise | null; _getStore: () => Promise } = { _store: null, - _getStore: async () => api._store || (api._store = Store.load(name)), - 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), - clear: async () => await (await api._getStore()).clear(), - key: async (index: number) => (await (await api._getStore()).keys())[index], - getLength: async () => (await api._getStore()).length(), + _getStore: async () => { + if (api._store) return api._store + api._store = Store.load(name).catch(() => memory()) + return api._store + }, + getItem: async (key: string) => { + const store = await api._getStore() + const value = await store.get(key).catch(() => null) + if (value === undefined) return null + return value + }, + setItem: async (key: string, value: string) => { + const store = await api._getStore() + await store.set(key, value).catch(() => undefined) + }, + removeItem: async (key: string) => { + const store = await api._getStore() + await store.delete(key).catch(() => undefined) + }, + clear: async () => { + const store = await api._getStore() + await store.clear().catch(() => undefined) + }, + key: async (index: number) => { + const store = await api._getStore() + return (await store.keys().catch(() => []))[index] + }, + getLength: async () => { + const store = await api._getStore() + return await store.length().catch(() => 0) + }, get length() { return api.getLength() }, @@ -79,20 +131,25 @@ const platform: Platform = { checkUpdate: async () => { if (!UPDATER_ENABLED) return { updateAvailable: false } - update = await check() - if (!update) return { updateAvailable: false } - await update.download() - return { updateAvailable: true, version: update.version } + const next = await check().catch(() => null) + if (!next) return { updateAvailable: false } + const ok = await next + .download() + .then(() => true) + .catch(() => false) + if (!ok) return { updateAvailable: false } + update = next + return { updateAvailable: true, version: next.version } }, update: async () => { if (!UPDATER_ENABLED || !update) return - if (ostype() === "windows") await invoke("kill_sidecar") - await update.install() + if (ostype() === "windows") await invoke("kill_sidecar").catch(() => undefined) + await update.install().catch(() => undefined) }, restart: async () => { - await invoke("kill_sidecar") + await invoke("kill_sidecar").catch(() => undefined) await relaunch() }, -- cgit v1.2.3