-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathactivity-watcher.js
More file actions
43 lines (34 loc) · 905 Bytes
/
activity-watcher.js
File metadata and controls
43 lines (34 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
var inherit = require('inherit');
var ActivityWatcher = inherit({
/**
* @param {number} timeout
* @param {function} fn
*/
__constructor: function (timeout, fn) {
this._timeout = timeout;
this._lastUpdate = null;
this._fn = fn;
this._timer = null;
this.update();
},
update: function () {
this._lastUpdate = Date.now();
},
start: function () {
if (this._timer) {
return;
}
var _this = this;
this._timer = setTimeout(function () {
clearTimeout(_this._timer);
_this._timer = null;
if ((Date.now() - _this._lastUpdate) >= _this._timeout) {
_this._fn();
} else {
_this.start();
}
}, _this._timeout / 2).unref();
},
});
module.exports = ActivityWatcher;