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';
|
2015-10-26 14:54:54 +01:00
|
|
|
|
2017-07-01 15:50:22 +02:00
|
|
|
const MIN_DISPATCH_INTERVAL_MS = 500;
|
|
|
|
const CURRENTLY_ACTIVE_THRESHOLD_MS = 2000;
|
2015-10-26 14:54:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class watches for user activity (moving the mouse or pressing a key)
|
|
|
|
* and dispatches the user_activity action at times when the user is interacting
|
|
|
|
* with the app (but at a much lower frequency than mouse move events)
|
|
|
|
*/
|
|
|
|
class UserActivity {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listening to user activity
|
|
|
|
*/
|
|
|
|
start() {
|
2016-02-12 11:19:01 +01:00
|
|
|
document.onmousedown = this._onUserActivity.bind(this);
|
2015-10-26 14:54:54 +01:00
|
|
|
document.onmousemove = this._onUserActivity.bind(this);
|
2017-04-27 11:16:24 +02:00
|
|
|
document.onkeydown = this._onUserActivity.bind(this);
|
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.
|
2016-09-06 13:10:43 +02:00
|
|
|
window.addEventListener('wheel', this._onUserActivity.bind(this),
|
|
|
|
{ passive: true, capture: true });
|
2015-11-18 15:51:06 +01:00
|
|
|
this.lastActivityAtTs = new Date().getTime();
|
|
|
|
this.lastDispatchAtTs = 0;
|
2016-01-14 17:01:31 +01:00
|
|
|
this.activityEndTimer = undefined;
|
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;
|
2016-09-06 13:10:43 +02:00
|
|
|
window.removeEventListener('wheel', this._onUserActivity.bind(this),
|
|
|
|
{ passive: true, capture: true });
|
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() {
|
2016-01-14 17:15:07 +01:00
|
|
|
return this.lastActivityAtTs > new Date().getTime() - CURRENTLY_ACTIVE_THRESHOLD_MS;
|
2016-01-14 17:01:31 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 16:39:10 +01:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:15:07 +01:00
|
|
|
this.lastActivityAtTs = new Date().getTime();
|
2016-01-14 17:13:50 +01:00
|
|
|
if (this.lastDispatchAtTs < this.lastActivityAtTs - MIN_DISPATCH_INTERVAL_MS) {
|
2015-11-18 15:51:06 +01:00
|
|
|
this.lastDispatchAtTs = this.lastActivityAtTs;
|
2015-10-26 14:54:54 +01:00
|
|
|
dis.dispatch({
|
2017-07-01 15:50:22 +02:00
|
|
|
action: 'user_activity',
|
2015-10-26 14:54:54 +01:00
|
|
|
});
|
2016-01-14 17:01:31 +01:00
|
|
|
if (!this.activityEndTimer) {
|
2017-07-01 15:50:22 +02:00
|
|
|
this.activityEndTimer = setTimeout(this._onActivityEndTimer.bind(this), MIN_DISPATCH_INTERVAL_MS);
|
2016-01-14 17:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onActivityEndTimer() {
|
2017-07-01 15:50:22 +02:00
|
|
|
const now = new Date().getTime();
|
|
|
|
const targetTime = this.lastActivityAtTs + MIN_DISPATCH_INTERVAL_MS;
|
2016-01-14 17:01:31 +01:00
|
|
|
if (now >= targetTime) {
|
|
|
|
dis.dispatch({
|
2017-07-01 15:50:22 +02:00
|
|
|
action: 'user_activity_end',
|
2016-01-14 17:01:31 +01:00
|
|
|
});
|
|
|
|
this.activityEndTimer = undefined;
|
|
|
|
} else {
|
2017-07-01 15:50:22 +02:00
|
|
|
this.activityEndTimer = setTimeout(this._onActivityEndTimer.bind(this), targetTime - now);
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new UserActivity();
|