This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 58
Clean up Electron win listeners on app reload #17
Comments
Yeah I have implemented |
Implemented in 73a24df |
There is a problem with this, it's pretty likely you might have other event listeners on the |
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); |
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);
}
} |
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]);
});
});
} |
binaryfunt
changed the title
Using win.on event in renderer process give error on console
Clean up Electron win listeners on app reload
Apr 11, 2020
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
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
This will remove ALL listeners attached to your window before unloading it.
Note - onbeforeunload will be called before page is refreshed or closed.
Screenshot-
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
The text was updated successfully, but these errors were encountered: