-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathutils.ts
More file actions
56 lines (51 loc) · 1.62 KB
/
utils.ts
File metadata and controls
56 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { ipcRenderer } from 'electron';
import type { EventData, EventType } from '../shared/events';
/**
* Send a fire-and-forget IPC message from the renderer to the main process.
*
* @param event - The IPC event type to send.
* @param data - Optional payload to include with the event.
*/
export function sendMainEvent(event: EventType, data?: EventData): void {
ipcRenderer.send(event, data);
}
/**
* Send an IPC message from the renderer to the main process and await a response.
*
* @param event - The IPC event type to invoke.
* @param data - Optional string payload to include with the event.
* @returns A promise that resolves to the string response from the main process.
*/
export async function invokeMainEvent(
event: EventType,
data?: string,
): Promise<string> {
try {
return await ipcRenderer.invoke(event, data);
} catch (err) {
// biome-ignore lint/suspicious/noConsole: preload environment is strictly sandboxed
console.error(`[IPC] invoke failed: ${event}`, err);
throw err;
}
}
/**
* Invoke a main-process handler with structured `EventData` and await the result.
*/
export function invokeMainEventWithData(
event: EventType,
data?: EventData,
): Promise<unknown> {
return ipcRenderer.invoke(event, data);
}
/**
* Register a listener for an IPC event sent from the main process to the renderer.
*
* @param event - The IPC event type to listen for.
* @param listener - The callback invoked when the event is received.
*/
export function onRendererEvent(
event: EventType,
listener: (event: Electron.IpcRendererEvent, args: string) => void,
) {
ipcRenderer.on(event, listener);
}