electron-seamless-titlebar-tutorial icon indicating copy to clipboard operation
electron-seamless-titlebar-tutorial copied to clipboard

Clean up Electron win listeners on app reload

Open AtiqGauri opened this issue 4 years ago • 6 comments

Using events in renderer process throw error on development console saying Attempting to call a function in a renderer window that has been closed or released.

win.on('maximize', toggleMaxRestoreButtons);
win.on('unmaximize', toggleMaxRestoreButtons);

This will cause error because you haven't cleaned up correctly and a listener exists on a remote object from a renderer that does not exist any more.

Solution - Avoid using remote or clean up your event handlers on remote objects if you have to use it

Add event remover in your code where you had added events code

window.onbeforeunload = (e) => {
    win.removeAllListeners();
};

This will remove ALL listeners attached to your window before unloading it.

Note - onbeforeunload will be called before page is refreshed or closed.

Screenshot- Screenshot (146)

Conclusion- update tutorial to adapt to solution

To reproduce-

This is not same as your tutorial but here too I am using win.on events for blur and focus and it throws errors. https://gist.github.com/AtiqGauri/1cea1c548025faa77f9f29008ca5a5fe#file-main-js-L4

AtiqGauri avatar Mar 05 '20 09:03 AtiqGauri

Yeah I have implemented window.onbeforeunload / win.removeAllListeners(); in my own app because I was getting annoying warnings when react autoreloaded. It's a good idea.

Toby56 avatar Mar 05 '20 10:03 Toby56

Implemented in 73a24df

binaryfunt avatar Mar 08 '20 15:03 binaryfunt

There is a problem with this, it's pretty likely you might have other event listeners on the win object that you might not want removed just because the webpage reloaded. Maybe only remove the ones that are for the window buttons?

Toby56 avatar Mar 16 '20 07:03 Toby56

Not hard, even if you have a lot of events like this XD:

win.removeListener("page-title-updated", updateTitle);
win.removeListener("maximize", toggleMaximized);
win.removeListener("unmaximize", toggleMaximized);
win.removeListener("blur", toggleBlurred);
win.removeListener("focus", toggleBlurred);
win.removeListener("enter-full-screen", toggleFullScreen);
win.removeListener("leave-full-screen", toggleFullScreen);

Toby56 avatar Mar 16 '20 07:03 Toby56

I suppose so... Perhaps one could even do something like

// Near top of file:
const trackedListeners = [];

// Later:
function addTrackedListener(win, event, handler) {
    win.on(event, handler);
    trackedListeners.push({'event': event, 'handler': handler});
}

function removeTrackedListeners(win) {
    for (listener of trackedListeners) {
        win.removeListener(listener.event, listener.handler);
    }
}

binaryfunt avatar Mar 16 '20 23:03 binaryfunt

Everything including adding and removing the listeners as well as the functions themselves:

let events = {
  "page-title-updated": () =>
    (document.getElementById("titlebar-text").innerHTML = document.title),
  "maximize, unmaximize": () =>
    document.body.classList[win.isMaximized() ? "add" : "remove"]("maximized"),
  "blur, focus": () =>
    document.body.classList[win.isFocused() ? "remove" : "add"]("blurred"),
  "enter-full-screen, leave-full-screen": () =>
    document.body.classList[win.isFullScreen() ? "add" : "remove"]("full-screen")
};

for (const event in events) {
  events[event]();
  event.split(", ").forEach(eventSplit => {
    win.on(eventSplit, events[event]);
    window.addEventListener("beforeunload", () => {
      win.removeListener(eventSplit, events[event]);
    });
  });
}

Toby56 avatar Mar 17 '20 07:03 Toby56