new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Electron · API · all subjects

ipcmain/methods

10 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

ipcMain message sending methods

When sending messages from the main process to renderer processes, use webContents.send(). The event name is the channel. To reply to a synchronous message, set event.returnValue. To send an asynchronous message back to the sender, use event.reply(), which automatically handles messages from frames that aren't the main frame (such as iframes), whereas event.sender.send() always sends to the main frame.

ipcMain.on(channel, listener)

Listen to messages on a channel. Takes channel (string) and listener (Function). The listener is called with listener(event, args...) where event is an IpcMainEvent and args are any values. When a new message arrives on the channel, the listener is invoked.

ipcMain.off(channel, listener)

Removes the specified listener from the listener array for the specified channel. Takes channel (string) and listener (Function where the function receives event as IpcMainEvent and args as any[]).

ipcMain.once(channel, listener)

Adds a one-time listener function for the event. Takes channel (string) and listener (Function). The listener is invoked only the next time a message is sent to the channel, after which it is automatically removed. The listener receives event (IpcMainEvent) and args (any[]).

ipcMain.addListener(channel, listener)

Alias for ipcMain.on(). Takes channel (string) and listener (Function receiving event as IpcMainEvent and args as any[]).

ipcMain.removeListener(channel, listener)

Alias for ipcMain.off(). Takes channel (string) and listener (Function receiving args as any[]).

ipcMain.handle(channel, listener)

Adds a handler for an invokable IPC. This handler is called whenever a renderer calls ipcRenderer.invoke(channel, ...args). Takes channel (string) and listener (Function<Promise<any> | any> receiving event as IpcMainInvokeEvent and args as any[]). If the listener returns a Promise, the promise result is returned as a reply to the remote caller. Otherwise, the return value is used as the reply. Errors thrown in the main process handler are not transparent; only the message property from the serialized error is provided to the renderer process.

ipcMain.handleOnce(channel, listener)

Handles a single invokable IPC message, then removes the listener. Takes channel (string) and listener (Function<Promise<any> | any> receiving event as IpcMainInvokeEvent and args as any[]). Works the same as ipcMain.handle() but is only triggered once.

ipcMain.removeHandler(channel)

Removes any handler for the specified channel, if present. Takes channel (string).

ipcMain.handle example with Promise

Main process example showing ipcMain.handle with an async function: ```js ipcMain.handle('my-invokable-ipc', async (event, ...args) => { const result = await somePromise(...args) return result }) ``` Renderer process example showing ipcRenderer.invoke: ```js async () => { const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2) // ... } ```

Give your agent this brain