diff options
| author | Adam <[email protected]> | 2025-12-31 12:00:39 -0600 |
|---|---|---|
| committer | Adam <[email protected]> | 2025-12-31 13:12:30 -0600 |
| commit | 65bc72098b737bee12f388215913808fa1629a4d (patch) | |
| tree | 5e392743dd2e4a5f7874d05bb99c20fe6f690bb2 /packages/desktop/src | |
| parent | b5546dce802a6befcb7e8b03577543399cef795b (diff) | |
| download | opencode-65bc72098b737bee12f388215913808fa1629a4d.tar.gz opencode-65bc72098b737bee12f388215913808fa1629a4d.zip | |
fix(desktop): more defensive access
Diffstat (limited to 'packages/desktop/src')
| -rw-r--r-- | packages/desktop/src/index.tsx | 89 |
1 files changed, 73 insertions, 16 deletions
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<string | null | undefined> + set(key: string, value: string): Promise<unknown> + delete(key: string): Promise<unknown> + clear(): Promise<unknown> + keys(): Promise<string[]> + length(): Promise<number> + } + + const memory = () => { + const data = new Map<string, string>() + 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<StoreLike> | null; _getStore: () => Promise<StoreLike> } = { _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() }, |
