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

ipcrenderer/methods

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

ipcRenderer.on() method

ipcRenderer.on(channel, listener) listens to a channel and calls the listener function with listener(event, args...) when a new message arrives. Parameters: channel (string), listener (Function with event IpcRendererEvent and ...args any[]).

ipcRenderer.once() method

ipcRenderer.once(channel, listener) adds a one-time listener function for the event. The listener is invoked only the next time a message is sent to the channel, after which it is removed. Parameters: channel (string), listener (Function with event IpcRendererEvent and ...args any[]).

ipcRenderer.addListener() method

ipcRenderer.addListener(channel, listener) is an alias for ipcRenderer.on(). Parameters: channel (string), listener (Function with event IpcRendererEvent and ...args any[]).

ipcRenderer.removeListener() method

ipcRenderer.removeListener(channel, listener) is an alias for ipcRenderer.off(). Parameters: channel (string), listener (Function with event IpcRendererEvent and ...args any[]).

ipcRenderer.removeAllListeners() method

ipcRenderer.removeAllListeners([channel]) removes all listeners from the specified channel. If no channel is specified, removes all listeners from all channels. Parameters: channel (string, optional).

ipcRenderer.send() method

ipcRenderer.send(channel, ...args) sends an asynchronous message to the main process via the specified channel along with arguments. Arguments are serialized with the Structured Clone Algorithm like window.postMessage, so prototype chains are not included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets throws an exception. Non-standard JavaScript types such as DOM objects or special Electron objects throw an exception. The main process handles it by listening with ipcMain module.

ipcRenderer.send() vs invoke()

If you need to receive a single response from the main process, like the result of a method call, use ipcRenderer.invoke() instead of ipcRenderer.send().

ipcRenderer.invoke() method

ipcRenderer.invoke(channel, ...args) sends a message to the main process via the specified channel and expects a result asynchronously. Returns Promise<any> that resolves with the response from the main process. Arguments are serialized with the Structured Clone Algorithm like window.postMessage. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets throws an exception. The main process should listen with ipcMain.handle(). Non-standard JavaScript types such as DOM objects throw an exception.

ipcRenderer.invoke() example

// Renderer process ipcRenderer.invoke('some-name', someArgument).then((result) => { // ... }) // Main process ipcMain.handle('some-name', async (event, someArgument) => { const result = await doSomeWork(someArgument) return result }) This example shows how to send a message from the renderer to the main process and receive an asynchronous response.

ipcRenderer.invoke() error handling

If the handler in the main process throws an error, the promise returned by invoke() will reject. However, the Error object in the renderer process will not be the same as the one thrown in the main process.

ipcRenderer.invoke() with MessagePort

When transferring a MessagePort to the main process with ipcRenderer.invoke(), use ipcRenderer.postMessage() instead.

ipcRenderer.sendSync() method

ipcRenderer.sendSync(channel, ...args) sends a message to the main process via the specified channel and expects a result synchronously. Returns any - the value sent back by the ipcMain handler. Arguments are serialized with the Structured Clone Algorithm like window.postMessage. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets throws an exception. The main process handles it by listening with ipcMain module and replies by setting event.returnValue.

ipcRenderer.sendSync() blocking warning

Sending a synchronous message blocks the whole renderer process until the reply is received. Use ipcRenderer.sendSync() only as a last resort. The asynchronous version invoke() is much better to use.

ipcRenderer.postMessage() method

ipcRenderer.postMessage(channel, message, [transfer]) sends a message to the main process, optionally transferring ownership of zero or more MessagePort objects. Parameters: channel (string), message (any), transfer (MessagePort[], optional). The transferred MessagePort objects are available in the main process as MessagePortMain objects by accessing the ports property of the emitted event.

ipcRenderer.postMessage() example

// Renderer process const { port1, port2 } = new MessageChannel() ipcRenderer.postMessage('port', { message: 'hello' }, [port1]) // Main process ipcMain.on('port', (e, msg) => { const [port] = e.ports // ... }) This example shows how to transfer a MessagePort from the renderer to the main process.

ipcRenderer.sendToHost() method

ipcRenderer.sendToHost(channel, ...args) is like ipcRenderer.send() but the event is sent to the <webview> element in the host page instead of the main process. Parameters: channel (string), ...args (any[]).

Give your agent this brain