@@ -15,58 +15,54 @@ limitations under the License.
1515*/
1616
1717var MatrixClientPeg = require ( "./MatrixClientPeg" ) ;
18+ var dis = require ( "./dispatcher" ) ;
1819
1920 // Time in ms after that a user is considered as unavailable/away
2021var UNAVAILABLE_TIME_MS = 3 * 60 * 1000 ; // 3 mins
2122var 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 ( ) ;
0 commit comments