Refactor Electron IPC POC to use global bridge

- Load the renderer bridge from `globalThis` instead of passing it through helpers
- Simplify the snapshot/client APIs and update the test harness accordingly
- Align the web example atoms with the new effect-based client wiring
This commit is contained in:
Julius Marminge committed 2026-05-06 11:14:11 -07:00
1 parent 0a64216c68
commit 28c62e101b
4 files changed
+109 -122

No files matched your search

+39 -16
View File
@@ -11,6 +11,7 @@ import { EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY } from "effect-electron-ipc/ipc
import type {
EffectElectronIpcMainFrame,
EffectElectronIpcMainSource,
EffectElectronIpcRendererBridge,
EffectElectronIpcRendererFrame,
} from "effect-electron-ipc/ipc";
@@ -28,11 +29,10 @@ describe("effect RPC over Electron IPC proof of concept", () => {
now: () => new Date("2026-05-06T12:00:00.000Z"),
});
return yield* loadDesktopIpcPocSnapshot({
globalObject: ipc.rendererGlobal,
echoText: "hello ipc",
ticks: 3,
});
return yield* withEffectElectronIpcRendererBridge(
ipc.rendererPort,
loadDesktopIpcPocSnapshot,
);
}),
),
);
@@ -44,7 +44,7 @@ describe("effect RPC over Electron IPC proof of concept", () => {
ipcTransport: "electron-ipc",
},
echo: {
text: "hello ipc",
text: "hello from the renderer",
echoedAt: "2026-05-06T12:00:00.000Z",
},
ticks: [
@@ -67,13 +67,16 @@ describe("effect RPC over Electron IPC proof of concept", () => {
platform: "test-os",
});
const client = yield* makeDesktopIpcPocBrowserClient({
globalObject: ipc.rendererGlobal,
});
return yield* withEffectElectronIpcRendererBridge(
ipc.rendererPort,
Effect.gen(function* () {
const client = yield* makeDesktopIpcPocBrowserClient;
return yield* client[DESKTOP_IPC_POC_METHODS.subscribeTicks]({ take: 3 }).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
return yield* client[DESKTOP_IPC_POC_METHODS.subscribeTicks]({ take: 3 }).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
);
}),
);
}),
),
@@ -143,10 +146,6 @@ class InMemoryEffectElectronIpc {
},
};
readonly rendererGlobal = {
[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY]: this.rendererPort,
};
close(): void {
this.closed = true;
for (const listener of this.closeListeners) {
@@ -154,3 +153,27 @@ class InMemoryEffectElectronIpc {
}
}
}
const withEffectElectronIpcRendererBridge = <A, E, R>(
bridge: EffectElectronIpcRendererBridge,
effect: Effect.Effect<A, E, R>,
): Effect.Effect<A, E, R> =>
Effect.acquireUseRelease(
Effect.sync(() => {
const globalObject = globalThis as Partial<
Record<typeof EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY, EffectElectronIpcRendererBridge>
>;
const previousBridge = globalObject[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY];
globalObject[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY] = bridge;
return () => {
if (previousBridge !== undefined) {
globalObject[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY] = previousBridge;
} else {
delete globalObject[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY];
}
};
}),
() => effect,
(restore) => Effect.sync(restore),
);
@@ -6,9 +6,7 @@ import {
getEffectElectronIpcRendererBridge,
makeEffectElectronIpcRendererPort,
makeEffectElectronIpcRendererProtocol,
type EffectElectronIpcBrowserGlobal,
} from "effect-electron-ipc/client";
import type { EffectElectronIpcRendererBridge } from "effect-electron-ipc/ipc";
import {
DESKTOP_IPC_POC_METHODS,
makeDesktopIpcPocClient,
@@ -24,52 +22,40 @@ export interface DesktopIpcPocSnapshot {
readonly ticks: ReadonlyArray<DesktopIpcPocTick>;
}
export interface DesktopIpcPocBrowserClientOptions {
readonly bridge?: EffectElectronIpcRendererBridge;
readonly globalObject?: EffectElectronIpcBrowserGlobal;
}
export const makeDesktopIpcPocBrowserClient: Effect.Effect<
DesktopIpcPocClient,
never,
Scope.Scope
> = Effect.gen(function* () {
const bridge = yield* Effect.sync(() => getEffectElectronIpcRendererBridge());
const rendererPort = makeEffectElectronIpcRendererPort(bridge);
const rendererProtocol = yield* makeEffectElectronIpcRendererProtocol(rendererPort);
export interface DesktopIpcPocSnapshotOptions extends DesktopIpcPocBrowserClientOptions {
readonly echoText?: string;
readonly ticks?: number;
}
return yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
});
export const makeDesktopIpcPocBrowserClient = (
options: DesktopIpcPocBrowserClientOptions = {},
): Effect.Effect<DesktopIpcPocClient, never, Scope.Scope> =>
Effect.gen(function* () {
const bridge = options.bridge ?? getEffectElectronIpcRendererBridge(options.globalObject);
const rendererPort = makeEffectElectronIpcRendererPort(bridge);
const rendererProtocol = yield* makeEffectElectronIpcRendererProtocol(rendererPort);
return yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
export const loadDesktopIpcPocSnapshot: Effect.Effect<
DesktopIpcPocSnapshot,
RpcClientError,
Scope.Scope
> = Effect.gen(function* () {
const client = yield* makeDesktopIpcPocBrowserClient;
const runtimeInfo = yield* client[DESKTOP_IPC_POC_METHODS.getRuntimeInfo]({});
const echo = yield* client[DESKTOP_IPC_POC_METHODS.echo]({
text: "hello from the renderer",
});
const ticks = yield* client[DESKTOP_IPC_POC_METHODS.subscribeTicks]({
take: 3,
}).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
);
export const loadDesktopIpcPocSnapshot = (
options: DesktopIpcPocSnapshotOptions = {},
): Effect.Effect<DesktopIpcPocSnapshot, RpcClientError, Scope.Scope> =>
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)));
return {
runtimeInfo,
echo,
ticks,
};
});
@@ -3,10 +3,7 @@ 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 snapshot = yield* loadDesktopIpcPocSnapshot;
const root = document.querySelector("#root");
if (root) {
+36 -55
View File
@@ -14,9 +14,7 @@ import {
getEffectElectronIpcRendererBridge,
makeEffectElectronIpcRendererPort,
makeEffectElectronIpcRendererProtocol,
type EffectElectronIpcBrowserGlobal,
} from "effect-electron-ipc/client";
import type { EffectElectronIpcRendererBridge } from "effect-electron-ipc/ipc";
import { createRoot } from "react-dom/client";
import type { ReactElement } from "react";
@@ -64,55 +62,43 @@ export interface DesktopIpcPocSnapshot {
readonly ticks: ReadonlyArray<DesktopIpcPocTick>;
}
export interface DesktopIpcPocBrowserClientOptions {
readonly bridge?: EffectElectronIpcRendererBridge;
readonly globalObject?: EffectElectronIpcBrowserGlobal;
}
export const makeDesktopIpcPocBrowserClient: Effect.Effect<
DesktopIpcPocClient,
never,
Scope.Scope
> = Effect.gen(function* () {
const bridge = yield* Effect.sync(() => getEffectElectronIpcRendererBridge());
const rendererPort = makeEffectElectronIpcRendererPort(bridge);
const rendererProtocol = yield* makeEffectElectronIpcRendererProtocol(rendererPort);
export interface DesktopIpcPocSnapshotOptions extends DesktopIpcPocBrowserClientOptions {
readonly echoText?: string;
readonly ticks?: number;
}
return yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
});
export const makeDesktopIpcPocBrowserClient = (
options: DesktopIpcPocBrowserClientOptions = {},
): Effect.Effect<DesktopIpcPocClient, never, Scope.Scope> =>
Effect.gen(function* () {
const bridge = options.bridge ?? getEffectElectronIpcRendererBridge(options.globalObject);
const rendererPort = makeEffectElectronIpcRendererPort(bridge);
const rendererProtocol = yield* makeEffectElectronIpcRendererProtocol(rendererPort);
return yield* makeDesktopIpcPocClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
);
export const loadDesktopIpcPocSnapshot: Effect.Effect<
DesktopIpcPocSnapshot,
RpcClientError,
Scope.Scope
> = Effect.gen(function* () {
const client = yield* makeDesktopIpcPocBrowserClient;
const runtimeInfo = yield* client[DESKTOP_IPC_POC_METHODS.getRuntimeInfo]({});
const echo = yield* client[DESKTOP_IPC_POC_METHODS.echo]({
text: "hello from the renderer",
});
const ticks = yield* client[DESKTOP_IPC_POC_METHODS.subscribeTicks]({
take: 3,
}).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
);
export const loadDesktopIpcPocSnapshot = (
options: DesktopIpcPocSnapshotOptions = {},
): Effect.Effect<DesktopIpcPocSnapshot, RpcClientError, Scope.Scope> =>
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)));
return {
runtimeInfo,
echo,
ticks,
};
});
// -----------------------------------------------------------------------------
// example/browser-atoms.ts
@@ -121,17 +107,12 @@ export const loadDesktopIpcPocSnapshotFromBrowser = (
const DESKTOP_IPC_POC_SNAPSHOT_STALE_TIME_MS = 5_000;
const DESKTOP_IPC_POC_SNAPSHOT_IDLE_TTL_MS = 60_000;
export const desktopIpcPocClientAtom = Atom.make(makeDesktopIpcPocBrowserClient()).pipe(
export const desktopIpcPocClientAtom = Atom.make(makeDesktopIpcPocBrowserClient).pipe(
Atom.keepAlive,
Atom.withLabel("desktop-ipc-poc:effect-rpc-client"),
);
export const desktopIpcPocSnapshotAtom = Atom.make(
loadDesktopIpcPocSnapshot({
echoText: "hello from an Effect Atom",
ticks: 5,
}),
).pipe(
export const desktopIpcPocSnapshotAtom = Atom.make(loadDesktopIpcPocSnapshot).pipe(
Atom.swr({
staleTime: DESKTOP_IPC_POC_SNAPSHOT_STALE_TIME_MS,
revalidateOnMount: true,
@@ -142,7 +123,7 @@ export const desktopIpcPocSnapshotAtom = Atom.make(
export const desktopIpcPocManualEchoAtom = Atom.make(
Effect.gen(function* () {
const client = yield* makeDesktopIpcPocBrowserClient();
const client = yield* makeDesktopIpcPocBrowserClient;
return yield* client[DESKTOP_IPC_POC_METHODS.echo]({
text: "manual echo from an Atom-backed action",
});