From 7e5d4b8ce8d50e67df0a6a242d4f8f94a216d4ce Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 14 Jan 2016 16:01:31 +0000 Subject: [PATCH 1/3] Send an event at the end of user activity too and use this to send RRs. --- src/UserActivity.js | 32 ++++++++++++++++++++++++++- src/components/structures/RoomView.js | 6 +++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/UserActivity.js b/src/UserActivity.js index 8b136c0bcc..2e485ed58c 100644 --- a/src/UserActivity.js +++ b/src/UserActivity.js @@ -16,7 +16,8 @@ limitations under the License. var dis = require("./dispatcher"); -var MIN_DISPATCH_INTERVAL = 1 * 1000; +var MIN_DISPATCH_INTERVAL = 500; +var CURRENTLY_ACTIVE_THRESHOLD = 500; /** * This class watches for user activity (moving the mouse or pressing a key) @@ -38,6 +39,7 @@ class UserActivity { window.addEventListener('wheel', this._onUserActivity.bind(this), true); this.lastActivityAtTs = new Date().getTime(); this.lastDispatchAtTs = 0; + this.activityEndTimer = undefined; } /** @@ -49,6 +51,14 @@ class UserActivity { window.removeEventListener('wheel', this._onUserActivity.bind(this), true); } + /** + * Return true if there has been user activity very recently + * (ie. within a few seconds) + */ + userCurrentlyActive() { + return this.lastActivityAtTs > (new Date).getTime() - CURRENTLY_ACTIVE_THRESHOLD; + } + _onUserActivity(event) { if (event.screenX && event.type == "mousemove") { if (event.screenX === this.lastScreenX && @@ -67,6 +77,26 @@ class UserActivity { dis.dispatch({ action: 'user_activity' }); + if (!this.activityEndTimer) { + this.activityEndTimer = setTimeout( + this._onActivityEndTimer.bind(this), MIN_DISPATCH_INTERVAL + ); + } + } + } + + _onActivityEndTimer() { + var now = (new Date).getTime(); + var targetTime = this.lastActivityAtTs + MIN_DISPATCH_INTERVAL; + if (now >= targetTime) { + dis.dispatch({ + action: 'user_activity_end' + }); + this.activityEndTimer = undefined; + } else { + this.activityEndTimer = setTimeout( + this._onActivityEndTimer.bind(this), targetTime - now + ); } } } diff --git a/src/components/structures/RoomView.js b/src/components/structures/RoomView.js index 8e193b25ab..842bb1273d 100644 --- a/src/components/structures/RoomView.js +++ b/src/components/structures/RoomView.js @@ -220,6 +220,12 @@ module.exports = React.createClass({ break; case 'user_activity': + case 'user_activity_end': + // we could treat user_activity_end differently and not + // send receipts for messages that have arrived between + // the actual user activity and the time they stopped + // being active, but let's see if this is actually + // necessary. this.sendReadReceipt(); break; } From 4f21e2beb3e64901de02e369d07a6c45b15aa458 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 14 Jan 2016 16:13:50 +0000 Subject: [PATCH 2/3] Suffix with units --- src/UserActivity.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/UserActivity.js b/src/UserActivity.js index 2e485ed58c..5ed94a5f36 100644 --- a/src/UserActivity.js +++ b/src/UserActivity.js @@ -16,8 +16,8 @@ limitations under the License. var dis = require("./dispatcher"); -var MIN_DISPATCH_INTERVAL = 500; -var CURRENTLY_ACTIVE_THRESHOLD = 500; +var MIN_DISPATCH_INTERVAL_MS = 500; +var CURRENTLY_ACTIVE_THRESHOLD_MS = 500; /** * This class watches for user activity (moving the mouse or pressing a key) @@ -56,7 +56,7 @@ class UserActivity { * (ie. within a few seconds) */ userCurrentlyActive() { - return this.lastActivityAtTs > (new Date).getTime() - CURRENTLY_ACTIVE_THRESHOLD; + return this.lastActivityAtTs > (new Date).getTime() - CURRENTLY_ACTIVE_THRESHOLD_MS; } _onUserActivity(event) { @@ -72,14 +72,14 @@ class UserActivity { } this.lastActivityAtTs = (new Date).getTime(); - if (this.lastDispatchAtTs < this.lastActivityAtTs - MIN_DISPATCH_INTERVAL) { + if (this.lastDispatchAtTs < this.lastActivityAtTs - MIN_DISPATCH_INTERVAL_MS) { this.lastDispatchAtTs = this.lastActivityAtTs; dis.dispatch({ action: 'user_activity' }); if (!this.activityEndTimer) { this.activityEndTimer = setTimeout( - this._onActivityEndTimer.bind(this), MIN_DISPATCH_INTERVAL + this._onActivityEndTimer.bind(this), MIN_DISPATCH_INTERVAL_MS ); } } @@ -87,7 +87,7 @@ class UserActivity { _onActivityEndTimer() { var now = (new Date).getTime(); - var targetTime = this.lastActivityAtTs + MIN_DISPATCH_INTERVAL; + var targetTime = this.lastActivityAtTs + MIN_DISPATCH_INTERVAL_MS; if (now >= targetTime) { dis.dispatch({ action: 'user_activity_end' From 740c22238eb51b1c9a5140a8cb7084ad063e7f05 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 14 Jan 2016 16:15:07 +0000 Subject: [PATCH 3/3] Better date syntax --- src/UserActivity.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/UserActivity.js b/src/UserActivity.js index 5ed94a5f36..669b007934 100644 --- a/src/UserActivity.js +++ b/src/UserActivity.js @@ -56,7 +56,7 @@ class UserActivity { * (ie. within a few seconds) */ userCurrentlyActive() { - return this.lastActivityAtTs > (new Date).getTime() - CURRENTLY_ACTIVE_THRESHOLD_MS; + return this.lastActivityAtTs > new Date().getTime() - CURRENTLY_ACTIVE_THRESHOLD_MS; } _onUserActivity(event) { @@ -71,7 +71,7 @@ class UserActivity { this.lastScreenY = event.screenY; } - this.lastActivityAtTs = (new Date).getTime(); + this.lastActivityAtTs = new Date().getTime(); if (this.lastDispatchAtTs < this.lastActivityAtTs - MIN_DISPATCH_INTERVAL_MS) { this.lastDispatchAtTs = this.lastActivityAtTs; dis.dispatch({ @@ -86,7 +86,7 @@ class UserActivity { } _onActivityEndTimer() { - var now = (new Date).getTime(); + var now = new Date().getTime(); var targetTime = this.lastActivityAtTs + MIN_DISPATCH_INTERVAL_MS; if (now >= targetTime) { dis.dispatch({