This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/user-presence.js
Sam Saffron bed3f7f69a
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.
2020-03-27 10:14:13 +11:00

47 lines
1.3 KiB
JavaScript

// for android we test webkit
const hiddenProperty =
document.hidden !== undefined
? "hidden"
: document.webkitHidden !== undefined
? "webkitHidden"
: undefined;
const MAX_UNSEEN_TIME = 60000;
let seenUserTime = Date.now();
export default function(maxUnseenTime) {
maxUnseenTime = maxUnseenTime === undefined ? MAX_UNSEEN_TIME : maxUnseenTime;
const now = Date.now();
if (seenUserTime + maxUnseenTime < now) {
return false;
}
if (hiddenProperty !== undefined) {
return !document[hiddenProperty];
} else {
return document && document.hasFocus;
}
}
export function seenUser() {
seenUserTime = Date.now();
}
// We could piggieback on the Scroll mixin, but it is not applied
// consistently to all pages
//
// We try to keep this as cheap as possible by performing absolute minimal
// amount of work when the event handler is fired
//
// An alternative would be to use a timer that looks at the scroll position
// however this will not work as message bus can issue page updates and scroll
// page around when user is not present
//
// We avoid tracking mouse move which would be very expensive
$(document).bind("touchmove.discourse-track-presence", seenUser);
$(document).bind("click.discourse-track-presence", seenUser);
$(window).bind("scroll.discourse-track-presence", seenUser);