FIX: do not leak visibility event subscriptions on stop/start

FIX: do not leak visibility event subscriptions on stop/start

Also expose me.onVisibilityChange that can be used to trigger a long poll when a custom shouldLongPollCallback is used.

diff --git a/assets/message-bus.js b/assets/message-bus.js
index 4c38591..e945c71 100644
--- a/assets/message-bus.js
+++ b/assets/message-bus.js
@@ -388,9 +388,17 @@
         clearTimeout(delayPollTimeout);
         delayPollTimeout = null;
       }
+      if (pollTimeout) {
+        clearTimeout(pollTimeout);
+        pollTimeout = null;
+      }
       if (me.longPoll) {
         me.longPoll.abort();
       }
+      if (me.onVisibilityChange) {
+        document.removeEventListener("visibilitychange", me.onVisibilityChange);
+        me.onVisibilityChange = null;
+      }
     },
 
     // Start polling
@@ -428,23 +436,22 @@
 
       // monitor visibility, issue a new long poll when the page shows
       if (document.addEventListener && "hidden" in document) {
-        me.visibilityEvent = document.addEventListener(
-          "visibilitychange",
-          function () {
-            if (
-              !document.hidden &&
-              !me.longPoll &&
-              (pollTimeout || delayPollTimeout)
-            ) {
-              clearTimeout(pollTimeout);
-              clearTimeout(delayPollTimeout);
-
-              delayPollTimeout = null;
-              pollTimeout = null;
-              poll();
-            }
+        me.onVisibilityChange = function () {
+          if (
+            !document.hidden &&
+            !me.longPoll &&
+            (pollTimeout || delayPollTimeout)
+          ) {
+            clearTimeout(pollTimeout);
+            clearTimeout(delayPollTimeout);
+
+            delayPollTimeout = null;
+            pollTimeout = null;
+            poll();
           }
-        );
+        };
+
+        document.addEventListener("visibilitychange", me.onVisibilityChange);
       }
 
       poll();

GitHub sha: b9cfb90d

1 Like