Detail ipc.removeListener & ipc.removeAllListeners

This commit is contained in:
Ming Luo 2016-01-13 10:18:12 -05:00
parent 489539d62e
commit a82ecfda06
2 changed files with 55 additions and 6 deletions

View file

@ -1,8 +1,10 @@
# ipcMain
The `ipcMain` module, when used in the main process, handles asynchronous and
synchronous messages sent from a renderer process (web page). Messages sent from
a renderer will be emitted to this module.
The `ipcMain` module is an instance of the
[EventEmitter](https://nodejs.org/api/events.html) class. When used in the main
process, it handles asynchronous and synchronous messages sent from a renderer
process (web page). Messages sent from a renderer will be emitted to this
module.
## Sending Messages
@ -54,6 +56,28 @@ The `ipcMain` module has the following method to listen for events:
When the event occurs the `callback` is called with an `event` object and a
message, `arg`.
Once done listening for messages, if you longer want to activate this callback
and for whatever reason can't merely stop sending messages on the channel, you
can use:
### `ipcMain.removeListener(channel, callback)`
* `channel` String - The event name.
* `callback` Function - The reference to the same function that you used for
`ipcMain.on(channel, callback)`
Alternatively, if you don't have access to the same callback, you can use:
### `ipcMain.removeAllListeners(channel)`
* `channel` String - The event name.
This has the expected effect of removing *all* handlers to this ipc channel.
Because of this class' inheritance from the `EventEmitter` node class, you can
also use `ipcMain.once(channel, callback)` to fire handlers meant to occur only
once, as in, they won't be activated after one call of `callback`
## IPC Event
The `event` object passed to the `callback` has the following methods:

View file

@ -1,8 +1,10 @@
# ipcRenderer
The `ipcRenderer` module provides a few methods so you can send synchronous and
asynchronous messages from the render process (web page) to the main process.
You can also receive replies from the main process.
The `ipcRenderer` module is an instance of the
[EventEmitter](https://nodejs.org/api/events.html) class. It provides a few
methods so you can send synchronous and asynchronous messages from the render
process (web page) to the main process. You can also receive replies from the
main process.
See [ipcMain](ipc-main.md) for code examples.
@ -18,6 +20,29 @@ The `ipcRenderer` module has the following method to listen for events:
When the event occurs the `callback` is called with an `event` object and
arbitrary arguments.
Once done listening for messages, if you longer want to activate this callback
and for whatever reason can't merely stop sending messages on the channel, you
can use:
### `ipcRenderer.removeListener(channel, callback)`
* `channel` String - The event name.
* `callback` Function - The reference to the same function that you used for
`ipcRenderer.on(channel, callback)`
Alternatively, if you don't have access to the same callback, you can use:
### `ipcRenderer.removeAllListeners(channel)`
* `channel` String - The event name.
This has the expected effect of removing *all* handlers to this ipc channel.
Because of this class' inheritance from the `EventEmitter` node class, you can
also use `ipcRenderer.once(channel, callback)` to fire handlers meant to occur only
once, as in, they won't be activated after one call of `callback`
## Sending Messages
The `ipcRenderer` module has the following methods for sending messages: