Skip to content

Commit eff0994

Browse files
committed
Update ipcLink.ts
1 parent 79da01e commit eff0994

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

packages/electron-trpc/src/renderer/__tests__/ipcLink.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import superjson from "superjson";
55
import { beforeEach, describe, expect, test, vi } from "vitest";
66
import z from "zod";
77
import type { RendererGlobalElectronTRPC } from "../../types";
8-
import { ipcLink } from "../ipcLink";
8+
import { _resetIPCClient, ipcLink } from "../ipcLink";
99

1010
const t = initTRPC.create({ transformer: superjson });
1111
const router = t.router({
@@ -38,6 +38,7 @@ const electronTRPC: RendererGlobalElectronTRPC = {} as any;
3838
let handlers: ((message: TRPCResponseMessage) => void)[] = [];
3939
beforeEach(() => {
4040
handlers = [];
41+
_resetIPCClient();
4142
electronTRPC.sendMessage = vi.fn();
4243
electronTRPC.onMessage = vi.fn().mockImplementation((handler) => {
4344
handlers.push(handler);

packages/electron-trpc/src/renderer/ipcLink.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,25 @@ export type IPCLinkOptions<TRouter extends AnyTRPCRouter> = TransformerOptions<
104104
inferTRPCClientTypes<TRouter>
105105
>;
106106

107+
let singletonClient: IPCClient | null = null;
108+
109+
function getOrCreateClient(): IPCClient {
110+
if (!singletonClient) {
111+
singletonClient = new IPCClient();
112+
}
113+
return singletonClient;
114+
}
115+
116+
/** @internal Reset singleton for testing only */
117+
export function _resetIPCClient() {
118+
singletonClient = null;
119+
}
120+
107121
export function ipcLink<TRouter extends AnyTRPCRouter>(
108122
opts?: IPCLinkOptions<TRouter>,
109123
): TRPCLink<TRouter> {
110124
return () => {
111-
const client = new IPCClient();
125+
const client = getOrCreateClient();
112126
const transformer = getTransformer(opts?.transformer);
113127

114128
return ({ op }) => {

0 commit comments

Comments
 (0)