Rename Effect Electron RPC package to IPC

- Rename the workspace package and update all imports
- Update the desktop IPC proof-of-concept to use the new transport name
This commit is contained in:
Julius Marminge committed 2026-05-06 10:56:53 -07:00
1 parent a7164ccb69
commit 0eec82b655
14 files changed
+337 -131

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@
"dependencies": {
"@effect/platform-node": "catalog:",
"effect": "catalog:",
"effect-electron-rpc": "workspace:*",
"effect-electron-ipc": "workspace:*",
"electron": "40.9.3",
"electron-updater": "^6.6.2"
},
+16 -16
View File
@@ -7,16 +7,16 @@ import {
} from "./effectRpcIpcPoc/example/browser-client.ts";
import { runDesktopIpcPocRpcServer } from "./effectRpcIpcPoc/example/rpc-server.ts";
import { DESKTOP_IPC_POC_METHODS } from "./effectRpcIpcPoc/example/protocol.ts";
import { EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY } from "effect-electron-rpc/ipc";
import { EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY } from "effect-electron-ipc/ipc";
import type {
EffectElectronRpcMainFrame,
EffectElectronRpcMainSource,
EffectElectronRpcRendererFrame,
} from "effect-electron-rpc/ipc";
EffectElectronIpcMainFrame,
EffectElectronIpcMainSource,
EffectElectronIpcRendererFrame,
} from "effect-electron-ipc/ipc";
describe("effect RPC over Electron IPC proof of concept", () => {
it("runs the end-to-end consumer example over the Electron IPC transport", async () => {
const ipc = new InMemoryEffectElectronRpc();
const ipc = new InMemoryEffectElectronIpc();
const result = await Effect.runPromise(
Effect.scoped(
@@ -56,7 +56,7 @@ describe("effect RPC over Electron IPC proof of concept", () => {
});
it("lets browser code consume the generated Effect RPC client directly", async () => {
const ipc = new InMemoryEffectElectronRpc();
const ipc = new InMemoryEffectElectronIpc();
const ticks = await Effect.runPromise(
Effect.scoped(
@@ -87,15 +87,15 @@ describe("effect RPC over Electron IPC proof of concept", () => {
});
});
class InMemoryEffectElectronRpc {
class InMemoryEffectElectronIpc {
private readonly mainListeners = new Set<
(source: EffectElectronRpcMainSource, frame: EffectElectronRpcRendererFrame) => void
(source: EffectElectronIpcMainSource, frame: EffectElectronIpcRendererFrame) => void
>();
private readonly rendererListeners = new Set<(frame: EffectElectronRpcMainFrame) => void>();
private readonly rendererListeners = new Set<(frame: EffectElectronIpcMainFrame) => void>();
private readonly closeListeners = new Set<() => void>();
private closed = false;
readonly source: EffectElectronRpcMainSource = {
readonly source: EffectElectronIpcMainSource = {
id: 1,
send: (frame) => {
queueMicrotask(() => {
@@ -116,8 +116,8 @@ class InMemoryEffectElectronRpc {
readonly mainPort = {
subscribe: (
listener: (
source: EffectElectronRpcMainSource,
frame: EffectElectronRpcRendererFrame,
source: EffectElectronIpcMainSource,
frame: EffectElectronIpcRendererFrame,
) => void,
) => {
this.mainListeners.add(listener);
@@ -128,14 +128,14 @@ class InMemoryEffectElectronRpc {
};
readonly rendererPort = {
send: (frame: EffectElectronRpcRendererFrame) => {
send: (frame: EffectElectronIpcRendererFrame) => {
queueMicrotask(() => {
for (const listener of this.mainListeners) {
listener(this.source, frame);
}
});
},
subscribe: (listener: (frame: EffectElectronRpcMainFrame) => void) => {
subscribe: (listener: (frame: EffectElectronIpcMainFrame) => void) => {
this.rendererListeners.add(listener);
return () => {
this.rendererListeners.delete(listener);
@@ -144,7 +144,7 @@ class InMemoryEffectElectronRpc {
};
readonly rendererGlobal = {
[EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY]: this.rendererPort,
[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY]: this.rendererPort,
};
close(): void {
@@ -1,22 +1,88 @@
import { Effect, Scope, Stream } from "effect";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
import { Cause, Effect, Option, Scope, Stream } from "effect";
import { RpcClient } from "effect/unstable/rpc";
import type { RpcClientError } from "effect/unstable/rpc/RpcClientError";
import {
getEffectElectronRpcRendererBridge,
makeEffectElectronRpcRendererPort,
makeEffectElectronRpcRendererProtocol,
type EffectElectronRpcBrowserGlobal,
} from "effect-electron-rpc/client";
import type { EffectElectronRpcRendererBridge } from "effect-electron-rpc/ipc";
getEffectElectronIpcRendererBridge,
makeEffectElectronIpcRendererPort,
makeEffectElectronIpcRendererProtocol,
type EffectElectronIpcBrowserGlobal,
} from "effect-electron-ipc/client";
import type { EffectElectronIpcRendererBridge } from "effect-electron-ipc/ipc";
import {
DESKTOP_IPC_POC_METHODS,
makeDesktopIpcPocClient,
type DesktopIpcPocClient,
type DesktopIpcPocEchoResult,
type DesktopIpcPocRuntimeInfo,
type DesktopIpcPocTick,
} from "./protocol.ts";
// -----------------------------------------------------------------------------
// example/preload.ts
// -----------------------------------------------------------------------------
// The real preload file is intentionally tiny:
//
// import { contextBridge, ipcRenderer } from "electron";
// import { exposeEffectElectronIpcPreloadBridge } from "effect-electron-ipc/preload";
//
// exposeEffectElectronIpcPreloadBridge({ contextBridge, ipcRenderer });
//
// That installs `window.effectElectronIpc`, which this browser module reads below.
// -----------------------------------------------------------------------------
// example/browser-react-runtime.ts
// -----------------------------------------------------------------------------
// These declarations stand in for the actual React and @effect/atom-react imports
// a renderer bundle would use. The transport and Effect RPC client code below is
// real; only the UI runtime host is declared to keep this proof of concept small.
type ReactElement = unknown;
type ReactNode = unknown;
type ReactComponent<P extends object = Record<string, never>> = (props: P) => ReactElement;
type ReactElementType<P extends object = Record<string, never>> = string | ReactComponent<P>;
declare const React: {
readonly createElement: <P extends object>(
type: ReactElementType<P>,
props?: P | null,
...children: ReactNode[]
) => ReactElement;
};
declare const createRoot: (container: Element) => {
readonly render: (element: ReactElement) => void;
};
declare const useAtomRefresh: <A>(atom: Atom.Atom<A>) => () => void;
declare const useAtomValue: <A>(atom: Atom.Atom<A>) => A;
// -----------------------------------------------------------------------------
// example/protocol.ts
// -----------------------------------------------------------------------------
// The shared RPC contract lives in protocol.ts. Both the Electron main process
// and browser renderer import this contract, so the renderer gets a typed client
// without depending on Electron-specific implementation code.
export interface DesktopIpcPocSnapshot {
readonly runtimeInfo: DesktopIpcPocRuntimeInfo;
readonly echo: DesktopIpcPocEchoResult;
readonly ticks: ReadonlyArray<DesktopIpcPocTick>;
}
// -----------------------------------------------------------------------------
// example/browser-client.ts
// -----------------------------------------------------------------------------
// This is the important browser-side transport step:
//
// preload bridge -> Effect Electron IPC renderer port
// -> Effect RPC RpcClient.Protocol
// -> typed DesktopIpcPocClient
export interface DesktopIpcPocBrowserClientOptions {
readonly bridge?: EffectElectronRpcRendererBridge;
readonly globalObject?: EffectElectronRpcBrowserGlobal;
readonly bridge?: EffectElectronIpcRendererBridge;
readonly globalObject?: EffectElectronIpcBrowserGlobal;
}
export interface DesktopIpcPocSnapshotOptions extends DesktopIpcPocBrowserClientOptions {
@@ -28,17 +94,18 @@ export const makeDesktopIpcPocBrowserClient = (
options: DesktopIpcPocBrowserClientOptions = {},
): Effect.Effect<DesktopIpcPocClient, never, Scope.Scope> =>
Effect.gen(function* () {
const bridge = options.bridge ?? getEffectElectronRpcRendererBridge(options.globalObject);
const rendererProtocol = yield* makeEffectElectronRpcRendererProtocol(
makeEffectElectronRpcRendererPort(bridge),
);
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 = (options: DesktopIpcPocSnapshotOptions = {}) =>
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]({});
@@ -62,3 +129,142 @@ export const loadDesktopIpcPocSnapshot = (options: DesktopIpcPocSnapshotOptions
export const loadDesktopIpcPocSnapshotFromBrowser = (
options: Omit<DesktopIpcPocSnapshotOptions, "bridge" | "globalObject"> = {},
) => Effect.runPromise(Effect.scoped(loadDesktopIpcPocSnapshot(options)));
// -----------------------------------------------------------------------------
// example/browser-atoms.ts
// -----------------------------------------------------------------------------
// These are the Effect Atom values the React layer consumes. The labels and SWR
// annotations are the important bit for app ergonomics: the RPC client and RPC
// query have stable, inspectable identities and refresh behavior.
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(
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(
Atom.swr({
staleTime: DESKTOP_IPC_POC_SNAPSHOT_STALE_TIME_MS,
revalidateOnMount: true,
}),
Atom.setIdleTTL(DESKTOP_IPC_POC_SNAPSHOT_IDLE_TTL_MS),
Atom.withLabel("desktop-ipc-poc:snapshot"),
);
export const desktopIpcPocManualEchoAtom = Atom.make(
Effect.gen(function* () {
const client = yield* makeDesktopIpcPocBrowserClient();
return yield* client[DESKTOP_IPC_POC_METHODS.echo]({
text: "manual echo from an Atom-backed action",
});
}),
).pipe(Atom.withLabel("desktop-ipc-poc:manual-echo"));
// -----------------------------------------------------------------------------
// example/components/DesktopIpcPocPanel.tsx
// -----------------------------------------------------------------------------
// The React layer does not know about Electron IPC or RpcClient.Protocol. It only
// reads Atom values, renders AsyncResult states, and calls Atom refresh handlers.
function formatAsyncResultError(result: AsyncResult.AsyncResult<unknown, unknown>): string | null {
if (result._tag !== "Failure") {
return null;
}
const error = Cause.squash(result.cause);
return error instanceof Error ? error.message : String(error);
}
function DesktopIpcPocClientStatus(): ReactElement {
const clientResult = useAtomValue(desktopIpcPocClientAtom);
const isReady = clientResult._tag === "Success";
const label = isReady
? "Effect RPC client ready"
: clientResult.waiting
? "Connecting RPC client"
: "RPC client failed";
return React.createElement(
"span",
{
"data-state": isReady ? "ready" : clientResult._tag.toLowerCase(),
},
label,
);
}
function RuntimeInfoView(props: { readonly runtimeInfo: DesktopIpcPocRuntimeInfo }): ReactElement {
return React.createElement(
"dl",
{ "aria-label": "Runtime info" },
React.createElement("dt", null, "App version"),
React.createElement("dd", null, props.runtimeInfo.appVersion),
React.createElement("dt", null, "Platform"),
React.createElement("dd", null, props.runtimeInfo.platform),
React.createElement("dt", null, "Transport"),
React.createElement("dd", null, props.runtimeInfo.ipcTransport),
);
}
function EchoView(props: { readonly echo: DesktopIpcPocEchoResult }): ReactElement {
return React.createElement("p", null, `Echoed "${props.echo.text}" at ${props.echo.echoedAt}`);
}
function TickList(props: { readonly ticks: ReadonlyArray<DesktopIpcPocTick> }): ReactElement {
return React.createElement(
"ol",
{ "aria-label": "Streamed ticks" },
...props.ticks.map((tick) =>
React.createElement("li", { key: tick.sequence }, `${tick.sequence}: ${tick.label}`),
),
);
}
export function DesktopIpcPocPanel(): ReactElement {
const snapshotResult = useAtomValue(desktopIpcPocSnapshotAtom);
const refreshSnapshot = useAtomRefresh(desktopIpcPocSnapshotAtom);
const snapshot = Option.getOrNull(AsyncResult.value(snapshotResult));
const error = formatAsyncResultError(snapshotResult);
return React.createElement(
"section",
{ "aria-label": "Effect Electron IPC proof of concept" },
React.createElement("header", null, React.createElement(DesktopIpcPocClientStatus, null)),
React.createElement(
"button",
{
disabled: snapshotResult.waiting,
onClick: refreshSnapshot,
type: "button",
},
snapshotResult.waiting ? "Refreshing" : "Refresh",
),
error ? React.createElement("p", { role: "alert" }, error) : null,
snapshot
? React.createElement(
"div",
null,
React.createElement(RuntimeInfoView, { runtimeInfo: snapshot.runtimeInfo }),
React.createElement(EchoView, { echo: snapshot.echo }),
React.createElement(TickList, { ticks: snapshot.ticks }),
)
: React.createElement("p", null, "Loading desktop RPC data"),
);
}
// -----------------------------------------------------------------------------
// example/renderer.tsx
// -----------------------------------------------------------------------------
// The renderer entrypoint just mounts the React tree. The preload has already
// installed the bridge, and the Atom graph lazily creates the Effect RPC client.
export function mountDesktopIpcPocReactExample(container: Element): void {
createRoot(container).render(React.createElement(DesktopIpcPocPanel, null));
}
@@ -4,7 +4,7 @@ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import { Effect } from "effect";
import { app, BrowserWindow, ipcMain } from "electron";
import { makeElectronIpcMainPort } from "effect-electron-rpc/main";
import { makeElectronIpcMainPort } from "effect-electron-ipc/main";
import { runDesktopIpcPocRpcServer } from "./rpc-server.ts";
const isMac = process.platform === "darwin";
@@ -1,8 +1,8 @@
import { contextBridge, ipcRenderer } from "electron";
import { exposeEffectElectronRpcPreloadBridge } from "effect-electron-rpc/preload";
import { exposeEffectElectronIpcPreloadBridge } from "effect-electron-ipc/preload";
exposeEffectElectronRpcPreloadBridge({
exposeEffectElectronIpcPreloadBridge({
contextBridge,
ipcRenderer,
});
@@ -1,12 +1,12 @@
import { Effect, Stream } from "effect";
import { RpcServer } from "effect/unstable/rpc";
import type { EffectElectronRpcMainPort } from "effect-electron-rpc/ipc";
import { makeEffectElectronRpcMainProtocol } from "effect-electron-rpc/main";
import type { EffectElectronIpcMainPort } from "effect-electron-ipc/ipc";
import { makeEffectElectronIpcMainProtocol } from "effect-electron-ipc/main";
import { DESKTOP_IPC_POC_METHODS, DesktopIpcPocRpcGroup } from "./protocol.ts";
export interface DesktopIpcPocMainOptions {
readonly port: EffectElectronRpcMainPort;
readonly port: EffectElectronIpcMainPort;
readonly appVersion?: string;
readonly platform?: string;
readonly now?: () => Date;
@@ -41,7 +41,7 @@ export const makeDesktopIpcPocHandlersLayer = (options: DesktopIpcPocMainOptions
export const runDesktopIpcPocRpcServer = (options: DesktopIpcPocMainOptions) =>
Effect.gen(function* () {
const mainProtocol = yield* makeEffectElectronRpcMainProtocol(options.port);
const mainProtocol = yield* makeEffectElectronIpcMainProtocol(options.port);
yield* RpcServer.make(DesktopIpcPocRpcGroup).pipe(
Effect.provideService(RpcServer.Protocol, mainProtocol),
+4 -4
View File
@@ -20,7 +20,7 @@
"dependencies": {
"@effect/platform-node": "catalog:",
"effect": "catalog:",
"effect-electron-rpc": "workspace:*",
"effect-electron-ipc": "workspace:*",
"electron": "40.9.3",
"electron-updater": "^6.6.2",
},
@@ -191,8 +191,8 @@
"vitest": "catalog:",
},
},
"packages/effect-electron-rpc": {
"name": "effect-electron-rpc",
"packages/effect-electron-ipc": {
"name": "effect-electron-ipc",
"dependencies": {
"effect": "catalog:",
},
@@ -1163,7 +1163,7 @@
"effect-codex-app-server": ["effect-codex-app-server@workspace:packages/effect-codex-app-server"],
"effect-electron-rpc": ["effect-electron-rpc@workspace:packages/effect-electron-rpc"],
"effect-electron-ipc": ["effect-electron-ipc@workspace:packages/effect-electron-ipc"],
"electron": ["[email protected]", "", { "dependencies": { "@electron/get": "^2.0.0", "@types/node": "^24.9.0", "extract-zip": "^2.0.1" }, "bin": { "electron": "cli.js" } }, "sha512-rDcJOT6BBE689Ada+4jD3rVr05pMv9MZOgT0x/rIMVDF9c4ttx4RTb6lVARTyxZC7uqpirttCtcli1eg1DX5qg=="],
@@ -1,5 +1,5 @@
{
"name": "effect-electron-rpc",
"name": "effect-electron-ipc",
"private": true,
"type": "module",
"exports": {
@@ -5,37 +5,37 @@ import * as Scope from "effect/Scope";
import * as RpcClient from "effect/unstable/rpc/RpcClient";
import {
EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY,
type EffectElectronRpcMainFrame,
type EffectElectronRpcRendererBridge,
type EffectElectronRpcRendererPort,
EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY,
type EffectElectronIpcMainFrame,
type EffectElectronIpcRendererBridge,
type EffectElectronIpcRendererPort,
} from "./ipc.ts";
export interface EffectElectronRpcBrowserGlobal {
readonly [EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY]?: EffectElectronRpcRendererBridge;
export interface EffectElectronIpcBrowserGlobal {
readonly [EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY]?: EffectElectronIpcRendererBridge;
}
export function getEffectElectronRpcRendererBridge(
globalObject: EffectElectronRpcBrowserGlobal = globalThis as EffectElectronRpcBrowserGlobal,
): EffectElectronRpcRendererBridge {
const bridge = globalObject[EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY];
export function getEffectElectronIpcRendererBridge(
globalObject: EffectElectronIpcBrowserGlobal = globalThis as EffectElectronIpcBrowserGlobal,
): EffectElectronIpcRendererBridge {
const bridge = globalObject[EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY];
if (!bridge) {
throw new Error(`Missing preload bridge: window.${EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY}`);
throw new Error(`Missing preload bridge: window.${EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY}`);
}
return bridge;
}
export const makeEffectElectronRpcRendererPort = (
bridge: EffectElectronRpcRendererBridge,
): EffectElectronRpcRendererPort => bridge;
export const makeEffectElectronIpcRendererPort = (
bridge: EffectElectronIpcRendererBridge,
): EffectElectronIpcRendererPort => bridge;
export const makeEffectElectronRpcRendererProtocol = (
port: EffectElectronRpcRendererPort,
export const makeEffectElectronIpcRendererProtocol = (
port: EffectElectronIpcRendererPort,
): Effect.Effect<RpcClient.Protocol["Service"], never, Scope.Scope> =>
RpcClient.Protocol.make((writeResponse) =>
Effect.gen(function* () {
const scope = yield* Effect.scope;
const responses = yield* Queue.make<EffectElectronRpcMainFrame>();
const responses = yield* Queue.make<EffectElectronIpcMainFrame>();
const unsubscribe = port.subscribe((frame) => {
Queue.offerUnsafe(responses, frame);
});
@@ -66,5 +66,5 @@ export const makeEffectElectronRpcRendererProtocol = (
}),
);
export const layerEffectElectronRpcRendererProtocol = (port: EffectElectronRpcRendererPort) =>
Layer.effect(RpcClient.Protocol, makeEffectElectronRpcRendererProtocol(port));
export const layerEffectElectronIpcRendererProtocol = (port: EffectElectronIpcRendererPort) =>
Layer.effect(RpcClient.Protocol, makeEffectElectronIpcRendererProtocol(port));
@@ -8,48 +8,48 @@ import type { FromClientEncoded, FromServerEncoded } from "effect/unstable/rpc/R
* them in JSON-RPC text.
*/
export const EFFECT_ELECTRON_RPC_CHANNELS = {
rendererToMain: "effect-electron-rpc:renderer-to-main",
mainToRenderer: "effect-electron-rpc:main-to-renderer",
export const EFFECT_ELECTRON_IPC_CHANNELS = {
rendererToMain: "effect-electron-ipc:renderer-to-main",
mainToRenderer: "effect-electron-ipc:main-to-renderer",
} as const;
export const EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY = "effectElectronRpc" as const;
export const EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY = "effectElectronIpc" as const;
export interface EffectElectronRpcRendererFrame {
export interface EffectElectronIpcRendererFrame {
readonly version: 1;
readonly rendererClientId: number;
readonly message: FromClientEncoded;
}
export interface EffectElectronRpcMainFrame {
export interface EffectElectronIpcMainFrame {
readonly version: 1;
readonly rendererClientId: number;
readonly message: FromServerEncoded;
}
export interface EffectElectronRpcRendererPort {
readonly send: (frame: EffectElectronRpcRendererFrame) => void;
readonly subscribe: (listener: (frame: EffectElectronRpcMainFrame) => void) => () => void;
export interface EffectElectronIpcRendererPort {
readonly send: (frame: EffectElectronIpcRendererFrame) => void;
readonly subscribe: (listener: (frame: EffectElectronIpcMainFrame) => void) => () => void;
}
export type EffectElectronRpcRendererBridge = EffectElectronRpcRendererPort;
export type EffectElectronIpcRendererBridge = EffectElectronIpcRendererPort;
export interface EffectElectronRpcMainSource {
export interface EffectElectronIpcMainSource {
readonly id: number;
readonly send: (frame: EffectElectronRpcMainFrame) => void;
readonly send: (frame: EffectElectronIpcMainFrame) => void;
readonly isClosed?: () => boolean;
readonly onClose?: (listener: () => void) => () => void;
}
export interface EffectElectronRpcMainPort {
export interface EffectElectronIpcMainPort {
readonly subscribe: (
listener: (source: EffectElectronRpcMainSource, frame: EffectElectronRpcRendererFrame) => void,
listener: (source: EffectElectronIpcMainSource, frame: EffectElectronIpcRendererFrame) => void,
) => () => void;
}
export function isEffectElectronRpcRendererFrame(
export function isEffectElectronIpcRendererFrame(
value: unknown,
): value is EffectElectronRpcRendererFrame {
): value is EffectElectronIpcRendererFrame {
return (
isRecord(value) &&
value.version === 1 &&
@@ -58,7 +58,7 @@ export function isEffectElectronRpcRendererFrame(
);
}
export function isEffectElectronRpcMainFrame(value: unknown): value is EffectElectronRpcMainFrame {
export function isEffectElectronIpcMainFrame(value: unknown): value is EffectElectronIpcMainFrame {
return (
isRecord(value) &&
value.version === 1 &&
@@ -7,16 +7,16 @@ import * as RpcServer from "effect/unstable/rpc/RpcServer";
import type { FromClientEncoded } from "effect/unstable/rpc/RpcMessage";
import {
EFFECT_ELECTRON_RPC_CHANNELS,
type EffectElectronRpcMainFrame,
type EffectElectronRpcMainPort,
type EffectElectronRpcMainSource,
isEffectElectronRpcRendererFrame,
EFFECT_ELECTRON_IPC_CHANNELS,
type EffectElectronIpcMainFrame,
type EffectElectronIpcMainPort,
type EffectElectronIpcMainSource,
isEffectElectronIpcRendererFrame,
} from "./ipc.ts";
export interface ElectronLikeWebContents {
readonly id: number;
readonly send: (channel: string, frame: EffectElectronRpcMainFrame) => void;
readonly send: (channel: string, frame: EffectElectronIpcMainFrame) => void;
readonly isDestroyed?: () => boolean;
readonly once?: (event: "destroyed", listener: () => void) => ElectronLikeWebContents;
readonly off?: (event: "destroyed", listener: () => void) => ElectronLikeWebContents;
@@ -44,16 +44,16 @@ export interface ElectronLikeIpcMain {
export function makeElectronIpcMainPort(
ipcMain: ElectronLikeIpcMain,
channels = EFFECT_ELECTRON_RPC_CHANNELS,
): EffectElectronRpcMainPort {
channels = EFFECT_ELECTRON_IPC_CHANNELS,
): EffectElectronIpcMainPort {
return {
subscribe: (listener) => {
const wrapped = (event: ElectronLikeIpcMainEvent, frame: unknown) => {
if (!isEffectElectronRpcRendererFrame(frame)) {
if (!isEffectElectronIpcRendererFrame(frame)) {
return;
}
const source: EffectElectronRpcMainSource = {
const source: EffectElectronIpcMainSource = {
id: event.sender.id,
send: (responseFrame) => {
event.sender.send(channels.mainToRenderer, responseFrame);
@@ -82,8 +82,8 @@ export function makeElectronIpcMainPort(
};
}
export const makeEffectElectronRpcMainProtocol = (
port: EffectElectronRpcMainPort,
export const makeEffectElectronIpcMainProtocol = (
port: EffectElectronIpcMainPort,
): Effect.Effect<RpcServer.Protocol["Service"], never, Scope.Scope> =>
Effect.gen(function* () {
const scope = yield* Effect.scope;
@@ -120,7 +120,7 @@ export const makeEffectElectronRpcMainProtocol = (
};
const registerClient = (
source: EffectElectronRpcMainSource,
source: EffectElectronIpcMainSource,
rendererClientId: number,
): number => {
const key = `${source.id}:${rendererClientId}`;
@@ -219,13 +219,13 @@ export const makeEffectElectronRpcMainProtocol = (
});
});
export const layerEffectElectronRpcMainProtocol = (port: EffectElectronRpcMainPort) =>
Layer.effect(RpcServer.Protocol, makeEffectElectronRpcMainProtocol(port));
export const layerEffectElectronIpcMainProtocol = (port: EffectElectronIpcMainPort) =>
Layer.effect(RpcServer.Protocol, makeEffectElectronIpcMainProtocol(port));
interface MainClientRecord {
readonly key: string;
readonly rendererClientId: number;
readonly source: EffectElectronRpcMainSource;
readonly source: EffectElectronIpcMainSource;
}
type MainProtocolRequest =
@@ -1,14 +1,14 @@
import {
EFFECT_ELECTRON_RPC_CHANNELS,
EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY,
type EffectElectronRpcRendererBridge,
type EffectElectronRpcRendererFrame,
isEffectElectronRpcMainFrame,
isEffectElectronRpcRendererFrame,
EFFECT_ELECTRON_IPC_CHANNELS,
EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY,
type EffectElectronIpcRendererBridge,
type EffectElectronIpcRendererFrame,
isEffectElectronIpcMainFrame,
isEffectElectronIpcRendererFrame,
} from "./ipc.ts";
export interface ElectronLikeIpcRenderer {
readonly send: (channel: string, frame: EffectElectronRpcRendererFrame) => void;
readonly send: (channel: string, frame: EffectElectronIpcRendererFrame) => void;
readonly on: (
channel: string,
listener: (event: unknown, frame: unknown) => void,
@@ -24,23 +24,23 @@ export interface ElectronLikeIpcRenderer {
}
export interface ElectronLikeContextBridge {
readonly exposeInMainWorld: (apiKey: string, api: EffectElectronRpcRendererBridge) => void;
readonly exposeInMainWorld: (apiKey: string, api: EffectElectronIpcRendererBridge) => void;
}
export function makeEffectElectronRpcPreloadBridge(
export function makeEffectElectronIpcPreloadBridge(
electronIpcRenderer: ElectronLikeIpcRenderer,
channels = EFFECT_ELECTRON_RPC_CHANNELS,
): EffectElectronRpcRendererBridge {
channels = EFFECT_ELECTRON_IPC_CHANNELS,
): EffectElectronIpcRendererBridge {
return {
send: (frame) => {
if (!isEffectElectronRpcRendererFrame(frame)) {
if (!isEffectElectronIpcRendererFrame(frame)) {
throw new TypeError("Invalid Effect RPC renderer frame");
}
electronIpcRenderer.send(channels.rendererToMain, frame);
},
subscribe: (listener) => {
const wrapped = (_event: unknown, frame: unknown) => {
if (isEffectElectronRpcMainFrame(frame)) {
if (isEffectElectronIpcMainFrame(frame)) {
listener(frame);
}
};
@@ -53,15 +53,15 @@ export function makeEffectElectronRpcPreloadBridge(
};
}
export function exposeEffectElectronRpcPreloadBridge(options: {
export function exposeEffectElectronIpcPreloadBridge(options: {
readonly contextBridge: ElectronLikeContextBridge;
readonly ipcRenderer: ElectronLikeIpcRenderer;
readonly globalKey?: string;
readonly channels?: typeof EFFECT_ELECTRON_RPC_CHANNELS;
readonly channels?: typeof EFFECT_ELECTRON_IPC_CHANNELS;
}): void {
options.contextBridge.exposeInMainWorld(
options.globalKey ?? EFFECT_ELECTRON_RPC_RENDERER_BRIDGE_KEY,
makeEffectElectronRpcPreloadBridge(options.ipcRenderer, options.channels),
options.globalKey ?? EFFECT_ELECTRON_IPC_RENDERER_BRIDGE_KEY,
makeEffectElectronIpcPreloadBridge(options.ipcRenderer, options.channels),
);
}
@@ -7,18 +7,18 @@ import * as RpcServer from "effect/unstable/rpc/RpcServer";
import { describe, expect, it } from "vitest";
import type {
EffectElectronRpcMainFrame,
EffectElectronRpcMainSource,
EffectElectronRpcRendererFrame,
EffectElectronIpcMainFrame,
EffectElectronIpcMainSource,
EffectElectronIpcRendererFrame,
} from "./ipc.ts";
import {
makeEffectElectronRpcRendererPort,
makeEffectElectronRpcRendererProtocol,
makeEffectElectronIpcRendererPort,
makeEffectElectronIpcRendererProtocol,
} from "./client.ts";
import { makeEffectElectronRpcMainProtocol } from "./main.ts";
import { makeEffectElectronIpcMainProtocol } from "./main.ts";
const TEST_METHODS = {
echo: "effect-electron-rpc.test.echo",
echo: "effect-electron-ipc.test.echo",
} as const;
const EchoInput = Schema.Struct({
@@ -48,12 +48,12 @@ const TestHandlersLive = TestRpcGroup.toLayer(
describe("Effect Electron RPC transport", () => {
it("round-trips an Effect RPC through renderer and main ports", async () => {
const transport = new InMemoryEffectElectronRpc();
const transport = new InMemoryEffectElectronIpc();
const result = await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const mainProtocol = yield* makeEffectElectronRpcMainProtocol(transport.mainPort);
const mainProtocol = yield* makeEffectElectronIpcMainProtocol(transport.mainPort);
yield* RpcServer.make(TestRpcGroup).pipe(
Effect.provideService(RpcServer.Protocol, mainProtocol),
@@ -61,8 +61,8 @@ describe("Effect Electron RPC transport", () => {
Effect.forkScoped,
);
const rendererProtocol = yield* makeEffectElectronRpcRendererProtocol(
makeEffectElectronRpcRendererPort(transport.rendererPort),
const rendererProtocol = yield* makeEffectElectronIpcRendererProtocol(
makeEffectElectronIpcRendererPort(transport.rendererPort),
);
const client = yield* makeTestClient.pipe(
Effect.provideService(RpcClient.Protocol, rendererProtocol),
@@ -81,13 +81,13 @@ describe("Effect Electron RPC transport", () => {
});
});
class InMemoryEffectElectronRpc {
class InMemoryEffectElectronIpc {
private readonly mainListeners = new Set<
(source: EffectElectronRpcMainSource, frame: EffectElectronRpcRendererFrame) => void
(source: EffectElectronIpcMainSource, frame: EffectElectronIpcRendererFrame) => void
>();
private readonly rendererListeners = new Set<(frame: EffectElectronRpcMainFrame) => void>();
private readonly rendererListeners = new Set<(frame: EffectElectronIpcMainFrame) => void>();
readonly source: EffectElectronRpcMainSource = {
readonly source: EffectElectronIpcMainSource = {
id: 1,
send: (frame) => {
queueMicrotask(() => {
@@ -101,8 +101,8 @@ class InMemoryEffectElectronRpc {
readonly mainPort = {
subscribe: (
listener: (
source: EffectElectronRpcMainSource,
frame: EffectElectronRpcRendererFrame,
source: EffectElectronIpcMainSource,
frame: EffectElectronIpcRendererFrame,
) => void,
) => {
this.mainListeners.add(listener);
@@ -113,14 +113,14 @@ class InMemoryEffectElectronRpc {
};
readonly rendererPort = {
send: (frame: EffectElectronRpcRendererFrame) => {
send: (frame: EffectElectronIpcRendererFrame) => {
queueMicrotask(() => {
for (const listener of this.mainListeners) {
listener(this.source, frame);
}
});
},
subscribe: (listener: (frame: EffectElectronRpcMainFrame) => void) => {
subscribe: (listener: (frame: EffectElectronIpcMainFrame) => void) => {
this.rendererListeners.add(listener);
return () => {
this.rendererListeners.delete(listener);