Skip to content

Commit a850f19

Browse files
committed
Separate out the activity watcher from presence code so I can hook read receipts into it without tangling it into the presence code.
1 parent 5a760b7 commit a850f19

3 files changed

Lines changed: 95 additions & 31 deletions

File tree

‎src/Presence.js‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,54 @@ limitations under the License.
1515
*/
1616

1717
var MatrixClientPeg = require("./MatrixClientPeg");
18+
var dis = require("./dispatcher");
1819

1920
// Time in ms after that a user is considered as unavailable/away
2021
var UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
2122
var PRESENCE_STATES = ["online", "offline", "unavailable"];
2223

23-
// The current presence state
24-
var state, timer;
25-
26-
module.exports = {
24+
class Presence {
2725

2826
/**
2927
* Start listening the user activity to evaluate his presence state.
3028
* Any state change will be sent to the Home Server.
3129
*/
32-
start: function() {
33-
var self = this;
30+
start() {
3431
this.running = true;
35-
if (undefined === state) {
36-
// The user is online if they move the mouse or press a key
37-
document.onmousemove = function() { self._resetTimer(); };
38-
document.onkeypress = function() { self._resetTimer(); };
32+
if (undefined === this.state) {
3933
this._resetTimer();
34+
this.dispatcherRef = dis.register(this._onUserActivity.bind(this));
4035
}
41-
},
36+
}
4237

4338
/**
4439
* Stop tracking user activity
4540
*/
46-
stop: function() {
41+
stop() {
4742
this.running = false;
48-
if (timer) {
49-
clearTimeout(timer);
50-
timer = undefined;
43+
if (this.timer) {
44+
clearInterval(this.timer);
45+
this.timer = undefined;
46+
dis.unregister(this.dispatcherRef);
5147
}
52-
state = undefined;
53-
},
48+
this.state = undefined;
49+
}
5450

5551
/**
5652
* Get the current presence state.
5753
* @returns {string} the presence state (see PRESENCE enum)
5854
*/
59-
getState: function() {
60-
return state;
61-
},
55+
getState() {
56+
return this.state;
57+
}
6258

6359
/**
6460
* Set the presence state.
6561
* If the state has changed, the Home Server will be notified.
6662
* @param {string} newState the new presence state (see PRESENCE enum)
6763
*/
68-
setState: function(newState) {
69-
if (newState === state) {
64+
setState(newState) {
65+
if (newState === this.state) {
7066
return;
7167
}
7268
if (PRESENCE_STATES.indexOf(newState) === -1) {
@@ -75,33 +71,41 @@ module.exports = {
7571
if (!this.running) {
7672
return;
7773
}
78-
state = newState;
79-
MatrixClientPeg.get().setPresence(state).done(function() {
74+
var old_state = this.state;
75+
this.state = newState;
76+
MatrixClientPeg.get().setPresence(this.state).done(function() {
8077
console.log("Presence: %s", newState);
8178
}, function(err) {
8279
console.error("Failed to set presence: %s", err);
80+
this.state = old_state;
8381
});
84-
},
82+
}
8583

8684
/**
8785
* Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
8886
* @private
8987
*/
90-
_onUnavailableTimerFire: function() {
88+
_onUnavailableTimerFire() {
9189
this.setState("unavailable");
92-
},
90+
}
91+
92+
_onUserActivity() {
93+
this._resetTimer();
94+
}
9395

9496
/**
9597
* Callback called when the user made an action on the page
9698
* @private
9799
*/
98-
_resetTimer: function() {
100+
_resetTimer() {
99101
var self = this;
100102
this.setState("online");
101103
// Re-arm the timer
102-
clearTimeout(timer);
103-
timer = setTimeout(function() {
104+
clearTimeout(this.timer);
105+
this.timer = setTimeout(function() {
104106
self._onUnavailableTimerFire();
105107
}, UNAVAILABLE_TIME_MS);
106108
}
107-
};
109+
}
110+
111+
module.exports = new Presence();

‎src/UserActivity.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
var dis = require("./dispatcher");
18+
19+
var MIN_DISPATCH_INTERVAL = 1 * 1000;
20+
21+
/**
22+
* This class watches for user activity (moving the mouse or pressing a key)
23+
* and dispatches the user_activity action at times when the user is interacting
24+
* with the app (but at a much lower frequency than mouse move events)
25+
*/
26+
class UserActivity {
27+
28+
/**
29+
* Start listening to user activity
30+
*/
31+
start() {
32+
document.onmousemove = this._onUserActivity.bind(this);
33+
document.onkeypress = this._onUserActivity.bind(this);
34+
this.lastActivityAt = (new Date).getTime();
35+
this.lastDispatchAt = 0;
36+
}
37+
38+
/**
39+
* Stop tracking user activity
40+
*/
41+
stop() {
42+
document.onmousemove = undefined;
43+
document.onkeypress = undefined;
44+
}
45+
46+
_onUserActivity() {
47+
this.lastActivityAt = (new Date).getTime();
48+
if (this.lastDispatchAt < this.lastActivityAt - MIN_DISPATCH_INTERVAL) {
49+
this.lastDispatchAt = this.lastActivityAt;
50+
dis.dispatch({
51+
action: 'user_activity'
52+
});
53+
}
54+
}
55+
}
56+
57+
module.exports = new UserActivity();

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
var MatrixClientPeg = require("../../MatrixClientPeg");
1818
var RoomListSorter = require("../../RoomListSorter");
19+
var UserActivity = require("../../UserActivity");
1920
var Presence = require("../../Presence");
2021
var dis = require("../../dispatcher");
2122

@@ -92,6 +93,7 @@ module.exports = {
9293
window.localStorage.clear();
9394
}
9495
Notifier.stop();
96+
UserActivity.stop();
9597
Presence.stop();
9698
MatrixClientPeg.get().stopClient();
9799
MatrixClientPeg.get().removeAllListeners();
@@ -316,6 +318,7 @@ module.exports = {
316318
});
317319
});
318320
Notifier.start();
321+
UserActivity.start();
319322
Presence.start();
320323
cli.startClient();
321324
},

0 commit comments

Comments
 (0)