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
|
@ -15,58 +15,54 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var MatrixClientPeg = require("./MatrixClientPeg");
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
||||||
|
var dis = require("./dispatcher");
|
||||||
|
|
||||||
// Time in ms after that a user is considered as unavailable/away
|
// Time in ms after that a user is considered as unavailable/away
|
||||||
var UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
|
var UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
|
||||||
var PRESENCE_STATES = ["online", "offline", "unavailable"];
|
var PRESENCE_STATES = ["online", "offline", "unavailable"];
|
||||||
|
|
||||||
// The current presence state
|
class Presence {
|
||||||
var state, timer;
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start listening the user activity to evaluate his presence state.
|
* Start listening the user activity to evaluate his presence state.
|
||||||
* Any state change will be sent to the Home Server.
|
* Any state change will be sent to the Home Server.
|
||||||
*/
|
*/
|
||||||
start: function() {
|
start() {
|
||||||
var self = this;
|
|
||||||
this.running = true;
|
this.running = true;
|
||||||
if (undefined === state) {
|
if (undefined === this.state) {
|
||||||
// The user is online if they move the mouse or press a key
|
|
||||||
document.onmousemove = function() { self._resetTimer(); };
|
|
||||||
document.onkeypress = function() { self._resetTimer(); };
|
|
||||||
this._resetTimer();
|
this._resetTimer();
|
||||||
|
this.dispatcherRef = dis.register(this._onUserActivity.bind(this));
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop tracking user activity
|
* Stop tracking user activity
|
||||||
*/
|
*/
|
||||||
stop: function() {
|
stop() {
|
||||||
this.running = false;
|
this.running = false;
|
||||||
if (timer) {
|
if (this.timer) {
|
||||||
clearTimeout(timer);
|
clearInterval(this.timer);
|
||||||
timer = undefined;
|
this.timer = undefined;
|
||||||
|
dis.unregister(this.dispatcherRef);
|
||||||
}
|
}
|
||||||
state = undefined;
|
this.state = undefined;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current presence state.
|
* Get the current presence state.
|
||||||
* @returns {string} the presence state (see PRESENCE enum)
|
* @returns {string} the presence state (see PRESENCE enum)
|
||||||
*/
|
*/
|
||||||
getState: function() {
|
getState() {
|
||||||
return state;
|
return this.state;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the presence state.
|
* Set the presence state.
|
||||||
* If the state has changed, the Home Server will be notified.
|
* If the state has changed, the Home Server will be notified.
|
||||||
* @param {string} newState the new presence state (see PRESENCE enum)
|
* @param {string} newState the new presence state (see PRESENCE enum)
|
||||||
*/
|
*/
|
||||||
setState: function(newState) {
|
setState(newState) {
|
||||||
if (newState === state) {
|
if (newState === this.state) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (PRESENCE_STATES.indexOf(newState) === -1) {
|
if (PRESENCE_STATES.indexOf(newState) === -1) {
|
||||||
|
@ -75,33 +71,41 @@ module.exports = {
|
||||||
if (!this.running) {
|
if (!this.running) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
state = newState;
|
var old_state = this.state;
|
||||||
MatrixClientPeg.get().setPresence(state).done(function() {
|
this.state = newState;
|
||||||
|
MatrixClientPeg.get().setPresence(this.state).done(function() {
|
||||||
console.log("Presence: %s", newState);
|
console.log("Presence: %s", newState);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
console.error("Failed to set presence: %s", err);
|
console.error("Failed to set presence: %s", err);
|
||||||
|
this.state = old_state;
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
|
* Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_onUnavailableTimerFire: function() {
|
_onUnavailableTimerFire() {
|
||||||
this.setState("unavailable");
|
this.setState("unavailable");
|
||||||
},
|
}
|
||||||
|
|
||||||
|
_onUserActivity() {
|
||||||
|
this._resetTimer();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback called when the user made an action on the page
|
* Callback called when the user made an action on the page
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_resetTimer: function() {
|
_resetTimer() {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.setState("online");
|
this.setState("online");
|
||||||
// Re-arm the timer
|
// Re-arm the timer
|
||||||
clearTimeout(timer);
|
clearTimeout(this.timer);
|
||||||
timer = setTimeout(function() {
|
this.timer = setTimeout(function() {
|
||||||
self._onUnavailableTimerFire();
|
self._onUnavailableTimerFire();
|
||||||
}, UNAVAILABLE_TIME_MS);
|
}, UNAVAILABLE_TIME_MS);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
module.exports = new Presence();
|
||||||
|
|
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();
|
|
@ -16,6 +16,7 @@ limitations under the License.
|
||||||
|
|
||||||
var MatrixClientPeg = require("../../MatrixClientPeg");
|
var MatrixClientPeg = require("../../MatrixClientPeg");
|
||||||
var RoomListSorter = require("../../RoomListSorter");
|
var RoomListSorter = require("../../RoomListSorter");
|
||||||
|
var UserActivity = require("../../UserActivity");
|
||||||
var Presence = require("../../Presence");
|
var Presence = require("../../Presence");
|
||||||
var dis = require("../../dispatcher");
|
var dis = require("../../dispatcher");
|
||||||
|
|
||||||
|
@ -92,6 +93,7 @@ module.exports = {
|
||||||
window.localStorage.clear();
|
window.localStorage.clear();
|
||||||
}
|
}
|
||||||
Notifier.stop();
|
Notifier.stop();
|
||||||
|
UserActivity.stop();
|
||||||
Presence.stop();
|
Presence.stop();
|
||||||
MatrixClientPeg.get().stopClient();
|
MatrixClientPeg.get().stopClient();
|
||||||
MatrixClientPeg.get().removeAllListeners();
|
MatrixClientPeg.get().removeAllListeners();
|
||||||
|
@ -316,6 +318,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Notifier.start();
|
Notifier.start();
|
||||||
|
UserActivity.start();
|
||||||
Presence.start();
|
Presence.start();
|
||||||
cli.startClient();
|
cli.startClient();
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue