Refactor IPC PoC into reusable example modules

- Move the core IPC protocol and transport helpers into a library layout
- Add Electron and browser example entrypoints for the RPC PoC
- Update the proof-of-concept test to exercise the end-to-end example
This commit is contained in:
Julius Marminge committed 2026-05-06 10:30:39 -07:00
1 parent 1a3dec46ba
commit 439836f6b9
10 files changed
+198 -95

No files matched your search

+33 -53
View File
@@ -1,58 +1,38 @@
import { Effect, Stream } from "effect";
import { RpcClient, RpcServer } from "effect/unstable/rpc";
import { describe, expect, it } from "vitest";
import {
DESKTOP_IPC_POC_METHODS,
DesktopIpcPocRpcGroup,
makeDesktopIpcPocClient,
makeDesktopIpcPocHandlersLayer,
} from "./effectRpcIpcPoc/protocol.ts";
import {
getEffectRpcIpcRendererBridge,
makeEffectRpcIpcRendererPort,
makeEffectRpcIpcRendererProtocol,
} from "./effectRpcIpcPoc/client.ts";
import { makeEffectRpcIpcMainProtocol } from "./effectRpcIpcPoc/main.ts";
import { EFFECT_RPC_IPC_RENDERER_BRIDGE_KEY } from "./effectRpcIpcPoc/ipc.ts";
loadDesktopIpcPocSnapshot,
makeDesktopIpcPocBrowserClient,
} from "./effectRpcIpcPoc/example/browser-client.ts";
import { runDesktopIpcPocRpcServer } from "./effectRpcIpcPoc/example/main.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 {
EffectRpcIpcMainFrame,
EffectRpcIpcMainSource,
EffectRpcIpcRendererFrame,
} from "./effectRpcIpcPoc/ipc.ts";
} from "./effectRpcIpcPoc/library/ipc.ts";
describe("effect RPC over Electron IPC proof of concept", () => {
it("round-trips unary requests through the renderer/main protocol pair", async () => {
it("runs the end-to-end consumer example over the Electron IPC transport", async () => {
const ipc = new InMemoryEffectRpcIpc();
const result = await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const mainProtocol = yield* makeEffectRpcIpcMainProtocol(ipc.mainPort);
yield* runDesktopIpcPocRpcServer({
port: ipc.mainPort,
appVersion: "1.2.3",
platform: "test-os",
now: () => new Date("2026-05-06T12:00:00.000Z"),
});
yield* RpcServer.make(DesktopIpcPocRpcGroup).pipe(
Effect.provideService(RpcServer.Protocol, mainProtocol),
Effect.provide(
makeDesktopIpcPocHandlersLayer({
appVersion: "1.2.3",
platform: "test-os",
now: () => new Date("2026-05-06T12:00:00.000Z"),
}),
),
Effect.forkScoped,
);
const rendererProtocol = yield* makeEffectRpcIpcRendererProtocol(
makeEffectRpcIpcRendererPort(getEffectRpcIpcRendererBridge(ipc.rendererGlobal)),
);
const client = yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
const runtimeInfo = yield* client[DESKTOP_IPC_POC_METHODS.getRuntimeInfo]({});
const echo = yield* client[DESKTOP_IPC_POC_METHODS.echo]({ text: "hello ipc" });
return { runtimeInfo, echo };
return yield* loadDesktopIpcPocSnapshot({
globalObject: ipc.rendererGlobal,
echoText: "hello ipc",
ticks: 3,
});
}),
),
);
@@ -67,29 +47,29 @@ describe("effect RPC over Electron IPC proof of concept", () => {
text: "hello ipc",
echoedAt: "2026-05-06T12:00:00.000Z",
},
ticks: [
{ sequence: 1, label: "tick:1" },
{ sequence: 2, label: "tick:2" },
{ sequence: 3, label: "tick:3" },
],
});
});
it("keeps Effect RPC streaming semantics over the same IPC envelope", async () => {
it("lets browser code consume the generated Effect RPC client directly", async () => {
const ipc = new InMemoryEffectRpcIpc();
const ticks = await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const mainProtocol = yield* makeEffectRpcIpcMainProtocol(ipc.mainPort);
yield* runDesktopIpcPocRpcServer({
port: ipc.mainPort,
appVersion: "0.0.0-test",
platform: "test-os",
});
yield* RpcServer.make(DesktopIpcPocRpcGroup).pipe(
Effect.provideService(RpcServer.Protocol, mainProtocol),
Effect.provide(makeDesktopIpcPocHandlersLayer()),
Effect.forkScoped,
);
const rendererProtocol = yield* makeEffectRpcIpcRendererProtocol(
makeEffectRpcIpcRendererPort(getEffectRpcIpcRendererBridge(ipc.rendererGlobal)),
);
const client = yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
const client = yield* makeDesktopIpcPocBrowserClient({
globalObject: ipc.rendererGlobal,
});
return yield* client[DESKTOP_IPC_POC_METHODS.subscribeTicks]({ take: 3 }).pipe(
Stream.runCollect,
@@ -0,0 +1,64 @@
import { Effect, Scope, Stream } from "effect";
import { RpcClient } from "effect/unstable/rpc";
import {
getEffectRpcIpcRendererBridge,
makeEffectRpcIpcRendererPort,
makeEffectRpcIpcRendererProtocol,
type EffectRpcIpcBrowserGlobal,
} from "../library/client.ts";
import type { EffectRpcIpcRendererBridge } from "../library/ipc.ts";
import {
DESKTOP_IPC_POC_METHODS,
makeDesktopIpcPocClient,
type DesktopIpcPocClient,
} from "./protocol.ts";
export interface DesktopIpcPocBrowserClientOptions {
readonly bridge?: EffectRpcIpcRendererBridge;
readonly globalObject?: EffectRpcIpcBrowserGlobal;
}
export interface DesktopIpcPocSnapshotOptions extends DesktopIpcPocBrowserClientOptions {
readonly echoText?: string;
readonly ticks?: number;
}
export const makeDesktopIpcPocBrowserClient = (
options: DesktopIpcPocBrowserClientOptions = {},
): Effect.Effect<DesktopIpcPocClient, never, Scope.Scope> =>
Effect.gen(function* () {
const bridge = options.bridge ?? getEffectRpcIpcRendererBridge(options.globalObject);
const rendererProtocol = yield* makeEffectRpcIpcRendererProtocol(
makeEffectRpcIpcRendererPort(bridge),
);
return yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
});
export const loadDesktopIpcPocSnapshot = (options: DesktopIpcPocSnapshotOptions = {}) =>
Effect.gen(function* () {
const client = yield* makeDesktopIpcPocBrowserClient(options);
const runtimeInfo = yield* client[DESKTOP_IPC_POC_METHODS.getRuntimeInfo]({});
const echo = yield* client[DESKTOP_IPC_POC_METHODS.echo]({
text: options.echoText ?? "hello from the renderer",
});
const ticks = yield* client[DESKTOP_IPC_POC_METHODS.subscribeTicks]({
take: options.ticks ?? 3,
}).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
);
return {
runtimeInfo,
echo,
ticks,
};
});
export const loadDesktopIpcPocSnapshotFromBrowser = (
options: Omit<DesktopIpcPocSnapshotOptions, "bridge" | "globalObject"> = {},
) => Effect.runPromise(Effect.scoped(loadDesktopIpcPocSnapshot(options)));
@@ -0,0 +1,22 @@
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;
};
@@ -0,0 +1,8 @@
import { contextBridge, ipcRenderer } from "electron";
import { exposeEffectRpcIpcPreloadBridge } from "../library/preload.ts";
exposeEffectRpcIpcPreloadBridge({
contextBridge,
ipcRenderer,
});
@@ -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,
);
});
@@ -1,4 +1,4 @@
import { Effect, Schema, Stream } from "effect";
import { Effect, Schema } from "effect";
import { RpcClient } from "effect/unstable/rpc";
import * as Rpc from "effect/unstable/rpc/Rpc";
import * as RpcGroup from "effect/unstable/rpc/RpcGroup";
@@ -60,37 +60,6 @@ export const DesktopIpcPocRpcGroup = RpcGroup.make(
DesktopIpcPocSubscribeTicksRpc,
);
export const makeDesktopIpcPocHandlersLayer = (options?: {
readonly appVersion?: string;
readonly platform?: string;
readonly now?: () => Date;
}) => {
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 makeDesktopIpcPocClient = RpcClient.make(DesktopIpcPocRpcGroup);
type DesktopIpcPocClientFactory = typeof makeDesktopIpcPocClient;
export type DesktopIpcPocClient =
@@ -1,5 +1,3 @@
import { contextBridge, ipcRenderer, type IpcRendererEvent } from "electron";
import {
EFFECT_RPC_IPC_CHANNELS,
EFFECT_RPC_IPC_RENDERER_BRIDGE_KEY,
@@ -13,18 +11,22 @@ export interface ElectronLikeIpcRenderer {
readonly send: (channel: string, frame: EffectRpcIpcRendererFrame) => void;
readonly on: (
channel: string,
listener: (event: IpcRendererEvent, frame: unknown) => void,
listener: (event: unknown, frame: unknown) => void,
) => ElectronLikeIpcRenderer;
readonly off?: (
channel: string,
listener: (event: IpcRendererEvent, frame: unknown) => void,
listener: (event: unknown, frame: unknown) => void,
) => ElectronLikeIpcRenderer;
readonly removeListener?: (
channel: string,
listener: (event: IpcRendererEvent, frame: unknown) => void,
listener: (event: unknown, frame: unknown) => void,
) => ElectronLikeIpcRenderer;
}
export interface ElectronLikeContextBridge {
readonly exposeInMainWorld: (apiKey: string, api: EffectRpcIpcRendererBridge) => void;
}
export function makeEffectRpcIpcPreloadBridge(
electronIpcRenderer: ElectronLikeIpcRenderer,
channels = EFFECT_RPC_IPC_CHANNELS,
@@ -37,7 +39,7 @@ export function makeEffectRpcIpcPreloadBridge(
electronIpcRenderer.send(channels.rendererToMain, frame);
},
subscribe: (listener) => {
const wrapped = (_event: IpcRendererEvent, frame: unknown) => {
const wrapped = (_event: unknown, frame: unknown) => {
if (isEffectRpcIpcMainFrame(frame)) {
listener(frame);
}
@@ -51,10 +53,17 @@ export function makeEffectRpcIpcPreloadBridge(
};
}
contextBridge.exposeInMainWorld(
EFFECT_RPC_IPC_RENDERER_BRIDGE_KEY,
makeEffectRpcIpcPreloadBridge(ipcRenderer),
);
export function exposeEffectRpcIpcPreloadBridge(options: {
readonly contextBridge: ElectronLikeContextBridge;
readonly ipcRenderer: ElectronLikeIpcRenderer;
readonly globalKey?: string;
readonly channels?: typeof EFFECT_RPC_IPC_CHANNELS;
}): void {
options.contextBridge.exposeInMainWorld(
options.globalKey ?? EFFECT_RPC_IPC_RENDERER_BRIDGE_KEY,
makeEffectRpcIpcPreloadBridge(options.ipcRenderer, options.channels),
);
}
function removeIpcListener<TListener>(
target: {