Files
t3code/apps/desktop/src/effectRpcIpcPoc/example/main.ts
T
Julius Marminge e11dba8fe4 Consolidate Electron IPC POC protocol in shared contracts
- Move the POC RPC group and methods to `@t3tools/contracts`
- Inline the renderer client setup in the IPC test and remove obsolete example files
- Update the main window placeholder copy
2026-05-06 11:47:45 -07:00

97 lines
2.4 KiB
TypeScript

import * as Path from "node:path";
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import { Effect } from "effect";
import { app, BrowserWindow, ipcMain } from "electron";
import { makeElectronIpcMainPort } from "effect-electron-ipc/main";
import { runDesktopIpcPocRpcServer } from "./rpc-server.ts";
const isMac = process.platform === "darwin";
export const makeMainWindow = Effect.sync(() => {
const window = new BrowserWindow({
width: 900,
height: 650,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
preload: Path.join(__dirname, "preload.cjs"),
},
});
void window.loadURL(
`data:text/html;charset=utf-8,${encodeURIComponent(`
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Effect RPC Electron IPC POC</title>
</head>
<body>
<main id="root">Renderer bundle would mount apps/web/src/examples/effectElectronIpcPoc.tsx here.</main>
</body>
</html>
`)}`,
);
return window;
});
export const installElectronLifecycleHandlers = Effect.acquireRelease(
Effect.sync(() => {
const onWindowAllClosed = () => {
if (!isMac) {
app.quit();
}
};
const onActivate = () => {
if (BrowserWindow.getAllWindows().length === 0) {
Effect.runFork(makeMainWindow);
}
};
app.on("window-all-closed", onWindowAllClosed);
app.on("activate", onActivate);
return {
onActivate,
onWindowAllClosed,
};
}),
({ onActivate, onWindowAllClosed }) =>
Effect.sync(() => {
app.off("activate", onActivate);
app.off("window-all-closed", onWindowAllClosed);
}),
);
export const waitForElectronAppReady = Effect.promise(() => app.whenReady());
export const waitForElectronAppQuit = Effect.callback<void>((resume) => {
const onBeforeQuit = () => {
resume(Effect.void);
};
app.once("before-quit", onBeforeQuit);
return Effect.sync(() => {
app.off("before-quit", onBeforeQuit);
});
});
export const main = Effect.gen(function* () {
yield* waitForElectronAppReady;
yield* installElectronLifecycleHandlers;
yield* runDesktopIpcPocRpcServer({
port: makeElectronIpcMainPort(ipcMain),
appVersion: app.getVersion(),
platform: process.platform,
});
yield* makeMainWindow;
yield* waitForElectronAppQuit;
}).pipe(Effect.scoped);
NodeRuntime.runMain(main);