Skip to content

Commit e6187f7

Browse files
committed
fix: properly handle trpc requestIDs with HMR
1 parent 322f15f commit e6187f7

34 files changed

+2459
-24
lines changed

apps/array/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
"remark-gfm": "^4.0.1",
146146
"sonner": "^2.0.7",
147147
"tippy.js": "^6.3.7",
148-
"trpc-electron": "^0.1.2",
148+
"@posthog/electron-trpc": "workspace:*",
149149
"uuid": "^9.0.1",
150150
"vscode-icons-js": "^11.6.1",
151151
"zod": "^4.1.12",

apps/array/src/main/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { mkdirSync } from "node:fs";
77
import os from "node:os";
88
import path from "node:path";
99
import { fileURLToPath } from "node:url";
10+
import { createIPCHandler } from "@posthog/electron-trpc/main";
1011
import {
1112
app,
1213
BrowserWindow,
@@ -16,7 +17,6 @@ import {
1617
type MenuItemConstructorOptions,
1718
shell,
1819
} from "electron";
19-
import { createIPCHandler } from "trpc-electron/main";
2020
import "./lib/logger";
2121
import { ANALYTICS_EVENTS } from "../types/analytics.js";
2222
import { container } from "./di/container.js";

apps/array/src/main/preload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exposeElectronTRPC } from "trpc-electron/main";
1+
import { exposeElectronTRPC } from "@posthog/electron-trpc/main";
22
import "electron-log/preload";
33

44
process.once("loaded", async () => {

apps/array/src/main/services/ui/service.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { injectable } from "inversify";
22
import { TypedEventEmitter } from "../../lib/typed-event-emitter.js";
33
import { UIServiceEvent, type UIServiceEvents } from "./schemas.js";
44

5-
/**
6-
* UIService handles UI events that need to be communicated from main to renderer.
7-
* These are typically triggered by menu items or other main process actions.
8-
*/
95
@injectable()
106
export class UIService extends TypedEventEmitter<UIServiceEvents> {
117
openSettings(): void {

apps/array/src/renderer/trpc/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { ipcLink } from "@posthog/electron-trpc/renderer";
12
import { createTRPCProxyClient } from "@trpc/client";
23
import { type CreateTRPCReact, createTRPCReact } from "@trpc/react-query";
3-
import { ipcLink } from "trpc-electron/renderer";
44
import type { TrpcRouter } from "../../main/trpc/router.js";
55

66
export function createTrpcClient() {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const config = {
2+
compilationOptions: {
3+
preferredConfigPath: "./tsconfig.json",
4+
},
5+
entries: [
6+
{
7+
filePath: "./src/renderer/index.ts",
8+
outFile: `./dist/renderer.d.ts`,
9+
noCheck: true,
10+
},
11+
{
12+
filePath: "./src/main/index.ts",
13+
outFile: `./dist/main.d.ts`,
14+
noCheck: true,
15+
},
16+
],
17+
};
18+
19+
module.exports = config;

packages/electron-trpc/main.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Generated by dts-bundle-generator v8.1.2
2+
3+
import { AnyTRPCRouter, inferRouterContext } from "@trpc/server";
4+
import { BrowserWindow, IpcMainInvokeEvent } from "electron";
5+
6+
export declare const ELECTRON_TRPC_CHANNEL = "trpc-electron";
7+
export interface CreateContextOptions {
8+
event: IpcMainInvokeEvent;
9+
}
10+
export type MaybePromise<TType> = Promise<TType> | TType;
11+
declare class IPCHandler<TRouter extends AnyTRPCRouter> {
12+
constructor({
13+
createContext,
14+
router,
15+
windows,
16+
}: {
17+
createContext?: (
18+
opts: CreateContextOptions,
19+
) => MaybePromise<inferRouterContext<TRouter>>;
20+
router: TRouter;
21+
windows?: BrowserWindow[];
22+
});
23+
attachWindow(win: BrowserWindow): void;
24+
detachWindow(win: BrowserWindow, webContentsId?: number): void;
25+
}
26+
export declare const createIPCHandler: <TRouter extends AnyTRPCRouter>({
27+
createContext,
28+
router,
29+
windows,
30+
}: {
31+
createContext?: (
32+
opts: CreateContextOptions,
33+
) => Promise<inferRouterContext<TRouter>>;
34+
router: TRouter;
35+
windows?: Electron.BrowserWindow[];
36+
}) => IPCHandler<TRouter>;
37+
export declare const exposeElectronTRPC: () => void;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "@posthog/electron-trpc",
3+
"description": "Electron support for tRPC (forked with HMR fix)",
4+
"version": "0.1.0",
5+
"type": "module",
6+
"exports": {
7+
"./main": {
8+
"types": "./dist/main/index.d.ts",
9+
"import": "./dist/main.mjs"
10+
},
11+
"./renderer": {
12+
"types": "./dist/renderer/index.d.ts",
13+
"import": "./dist/renderer.mjs"
14+
}
15+
},
16+
"files": [
17+
"dist",
18+
"src",
19+
"main.d.ts",
20+
"renderer.d.ts"
21+
],
22+
"scripts": {
23+
"build": "vite build -c src/main/vite.config.ts && vite build -c src/renderer/vite.config.ts && pnpm build:types",
24+
"build:types": "tsc -p tsconfig.build.json",
25+
"dev": "pnpm build",
26+
"typecheck": "tsc --noEmit",
27+
"test": "vitest -c vitest.config.ts",
28+
"test:ci": "vitest run -c vitest.config.ts --coverage"
29+
},
30+
"devDependencies": {
31+
"@trpc/client": "^11.8.0",
32+
"@trpc/server": "^11.8.0",
33+
"@types/node": "^20.3.1",
34+
"@vitest/coverage-v8": "^0.34.0",
35+
"builtin-modules": "^3.3.0",
36+
"dts-bundle-generator": "^8.0.1",
37+
"electron": "^35.2.1",
38+
"vite": "^6.0.7",
39+
"vite-plugin-commonjs-externals": "^0.1.4",
40+
"vitest": "^2.1.8",
41+
"typescript": "^5.8.3"
42+
},
43+
"peerDependencies": {
44+
"@trpc/client": ">=11.0.0",
45+
"@trpc/server": ">=11.0.0",
46+
"electron": ">19.0.0"
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Generated by dts-bundle-generator v8.1.2
2+
3+
import { TRPCLink } from "@trpc/client";
4+
import { TransformerOptions } from "@trpc/client/unstable-internals";
5+
import { AnyTRPCRouter, inferTRPCClientTypes } from "@trpc/server";
6+
7+
export declare const ELECTRON_TRPC_CHANNEL = "trpc-electron";
8+
export type IPCLinkOptions<TRouter extends AnyTRPCRouter> = TransformerOptions<
9+
inferTRPCClientTypes<TRouter>
10+
>;
11+
export declare function ipcLink<TRouter extends AnyTRPCRouter>(
12+
opts?: IPCLinkOptions<TRouter>,
13+
): TRPCLink<TRouter>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ELECTRON_TRPC_CHANNEL = "trpc-electron";

0 commit comments

Comments
 (0)