refactor(desktop): add typed ipc contract bridge#37588
Open
Hona wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an initial typed IPC contract layer for the desktop app to centralize IPC channel names and associate request/response types, with small helper wrappers and basic tests to encourage typed usage across preload and main IPC calls.
Changes:
- Introduces
DesktopIpcContractand typed helper wrappers (invokeIpcWith,handleIpcWith) for IPC calls. - Adds a small bun:test suite covering the helper wrappers.
- Establishes a single place to enumerate IPC channels and their payload types (currently partial).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| packages/desktop/src/preload/ipc-contract.ts | Adds the typed IPC channel contract plus typed invoke/handle helper wrappers. |
| packages/desktop/src/preload/ipc-contract.test.ts | Adds bun:test coverage for the typed IPC helper wrappers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1
to
+25
| import type { ServerReadyData } from "./types" | ||
|
|
||
| export interface DesktopIpcContract { | ||
| "kill-sidecar": { request: void; response: void } | ||
| "await-initialization": { request: void; response: ServerReadyData } | ||
| "consume-initial-deep-links": { request: void; response: string[] } | ||
| "get-default-server-url": { request: void; response: string | null } | ||
| "set-default-server-url": { request: string | null; response: void } | ||
| "is-first-launch-onboarding-pending": { request: void; response: boolean } | ||
| "finish-first-launch-onboarding": { request: boolean; response: string | null } | ||
| "is-old-layout-eligible": { request: void; response: boolean } | ||
| "get-display-backend": { request: void; response: string | null } | ||
| "set-display-backend": { request: string | null; response: void } | ||
| "parse-markdown": { request: string; response: string } | ||
| "check-app-exists": { request: string; response: boolean } | ||
| "resolve-app-path": { request: string; response: string | null } | ||
| "set-background-color": { request: string; response: void } | ||
| "export-debug-logs": { request: void; response: string } | ||
| "store-get": { request: { name: string; key: string }; response: string | null } | ||
| "store-set": { request: { name: string; key: string; value: string }; response: void } | ||
| "store-delete": { request: { name: string; key: string }; response: void } | ||
| "store-clear": { request: { name: string }; response: void } | ||
| "store-keys": { request: { name: string }; response: string[] } | ||
| "store-length": { request: { name: string }; response: number } | ||
| } |
Comment on lines
+27
to
+43
| export type IpcChannel = keyof DesktopIpcContract | ||
|
|
||
| export async function invokeIpcWith<K extends IpcChannel>( | ||
| ipcRenderer: { invoke(channel: string, ...args: any[]): Promise<any> }, | ||
| channel: K, | ||
| request: DesktopIpcContract[K]["request"], | ||
| ): Promise<DesktopIpcContract[K]["response"]> { | ||
| return ipcRenderer.invoke(channel, request) | ||
| } | ||
|
|
||
| export function handleIpcWith<K extends IpcChannel>( | ||
| ipcMain: { handle(channel: string, listener: (event: any, request: any) => any): void }, | ||
| channel: K, | ||
| handler: (request: DesktopIpcContract[K]["request"]) => Promise<DesktopIpcContract[K]["response"]> | DesktopIpcContract[K]["response"], | ||
| ) { | ||
| ipcMain.handle(channel, (_event, request) => handler(request)) | ||
| } |
Comment on lines
+5
to
+13
| test("invokeIpcWith calls renderer with typed payload and receives response", async () => { | ||
| const mockInvoke = vi.fn().mockResolvedValue("https://127.0.0.1:4096") | ||
| const mockRenderer = { invoke: mockInvoke } as any | ||
|
|
||
| const result = await invokeIpcWith(mockRenderer, "get-default-server-url", undefined) | ||
|
|
||
| expect(mockInvoke).toHaveBeenCalledWith("get-default-server-url", undefined) | ||
| expect(result).toBe("https://127.0.0.1:4096") | ||
| }) |
Comment on lines
+3
to
+16
| export interface DesktopIpcContract { | ||
| "kill-sidecar": { request: void; response: void } | ||
| "await-initialization": { request: void; response: ServerReadyData } | ||
| "consume-initial-deep-links": { request: void; response: string[] } | ||
| "get-default-server-url": { request: void; response: string | null } | ||
| "set-default-server-url": { request: string | null; response: void } | ||
| "is-first-launch-onboarding-pending": { request: void; response: boolean } | ||
| "finish-first-launch-onboarding": { request: boolean; response: string | null } | ||
| "is-old-layout-eligible": { request: void; response: boolean } | ||
| "get-display-backend": { request: void; response: string | null } | ||
| "set-display-backend": { request: string | null; response: void } | ||
| "parse-markdown": { request: string; response: string } | ||
| "check-app-exists": { request: string; response: boolean } | ||
| "resolve-app-path": { request: string; response: string | null } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduces a end-to-end typed IPC contract bridge (\DesktopIpcContract) for desktop IPC calls between preload and main process.
Details