Electron JS event is firing multiple times
Asked Answered
K

3

1

In electronJS, I have created a custom application menu in which I'm sending the event from main process to renderer process, but now what happening is where I'm listening this event is running multiple times. So, if anyone could help me to find and resolve the error. Thanks. Here's my code:

label: test,
          click: function (item, focusedWindow, event) {
            mainWindow.webContents.send('test')
          }

ipcRenderer.on('test', (event, action) => {
      console.log('called')
    })

Now this console.log is printed multiple times.

original code:

{
  label: constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.LABEL,
  accelerator: constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.ACCELERATOR,
  click: function (item, focusedWindow, event) {
    contents.send(constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.EVENT)
  }
}

created: function () {
ipcRenderer.on(constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.EVENT, () => {
  console.log('clicked')
})

},

Kela answered 15/9, 2020 at 16:21 Comment(2)
The context of your code matters. This is fairly typical behavior when the same event handler is registered time and again, but I'm just guessing, because the code you included in your question is not complete to demonstrate the problem. Please read the following help article: minimal reproducible example.Aceydeucy
@Aceydeucy I just wanted to know what I'm doing wrong or if I'm registering it wrongly. I have added the code I have used.Kela
I
2

Try using ipc.removeAllListeners('your_name_channel') in your closed window function:

Your_Window.on('closed',()=>{
    ipc.removeAllListeners('your_name_channel');
})
Isopleth answered 5/12, 2021 at 17:33 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Larina
K
3

So after a lot of searching I found the answer.If you are switching the routes and registered some channels on one component and some on other, So you can remove the listeners for the particular channels in a lifecycle method(destroyed) when the component is unmounted. My issue was I was switching between routes and every time created was running in which I registered ipc renderer to listen to those channels. So i removed the listeners to the channels in destroyed lifecycle hook.

It can be done by:

ipcrenderer.removeAllListeners([channel])

Here's is the link for docs: Electron

Kela answered 18/9, 2020 at 22:49 Comment(0)
I
2

Try using ipc.removeAllListeners('your_name_channel') in your closed window function:

Your_Window.on('closed',()=>{
    ipc.removeAllListeners('your_name_channel');
})
Isopleth answered 5/12, 2021 at 17:33 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Larina
F
0

ipcRenderer.on continuously listens, while once is only invoked for the next event - then removed.

ipcRenderer.once(*channel*, *listener*)

Source: http://man.hubwiz.com/docset/electron.docset/Contents/Resources/Documents/docs/api/ipc-renderer.html

Failure answered 15/9, 2020 at 16:32 Comment(3)
Thanks for replying. but using once doesn't work, as it removes the listener and second time when I click on the menu item it simply don't work.Kela
@shawnbatra then your problem is probably due to the created function being called multiple times. Like @snwfkl mentioned, you have only provided a small snippet of code, your bug is not here, it's somewhere else.Griffen
@Griffen So, what is best place to register it? One more thing is it possible that there are two components on two different routes and I have registered some ipcrenderer channels on component one and some on other. When I switch the component it is registering again as created function is called every time. And if this is the case What should I do?Kela

© 2022 - 2024 — McMap. All rights reserved.