From d43b77b56b48b74f27c6acab03ac363501368c28 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 26 Oct 2021 16:44:20 +0100 Subject: [PATCH] DEV: Ensure presence beforeunload event listener is cleaned up In production, services are singletons. This logic will only affect the test environment --- .../javascripts/discourse/app/services/presence.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/app/services/presence.js b/app/assets/javascripts/discourse/app/services/presence.js index 229963fa18c..da639f150d9 100644 --- a/app/assets/javascripts/discourse/app/services/presence.js +++ b/app/assets/javascripts/discourse/app/services/presence.js @@ -227,9 +227,14 @@ export default class PresenceService extends Service { this._presentProxies = {}; this._subscribedProxies = {}; this._initialDataRequests = {}; - window.addEventListener("beforeunload", () => { - this._beaconLeaveAll(); - }); + + this._beforeUnloadCallback = () => this._beaconLeaveAll(); + window.addEventListener("beforeunload", this._beforeUnloadCallback); + } + + willDestroy() { + super.willDestroy(...arguments); + window.removeEventListener("beforeunload", this._beforeUnloadCallback); } // Get a PresenceChannel object representing a single channel