jooy2 / vutron

💚 Quick Start Templates for Vite + Electron + Vue 3 + Vuetify + TypeScript. Vutron is a preconfigured template for developing Electron cross-platform desktop apps. It uses Vue 3 and allows you to build a fast development environment with little effort.
https://vutron.cdget.com
MIT License
253 stars 21 forks source link

MaxListenersExceededWarning because of window.mainApi.receive #176

Closed lnrdnl closed 9 months ago

lnrdnl commented 9 months ago

MaxListenersExceededWarning In the template a listener for msgReceivedVersion is registered. But this happens every time the component is mounted. So when switching from screen to screen 12 times, you'll get a warning. This is a possible memory leak.

To Reproduce Switch from Main to Second at least 12 times.

Expected behavior Listeners should be registered only once or destroyed on onUnmounted() (I guess)

Device information (please complete the following information):

jooy2 commented 9 months ago

Hello! Thanks for using Vutron template!

Thank you for reporting the issue. That's a very good point! I hadn't thought about disabling the event listener in the past template code because there was only one screen.

I've fixed and committed the issue to the latest master code, which now uses sendSync to get the value immediately from the main process instead of going through two steps, and changed the event listener to not register.

Let me know if this doesn't work for you. If you have any other issues or want to leave a comment, please create a new issue and I'll close this one.

Thanks again for reporting the issue!

lnrdnl commented 9 months ago

I suggest using .invoke() here and in IPC use .handle().

Still I'm wondering where to register the listeners if I would have any.

lnrdnl commented 9 months ago

So what I do in onMounted is:

onMounted((): void => {
  // Get application version from package.json version string (Using IPC communication)
  window.mainApi.invoke('msgRequestGetVersion').then((version: string) => {
    appVersion.value = version
  })
})

and in IPCs:

// Get application version
ipcMain.handle('msgRequestGetVersion', () => {
  return Constants.APP_VERSION
})