From bed3f7f69a718a0b47199e09bc1570a3164597a8 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 27 Mar 2020 10:14:13 +1100 Subject: [PATCH] DEV: long poll for 20 extra minutes when user stops interacting We have no way of detecting if a browser window is behind another window or off screen on a virtual desktop. In some cases we may want events to be delivered quicker to the browser. Specifically a user may still have a window in view but is not interacting. This gives users 20 minutes of extra "long polling time" prior to shifting to short polling. --- app/assets/javascripts/discourse/initializers/message-bus.js | 5 ++++- app/assets/javascripts/discourse/lib/user-presence.js | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/message-bus.js b/app/assets/javascripts/discourse/initializers/message-bus.js index 61905e42a0c..f732065c0ff 100644 --- a/app/assets/javascripts/discourse/initializers/message-bus.js +++ b/app/assets/javascripts/discourse/initializers/message-bus.js @@ -2,6 +2,8 @@ import userPresent from "discourse/lib/user-presence"; import { handleLogoff } from "discourse/lib/ajax"; +const LONG_POLL_AFTER_UNSEEN_TIME = 1200000; // 20 minutes + function ajax(opts) { if (opts.complete) { const oldComplete = opts.complete; @@ -31,7 +33,8 @@ export default { siteSettings = container.lookup("site-settings:main"); messageBus.alwaysLongPoll = Discourse.Environment === "development"; - messageBus.shouldLongPollCallback = userPresent; + messageBus.shouldLongPollCallback = () => + userPresent(LONG_POLL_AFTER_UNSEEN_TIME); // we do not want to start anything till document is complete messageBus.stop(); diff --git a/app/assets/javascripts/discourse/lib/user-presence.js b/app/assets/javascripts/discourse/lib/user-presence.js index 67d332981ff..7d6e02d737d 100644 --- a/app/assets/javascripts/discourse/lib/user-presence.js +++ b/app/assets/javascripts/discourse/lib/user-presence.js @@ -10,10 +10,11 @@ const MAX_UNSEEN_TIME = 60000; let seenUserTime = Date.now(); -export default function() { +export default function(maxUnseenTime) { + maxUnseenTime = maxUnseenTime === undefined ? MAX_UNSEEN_TIME : maxUnseenTime; const now = Date.now(); - if (seenUserTime + MAX_UNSEEN_TIME < now) { + if (seenUserTime + maxUnseenTime < now) { return false; }