-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathevents.ts
More file actions
54 lines (49 loc) · 1.59 KB
/
events.ts
File metadata and controls
54 lines (49 loc) · 1.59 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
import { ipcMain } from 'electron';
import type { Menubar } from 'menubar';
import type { EventData, EventType } from '../shared/events';
/**
* Register a fire-and-forget IPC listener on the main process (ipcMain.on).
* Use this when the renderer sends a one-way message and no return value is needed.
*
* @param event - The IPC channel/event name to listen on.
* @param listener - Callback invoked when the event is received.
*/
export function onMainEvent<T = EventData>(
event: EventType,
listener: (event: Electron.IpcMainEvent, args: T) => void,
) {
ipcMain.on(event, listener as Parameters<typeof ipcMain.on>[1]);
}
/**
* Register a request/response IPC handler on the main process (ipcMain.handle).
* Use this when the renderer invokes a channel and expects a value back.
*
* @param event - The IPC channel/event name to handle.
* @param listener - Callback whose return value is sent back to the renderer.
*/
export function handleMainEvent<T = EventData>(
event: EventType,
listener: (
event: Electron.IpcMainInvokeEvent,
data: T,
) => unknown | Promise<unknown>,
) {
ipcMain.handle(event, listener as Parameters<typeof ipcMain.handle>[1]);
}
/**
* Push an event from the main process to the renderer via webContents.
*
* @param mb - The menubar instance whose window receives the event.
* @param event - The IPC channel/event name to emit.
* @param data - Optional payload sent with the event.
*/
export function sendRendererEvent(
mb: Menubar,
event: EventType,
data?: string,
) {
if (!mb.window) {
return;
}
mb.window.webContents.send(event, data);
}