From 2c1080924499641611ce31ee097ce9b3bbcaaf79 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 23 Jul 2021 15:52:10 +0300 Subject: [PATCH] FIX: Long poll if window becomes active (#13825) This commit fixes two bugs. The first one is that onPresenceChange was called with invalid arguments and it did not register a callback. The second bug is that it triggered the wrong visibilitychange event. The function it tried to call does not exist in all versions of MessageBus. It is safer to trigger an event instead because that exists in all versions. --- .../javascripts/discourse/app/initializers/message-bus.js | 6 +----- app/assets/javascripts/discourse/app/lib/user-presence.js | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/discourse/app/initializers/message-bus.js b/app/assets/javascripts/discourse/app/initializers/message-bus.js index 2309ba066eb6d8..b7964f6a9f1554 100644 --- a/app/assets/javascripts/discourse/app/initializers/message-bus.js +++ b/app/assets/javascripts/discourse/app/initializers/message-bus.js @@ -56,11 +56,7 @@ export default { // When 20 minutes pass we stop long polling due to "shouldLongPollCallback". onPresenceChange({ unseenTime: LONG_POLL_AFTER_UNSEEN_TIME, - callback: () => { - if (messageBus.onVisibilityChange) { - messageBus.onVisibilityChange(); - } - }, + callback: () => document.dispatchEvent(new Event("visibilitychange")), }); if (siteSettings.login_required && !user) { diff --git a/app/assets/javascripts/discourse/app/lib/user-presence.js b/app/assets/javascripts/discourse/app/lib/user-presence.js index 081fff45534d93..92f17111e8fdeb 100644 --- a/app/assets/javascripts/discourse/app/lib/user-presence.js +++ b/app/assets/javascripts/discourse/app/lib/user-presence.js @@ -44,11 +44,11 @@ export function seenUser() { } // register a callback for cases where presence changed -export function onPresenceChange(maxUnseenTime, callback) { - if (maxUnseenTime < MIN_DELTA) { +export function onPresenceChange({ unseenTime, callback }) { + if (unseenTime < MIN_DELTA) { throw "unseenTime is too short"; } - callbacks.push({ unseenTime: maxUnseenTime, callback: callback }); + callbacks.push({ unseenTime, callback }); } // We could piggieback on the Scroll mixin, but it is not applied