|
|
@@ -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()
|
|
|
},
|
|
|
|