Skip to content

Commit 5161cff

Browse files
committed
refactor: preload consistency
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 463f2c0 commit 5161cff

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/preload/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { invokeMainEvent, onRendererEvent, sendMainEvent } from './utils';
99
/**
1010
* The Gitify Bridge API exposed to the renderer via `contextBridge`.
1111
*
12-
* All renderer↔main IPC communication must go through this object.
13-
* It is available on `window.gitify` inside the renderer process.
12+
* Provides a safe, sandboxed interface for IPC communication between renderer and main.
13+
* Accessible as `window.gitify` in the renderer.
1414
*/
1515
export const api = {
1616
/**
@@ -118,7 +118,6 @@ export const api = {
118118
*/
119119
twemojiDirectory: () => invokeMainEvent(EVENTS.TWEMOJI_DIRECTORY),
120120

121-
/** Platform detection helpers. */
122121
/** Platform detection helpers. */
123122
platform: {
124123
/** Returns `true` when running on Linux. */
@@ -161,7 +160,7 @@ export const api = {
161160
},
162161
},
163162

164-
/** Electron web frame zoom controls. */
163+
/** Electron `webFrame` zoom controls. */
165164
zoom: {
166165
/**
167166
* Return the current Electron zoom level.
@@ -231,8 +230,10 @@ export const api = {
231230
},
232231
};
233232

234-
// Use `contextBridge` APIs to expose Electron APIs to renderer
235-
// Context isolation is always enabled in this app
233+
/**
234+
* Use `contextBridge` APIs to expose Electron APIs to renderer.
235+
* Context isolation is always enabled in this app
236+
*/
236237
try {
237238
contextBridge.exposeInMainWorld('gitify', api);
238239
} catch (error) {

src/preload/utils.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ipcRenderer } from 'electron';
3131
describe('preload/utils', () => {
3232
it('sendMainEvent forwards to ipcRenderer.send', () => {
3333
sendMainEvent(EVENTS.WINDOW_SHOW);
34+
3435
expect(ipcRenderer.send).toHaveBeenCalledWith(
3536
EVENTS.WINDOW_SHOW,
3637
undefined,
@@ -39,6 +40,7 @@ describe('preload/utils', () => {
3940

4041
it('invokeMainEvent forwards and resolves', async () => {
4142
const result = await invokeMainEvent(EVENTS.VERSION, 'data');
43+
4244
expect(ipcRenderer.invoke).toHaveBeenCalledWith(EVENTS.VERSION, 'data');
4345
expect(result).toBe('response');
4446
});
@@ -57,6 +59,7 @@ describe('preload/utils', () => {
5759
__emit: (channel: string, ...a: unknown[]) => void;
5860
}
5961
).__emit(EVENTS.UPDATE_ICON_TITLE, 'payload');
62+
6063
expect(ipcRenderer.on).toHaveBeenCalledWith(
6164
EVENTS.UPDATE_ICON_TITLE,
6265
handlerMock,

src/preload/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ export function sendMainEvent(event: EventType, data?: EventData): void {
1919
* @param data - Optional string payload to include with the event.
2020
* @returns A promise that resolves to the string response from the main process.
2121
*/
22-
export function invokeMainEvent(
22+
export async function invokeMainEvent(
2323
event: EventType,
2424
data?: string,
2525
): Promise<string> {
26-
return ipcRenderer.invoke(event, data);
26+
try {
27+
return await ipcRenderer.invoke(event, data);
28+
} catch (err) {
29+
// biome-ignore lint/suspicious/noConsole: preload environment is strictly sandboxed
30+
console.error(`[IPC] invoke failed: ${event}`, err);
31+
throw err;
32+
}
2733
}
2834

2935
/**

0 commit comments

Comments
 (0)