Files
Folo/packages/shared/src/bridge.ts
T
Innei 234471dfdf refactor: move to monorepo (#475)
* init

* fix: electron

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

* fix: build

Signed-off-by: Innei <i@innei.in>

* fix: lint

Signed-off-by: Innei <i@innei.in>

* fix: test

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
2024-09-18 20:54:48 +08:00

41 lines
1.2 KiB
TypeScript

import type { BrowserWindow } from "electron"
import type { toast } from "sonner"
import type { GeneralSettings, UISettings } from "./interface/settings"
const PREFIX = "__follow"
interface RenderGlobalContext {
showSetting: (path?: string) => void
getGeneralSettings: () => GeneralSettings
getUISettings: () => UISettings
electronClose: () => void
electronShow: () => void
toast: typeof toast
}
export const registerGlobalContext = (context: RenderGlobalContext) => {
globalThis[PREFIX] = context
}
export function callGlobalContextMethod<T extends keyof RenderGlobalContext>(
window: BrowserWindow,
method: T,
// @ts-expect-error
args: Parameters<RenderGlobalContext[T]> = [] as any,
): Promise<ReturnType<RenderGlobalContext[T]>>
export function callGlobalContextMethod(window: BrowserWindow, method: string, args?: any[]): void
export function callGlobalContextMethod<T extends keyof RenderGlobalContext>(
window: BrowserWindow,
method: T,
args: Parameters<RenderGlobalContext[T]> = [] as any,
) {
return window.webContents.executeJavaScript(
`globalThis.${PREFIX}.${method}(${args.map((arg) => JSON.stringify(arg)).join(",")})`,
)
}