DEV: Ensure presence beforeunload event listener is cleaned up

DEV: Ensure presence beforeunload event listener is cleaned up

In production, services are singletons. This logic will only affect the test environment

diff --git a/app/assets/javascripts/discourse/app/services/presence.js b/app/assets/javascripts/discourse/app/services/presence.js
index 229963f..da639f1 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

GitHub sha: d43b77b56b48b74f27c6acab03ac363501368c28

This commit appears in #14718 which was approved by markvanlan. It was merged by davidtaylorhq.