Refactor Electron IPC POC into main, preload, and renderer
- Split the Electron example into dedicated main, preload, renderer, and RPC server modules - Update the POC to create a real BrowserWindow and render the RPC snapshot
This commit is contained in:
1 parent
439836f6b9
commit
d15de553c2
6 files changed
+165
-69
No files matched your search
@@ -5,7 +5,7 @@ import {
|
||||
loadDesktopIpcPocSnapshot,
|
||||
makeDesktopIpcPocBrowserClient,
|
||||
} from "./effectRpcIpcPoc/example/browser-client.ts";
|
||||
import { runDesktopIpcPocRpcServer } from "./effectRpcIpcPoc/example/main.ts";
|
||||
import { runDesktopIpcPocRpcServer } from "./effectRpcIpcPoc/example/rpc-server.ts";
|
||||
import { DESKTOP_IPC_POC_METHODS } from "./effectRpcIpcPoc/example/protocol.ts";
|
||||
import { EFFECT_RPC_IPC_RENDERER_BRIDGE_KEY } from "./effectRpcIpcPoc/library/ipc.ts";
|
||||
import type {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import { Effect, Fiber } from "effect";
|
||||
import { app, ipcMain } from "electron";
|
||||
|
||||
import { makeElectronIpcMainPort } from "../library/main.ts";
|
||||
import { runDesktopIpcPocRpcServer } from "./main.ts";
|
||||
|
||||
export const runDesktopIpcPocElectronMainRpcServer = () =>
|
||||
runDesktopIpcPocRpcServer({
|
||||
port: makeElectronIpcMainPort(ipcMain),
|
||||
appVersion: app.getVersion(),
|
||||
platform: process.platform,
|
||||
});
|
||||
|
||||
export const startDesktopIpcPocElectronMainRpcServer = () => {
|
||||
const fiber = Effect.runFork(Effect.scoped(runDesktopIpcPocElectronMainRpcServer()));
|
||||
|
||||
app.once("before-quit", () => {
|
||||
Effect.runFork(Fiber.interrupt(fiber));
|
||||
});
|
||||
|
||||
return fiber;
|
||||
};
|
||||
@@ -1,51 +1,96 @@
|
||||
import { Effect, Stream } from "effect";
|
||||
import { RpcServer } from "effect/unstable/rpc";
|
||||
import * as Path from "node:path";
|
||||
|
||||
import { makeEffectRpcIpcMainProtocol } from "../library/main.ts";
|
||||
import type { EffectRpcIpcMainPort } from "../library/ipc.ts";
|
||||
import { DESKTOP_IPC_POC_METHODS, DesktopIpcPocRpcGroup } from "./protocol.ts";
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
import { Effect } from "effect";
|
||||
import { app, BrowserWindow, ipcMain } from "electron";
|
||||
|
||||
export interface DesktopIpcPocMainOptions {
|
||||
readonly port: EffectRpcIpcMainPort;
|
||||
readonly appVersion?: string;
|
||||
readonly platform?: string;
|
||||
readonly now?: () => Date;
|
||||
}
|
||||
import { makeElectronIpcMainPort } from "../library/main.ts";
|
||||
import { runDesktopIpcPocRpcServer } from "./rpc-server.ts";
|
||||
|
||||
export const makeDesktopIpcPocHandlersLayer = (options: DesktopIpcPocMainOptions) => {
|
||||
const now = options.now ?? (() => new Date());
|
||||
const isMac = process.platform === "darwin";
|
||||
|
||||
return DesktopIpcPocRpcGroup.toLayer(
|
||||
DesktopIpcPocRpcGroup.of({
|
||||
[DESKTOP_IPC_POC_METHODS.getRuntimeInfo]: () =>
|
||||
Effect.succeed({
|
||||
appVersion: options.appVersion ?? "0.0.0-poc",
|
||||
platform: options.platform ?? process.platform,
|
||||
ipcTransport: "electron-ipc" as const,
|
||||
}),
|
||||
[DESKTOP_IPC_POC_METHODS.echo]: (input) =>
|
||||
Effect.sync(() => ({
|
||||
text: input.text,
|
||||
echoedAt: now().toISOString(),
|
||||
})),
|
||||
[DESKTOP_IPC_POC_METHODS.subscribeTicks]: (input) =>
|
||||
Stream.fromIterable(
|
||||
Array.from({ length: Math.max(0, Math.floor(input.take)) }, (_, index) => ({
|
||||
sequence: index + 1,
|
||||
label: `tick:${index + 1}`,
|
||||
})),
|
||||
),
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
export const runDesktopIpcPocRpcServer = (options: DesktopIpcPocMainOptions) =>
|
||||
Effect.gen(function* () {
|
||||
const mainProtocol = yield* makeEffectRpcIpcMainProtocol(options.port);
|
||||
|
||||
yield* RpcServer.make(DesktopIpcPocRpcGroup).pipe(
|
||||
Effect.provideService(RpcServer.Protocol, mainProtocol),
|
||||
Effect.provide(makeDesktopIpcPocHandlersLayer(options)),
|
||||
Effect.forkScoped,
|
||||
);
|
||||
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 call example/renderer.ts 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);
|
||||
File renamed without changes.
@@ -0,0 +1,22 @@
|
||||
import { Effect } from "effect";
|
||||
|
||||
import { loadDesktopIpcPocSnapshot } from "./browser-client.ts";
|
||||
|
||||
const program = Effect.gen(function* () {
|
||||
const snapshot = yield* loadDesktopIpcPocSnapshot({
|
||||
echoText: "hello from the renderer",
|
||||
ticks: 5,
|
||||
});
|
||||
|
||||
const root = document.querySelector("#root");
|
||||
if (root) {
|
||||
root.textContent = JSON.stringify(snapshot, null, 2);
|
||||
}
|
||||
}).pipe(Effect.scoped);
|
||||
|
||||
Effect.runPromise(program).catch((error: unknown) => {
|
||||
const root = document.querySelector("#root");
|
||||
if (root) {
|
||||
root.textContent = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Effect, Stream } from "effect";
|
||||
import { RpcServer } from "effect/unstable/rpc";
|
||||
|
||||
import { makeEffectRpcIpcMainProtocol } from "../library/main.ts";
|
||||
import type { EffectRpcIpcMainPort } from "../library/ipc.ts";
|
||||
import { DESKTOP_IPC_POC_METHODS, DesktopIpcPocRpcGroup } from "./protocol.ts";
|
||||
|
||||
export interface DesktopIpcPocMainOptions {
|
||||
readonly port: EffectRpcIpcMainPort;
|
||||
readonly appVersion?: string;
|
||||
readonly platform?: string;
|
||||
readonly now?: () => Date;
|
||||
}
|
||||
|
||||
export const makeDesktopIpcPocHandlersLayer = (options: DesktopIpcPocMainOptions) => {
|
||||
const now = options.now ?? (() => new Date());
|
||||
|
||||
return DesktopIpcPocRpcGroup.toLayer(
|
||||
DesktopIpcPocRpcGroup.of({
|
||||
[DESKTOP_IPC_POC_METHODS.getRuntimeInfo]: () =>
|
||||
Effect.succeed({
|
||||
appVersion: options.appVersion ?? "0.0.0-poc",
|
||||
platform: options.platform ?? process.platform,
|
||||
ipcTransport: "electron-ipc" as const,
|
||||
}),
|
||||
[DESKTOP_IPC_POC_METHODS.echo]: (input) =>
|
||||
Effect.sync(() => ({
|
||||
text: input.text,
|
||||
echoedAt: now().toISOString(),
|
||||
})),
|
||||
[DESKTOP_IPC_POC_METHODS.subscribeTicks]: (input) =>
|
||||
Stream.fromIterable(
|
||||
Array.from({ length: Math.max(0, Math.floor(input.take)) }, (_, index) => ({
|
||||
sequence: index + 1,
|
||||
label: `tick:${index + 1}`,
|
||||
})),
|
||||
),
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
export const runDesktopIpcPocRpcServer = (options: DesktopIpcPocMainOptions) =>
|
||||
Effect.gen(function* () {
|
||||
const mainProtocol = yield* makeEffectRpcIpcMainProtocol(options.port);
|
||||
|
||||
yield* RpcServer.make(DesktopIpcPocRpcGroup).pipe(
|
||||
Effect.provideService(RpcServer.Protocol, mainProtocol),
|
||||
Effect.provide(makeDesktopIpcPocHandlersLayer(options)),
|
||||
Effect.forkScoped,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user