Files
geo/apps/desktop-client/src/preload/bridge.ts
T

60 lines
2.5 KiB
TypeScript
Raw Normal View History

import { contextBridge, ipcRenderer } from "electron/renderer";
import type { IpcRendererEvent } from "electron";
import type {
CreatePublishJobResponse,
DesktopAccountInfo,
DesktopPublishTaskListResponse,
DesktopRuntimeSessionSyncRequest,
ListDesktopPublishTasksParams,
} from "@geo/shared-types";
import type { DesktopRuntimeSnapshot } from "../renderer/types";
interface DesktopDeviceInfo {
device_name: string;
os: string;
cpu_arch: string;
}
const desktopBridge = {
app: {
ping: () => ipcRenderer.invoke("desktop:ping") as Promise<string>,
deviceInfo: () => ipcRenderer.invoke("desktop:device-info") as Promise<DesktopDeviceInfo>,
runtimeSnapshot: () =>
ipcRenderer.invoke("desktop:runtime-snapshot") as Promise<DesktopRuntimeSnapshot>,
bindPublishAccount: (platformId: string) =>
ipcRenderer.invoke("desktop:bind-publish-account", platformId) as Promise<DesktopAccountInfo>,
refreshRuntimeAccounts: () =>
ipcRenderer.invoke("desktop:refresh-runtime-accounts") as Promise<null>,
openPublishAccountConsole: (account: {
id: string;
platform: string;
platformUid: string;
displayName: string;
}) =>
ipcRenderer.invoke("desktop:open-publish-account-console", account) as Promise<null>,
unbindPublishAccount: (accountId: string, syncVersion: number) =>
ipcRenderer.invoke("desktop:unbind-publish-account", accountId, syncVersion) as Promise<null>,
listPublishTasks: (params?: ListDesktopPublishTasksParams) =>
ipcRenderer.invoke("desktop:list-publish-tasks", params) as Promise<DesktopPublishTaskListResponse>,
retryPublishTask: (taskId: string) =>
ipcRenderer.invoke("desktop:retry-publish-task", taskId) as Promise<CreatePublishJobResponse>,
syncRuntimeSession: (session: DesktopRuntimeSessionSyncRequest | null) =>
ipcRenderer.invoke("desktop:runtime-session-sync", session) as Promise<null>,
releaseRuntimeSession: (revoke?: boolean) =>
ipcRenderer.invoke("desktop:runtime-session-release", revoke) as Promise<null>,
onRuntimeInvalidated: (
listener: (event: { reason: "account-health"; at: number }) => void,
) => {
const wrapped = (_event: IpcRendererEvent, payload: { reason: "account-health"; at: number }) => {
listener(payload);
};
ipcRenderer.on("desktop:runtime-invalidated", wrapped);
return () => {
ipcRenderer.off("desktop:runtime-invalidated", wrapped);
};
},
},
};
contextBridge.exposeInMainWorld("desktopBridge", desktopBridge);