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')
})

},

Abhishek Matta
  • 218
  • 3
  • 15
  • 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: [example]. – snwflk Sep 15 '20 at 16:36
  • @snwflk 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. – Abhishek Matta Sep 15 '20 at 16:52

3 Answers3

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

Abhishek Matta
  • 218
  • 3
  • 15
2

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

Your_Window.on('closed',()=>{
    ipc.removeAllListeners('your_name_channel');
})
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 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](/help/how-to-answer). – Community Dec 06 '21 at 01:16
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

ALW
  • 3
  • 2
  • 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. – Abhishek Matta Sep 15 '20 at 16:44
  • @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. – psiphi75 Sep 17 '20 at 01:01
  • @psiphi75 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? – Abhishek Matta Sep 17 '20 at 04:26