admin管理员组文章数量:1394150
We have an electron app. We add / remove listeners using
const funct = () => {}
ipcRenderer.on('channel', funct);
// later...
ipcRenderer.removeListener('channel', funct)
We want to make sure no event handlers leak in our application. How would we query ipcRenderer
for all channel listeners?
We have an electron app. We add / remove listeners using
const funct = () => {}
ipcRenderer.on('channel', funct);
// later...
ipcRenderer.removeListener('channel', funct)
We want to make sure no event handlers leak in our application. How would we query ipcRenderer
for all channel listeners?
3 Answers
Reset to default 3ipcRenderer.eventNames()
lists all channels that have listenersipcRenderer.rawListeners(channel)
lists all listeners for a particular channel
ipcRenderer.eventNames().forEach(channel => ipcRenderer.rawListeners(channel))
Since ipcRenderer
and ipcMain
are Node EventEmitter
s, you can use the base API for event management.
eventNames
can be used to query every "channel", and removeAllListeners
can remove every listener for one channel
So this code will remove every listener from the emitter instance
ipcRenderer.eventNames().forEach(n => {
ipcRenderer.removeAllListeners(n)
})
However, you should not do this actually! (from node docs)
Note that it is bad practice to remove listeners added elsewhere in the code, particularly when the EventEmitter instance was created by some other ponent or module (e.g. sockets or file streams).
You don't have way to query for all channels at once. Closest thing is
https://electronjs/docs/api/ipc-renderer#ipcrendererremovealllistenerschannel
ipcRenderer.removeAllListeners(channel)
That you can remove all listeners to specific channels. You still have to manage list of channels by yourself.
本文标签: javascriptList all channel listeners for ipcRenderer in ElectronStack Overflow
版权声明:本文标题:javascript - List all channel listeners for ipcRenderer in Electron - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684699a2619628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论