2015-10-26 14:54:54 +01:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-10-26 14:54:54 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 15:50:22 +02:00
|
|
|
import dis from './dispatcher';
|
2018-12-11 15:45:11 +01:00
|
|
|
import Timer from './utils/Timer';
|
2015-10-26 14:54:54 +01:00
|
|
|
|
2018-12-11 15:45:11 +01:00
|
|
|
// important this is larger than the timeouts of timers
|
|
|
|
// used with UserActivity.timeWhileActive,
|
|
|
|
// such as READ_MARKER_INVIEW_THRESHOLD_MS,
|
|
|
|
// READ_MARKER_OUTOFVIEW_THRESHOLD_MS,
|
|
|
|
// READ_RECEIPT_INTERVAL_MS in TimelinePanel
|
|
|
|
const CURRENTLY_ACTIVE_THRESHOLD_MS = 2 * 60 * 1000;
|
2015-10-26 14:54:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class watches for user activity (moving the mouse or pressing a key)
|
2018-12-11 15:45:11 +01:00
|
|
|
* and starts/stops attached timers while the user is active.
|
2015-10-26 14:54:54 +01:00
|
|
|
*/
|
|
|
|
class UserActivity {
|
2018-12-11 15:45:11 +01:00
|
|
|
constructor() {
|
|
|
|
this._attachedTimers = [];
|
|
|
|
this._activityTimeout = new Timer(CURRENTLY_ACTIVE_THRESHOLD_MS);
|
|
|
|
this._onUserActivity = this._onUserActivity.bind(this);
|
|
|
|
this._onDocumentBlurred = this._onDocumentBlurred.bind(this);
|
|
|
|
this._onPageVisibilityChanged = this._onPageVisibilityChanged.bind(this);
|
|
|
|
this.lastScreenX = 0;
|
|
|
|
this.lastScreenY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the given timer while the user is active, aborting when the user becomes inactive.
|
|
|
|
* Can be called multiple times with the same already running timer, which is a NO-OP.
|
|
|
|
* Can be called before the user becomes active, in which case it is only started
|
|
|
|
* later on when the user does become active.
|
|
|
|
*/
|
|
|
|
timeWhileActive(timer) {
|
|
|
|
// important this happens first
|
|
|
|
const index = this._attachedTimers.indexOf(timer);
|
|
|
|
if (index === -1) {
|
|
|
|
this._attachedTimers.push(timer);
|
|
|
|
// remove when done or aborted
|
|
|
|
timer.finished().finally(() => {
|
|
|
|
const index = this._attachedTimers.indexOf(timer);
|
|
|
|
if (index !== -1) { // should never be -1
|
|
|
|
this._attachedTimers.splice(index, 1);
|
|
|
|
}
|
|
|
|
// as we fork the promise here,
|
|
|
|
// avoid unhandled rejection warnings
|
|
|
|
}).catch((err) => {});
|
|
|
|
}
|
|
|
|
if (this.userCurrentlyActive()) {
|
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:54:54 +01:00
|
|
|
/**
|
|
|
|
* Start listening to user activity
|
|
|
|
*/
|
|
|
|
start() {
|
2018-12-11 15:45:11 +01:00
|
|
|
document.onmousedown = this._onUserActivity;
|
|
|
|
document.onmousemove = this._onUserActivity;
|
|
|
|
document.onkeydown = this._onUserActivity;
|
|
|
|
document.addEventListener("visibilitychange", this._onPageVisibilityChanged);
|
|
|
|
document.addEventListener("blur", this._onDocumentBlurred);
|
|
|
|
document.addEventListener("focus", this._onUserActivity);
|
2016-01-08 23:19:31 +01:00
|
|
|
// can't use document.scroll here because that's only the document
|
|
|
|
// itself being scrolled. Need to use addEventListener's useCapture.
|
2016-01-09 01:06:54 +01:00
|
|
|
// also this needs to be the wheel event, not scroll, as scroll is
|
|
|
|
// fired when the view scrolls down for a new message.
|
2018-12-11 15:45:11 +01:00
|
|
|
window.addEventListener('wheel', this._onUserActivity,
|
2016-09-06 13:10:43 +02:00
|
|
|
{ passive: true, capture: true });
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop tracking user activity
|
|
|
|
*/
|
|
|
|
stop() {
|
2016-02-12 11:19:01 +01:00
|
|
|
document.onmousedown = undefined;
|
2015-10-26 14:54:54 +01:00
|
|
|
document.onmousemove = undefined;
|
2017-04-27 11:16:24 +02:00
|
|
|
document.onkeydown = undefined;
|
2018-12-11 15:45:11 +01:00
|
|
|
window.removeEventListener('wheel', this._onUserActivity,
|
2016-09-06 13:10:43 +02:00
|
|
|
{ passive: true, capture: true });
|
2018-12-11 15:45:11 +01:00
|
|
|
|
|
|
|
document.removeEventListener("visibilitychange", this._onPageVisibilityChanged);
|
|
|
|
document.removeEventListener("blur", this._onDocumentBlurred);
|
|
|
|
document.removeEventListener("focus", this._onUserActivity);
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 17:01:31 +01:00
|
|
|
/**
|
|
|
|
* Return true if there has been user activity very recently
|
|
|
|
* (ie. within a few seconds)
|
2017-07-01 15:50:22 +02:00
|
|
|
* @returns {boolean} true if user is currently/very recently active
|
2016-01-14 17:01:31 +01:00
|
|
|
*/
|
|
|
|
userCurrentlyActive() {
|
2018-12-11 15:45:11 +01:00
|
|
|
return this._activityTimeout.isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPageVisibilityChanged(e) {
|
|
|
|
if (document.visibilityState === "hidden") {
|
|
|
|
this._activityTimeout.abort();
|
|
|
|
} else {
|
|
|
|
this._onUserActivity(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDocumentBlurred() {
|
|
|
|
this._activityTimeout.abort();
|
2016-01-14 17:01:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 15:45:11 +01:00
|
|
|
async _onUserActivity(event) {
|
2017-07-01 15:50:22 +02:00
|
|
|
if (event.screenX && event.type === "mousemove") {
|
|
|
|
if (event.screenX === this.lastScreenX && event.screenY === this.lastScreenY) {
|
2015-11-29 16:39:10 +01:00
|
|
|
// mouse hasn't actually moved
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.lastScreenX = event.screenX;
|
|
|
|
this.lastScreenY = event.screenY;
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:25:48 +01:00
|
|
|
dis.dispatch({action: 'user_activity'});
|
2018-12-11 15:45:11 +01:00
|
|
|
if (!this._activityTimeout.isRunning()) {
|
|
|
|
this._activityTimeout.start();
|
|
|
|
dis.dispatch({action: 'user_activity_start'});
|
|
|
|
this._attachedTimers.forEach((t) => t.start());
|
|
|
|
try {
|
|
|
|
await this._activityTimeout.finished();
|
|
|
|
} catch (_e) { /* aborted */ }
|
|
|
|
this._attachedTimers.forEach((t) => t.abort());
|
2016-01-14 17:01:31 +01:00
|
|
|
} else {
|
2018-12-11 15:45:11 +01:00
|
|
|
this._activityTimeout.restart();
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:45:11 +01:00
|
|
|
|
2015-10-26 14:54:54 +01:00
|
|
|
module.exports = new UserActivity();
|