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.
This commit is contained in:
Sam Saffron 2020-03-27 10:14:13 +11:00
parent 257f59f366
commit bed3f7f69a
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5
2 changed files with 7 additions and 3 deletions

View File

@ -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();

View File

@ -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;
}