Separate out the activity watcher from presence code so I can hook read receipts into it without tangling it into the presence code.
This commit is contained in:
parent
5a760b71d0
commit
a850f19cd4
3 changed files with 95 additions and 31 deletions
57
src/UserActivity.js
Normal file
57
src/UserActivity.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
Copyright 2015 OpenMarket Ltd
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
var dis = require("./dispatcher");
|
||||
|
||||
var MIN_DISPATCH_INTERVAL = 1 * 1000;
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
document.onmousemove = this._onUserActivity.bind(this);
|
||||
document.onkeypress = this._onUserActivity.bind(this);
|
||||
this.lastActivityAt = (new Date).getTime();
|
||||
this.lastDispatchAt = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop tracking user activity
|
||||
*/
|
||||
stop() {
|
||||
document.onmousemove = undefined;
|
||||
document.onkeypress = undefined;
|
||||
}
|
||||
|
||||
_onUserActivity() {
|
||||
this.lastActivityAt = (new Date).getTime();
|
||||
if (this.lastDispatchAt < this.lastActivityAt - MIN_DISPATCH_INTERVAL) {
|
||||
this.lastDispatchAt = this.lastActivityAt;
|
||||
dis.dispatch({
|
||||
action: 'user_activity'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new UserActivity();
|
Loading…
Add table
Add a link
Reference in a new issue