docs: add missing ipcRenderer.off() / ipcRenderer.addListener() aliases (#39816)

* docs: add missing `ipcRenderer.off()` / `ipcRenderer.addListener()` aliases

* Update docs/api/ipc-renderer.md

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>

* fix ipcRenderer.removeListener

* update ts-smoke

---------

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
This commit is contained in:
Milan Burda 2023-09-18 18:58:20 +02:00 committed by GitHub
parent d7e4bb6608
commit 9c7086074e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

View file

@ -32,6 +32,15 @@ The `ipcRenderer` module has the following method to listen for events and send
Listens to `channel`, when a new message arrives `listener` would be called with
`listener(event, args...)`.
### `ipcRenderer.off(channel, listener)`
* `channel` string
* `listener` Function
* `event` [IpcRendererEvent][ipc-renderer-event]
* `...args` any[]
Alias for [`ipcRenderer.removeListener`](#ipcrendererremovelistenerchannel-listener).
### `ipcRenderer.once(channel, listener)`
* `channel` string
@ -42,10 +51,20 @@ Listens to `channel`, when a new message arrives `listener` would be called with
Adds a one time `listener` function for the event. This `listener` is invoked
only the next time a message is sent to `channel`, after which it is removed.
### `ipcRenderer.addListener(channel, listener)`
* `channel` string
* `listener` Function
* `event` [IpcRendererEvent][ipc-renderer-event]
* `...args` any[]
Alias for [`ipcRenderer.on`](#ipcrendereronchannel-listener).
### `ipcRenderer.removeListener(channel, listener)`
* `channel` string
* `listener` Function
* `event` [IpcRendererEvent][ipc-renderer-event]
* `...args` any[]
Removes the specified `listener` from the listener array for the specified

View file

@ -562,6 +562,11 @@ globalShortcut.unregisterAll();
// ipcMain
// https://github.com/electron/electron/blob/main/docs/api/ipc-main.md
ipcMain.handle('ping-pong', (event, arg: any) => {
console.log(arg); // prints "ping"
return 'pong';
});
ipcMain.on('asynchronous-message', (event, arg: any) => {
console.log(arg); // prints "ping"
event.sender.send('asynchronous-reply', 'pong');
@ -572,11 +577,6 @@ ipcMain.on('synchronous-message', (event, arg: any) => {
event.returnValue = 'pong';
});
ipcMain.on('synchronous-message', (event, arg: any) => {
console.log(arg); // prints "ping"
event.returnValue = 'pong';
});
const winWindows = new BrowserWindow({
width: 800,
height: 600,

View file

@ -4,8 +4,20 @@ import { clipboard, crashReporter, shell } from 'electron/common';
// In renderer process (web page).
// https://github.com/electron/electron/blob/main/docs/api/ipc-renderer.md
(async () => {
console.log(await ipcRenderer.invoke('ping-pong')); // prints "pong"
})();
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong"
ipcRenderer.on('test', () => {});
ipcRenderer.off('test', () => {});
ipcRenderer.once('test', () => {});
ipcRenderer.addListener('test', () => {});
ipcRenderer.removeListener('test', () => {});
ipcRenderer.removeAllListeners('test');
ipcRenderer.on('asynchronous-reply', (event, arg: any) => {
console.log(arg); // prints "pong"
event.sender.send('another-message', 'Hello World!');

View file

@ -220,9 +220,9 @@
vscode-uri "^3.0.7"
"@electron/typescript-definitions@^8.14.5":
version "8.14.6"
resolved "https://registry.yarnpkg.com/@electron/typescript-definitions/-/typescript-definitions-8.14.6.tgz#78ba1fa8314f06255bb9309791b33c9695ac42ef"
integrity sha512-HK70Q3nrp6h4cCxb/P65vFixdJ99vABLIG8TpqU21/fmuHdYboL4zcleWaYhXhU2EwduuOPfORFMrUTdBRc+lw==
version "8.14.7"
resolved "https://registry.yarnpkg.com/@electron/typescript-definitions/-/typescript-definitions-8.14.7.tgz#f8838eac200fa8106ce0a6b7044463b0fd86e9b6"
integrity sha512-y1kOB9Ckkd09+KpNDIID6LHO7WP69WoMiogGwNIRtBnEZIeK/aN5uy6plEF1OXYoJVkRKkM/ZSBhhwhk4H1rEA==
dependencies:
"@types/node" "^11.13.7"
chalk "^2.4.2"