-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity-monitor.js
43 lines (35 loc) · 1.09 KB
/
activity-monitor.js
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
import addEventListener from '@cfware/add-event-listener';
import runCallbacks from '@cfware/callback-array-once';
import cleanableInterval from '@cfware/cleanable-interval';
import activityEvents from './activity-events.js';
class ActivityMonitor {
_eventWatchers = [];
_activity = [false, false];
constructor(callback = () => {}) {
this._callback = callback;
}
get activity() {
return this._activity[0];
}
enable(interval = 10000) {
if (this._eventWatchers.length > 0) {
return;
}
const onactivity = () => {
this._activity = [true, true];
};
onactivity();
this._eventWatchers.push(
...activityEvents.map(event => addEventListener(window, event, onactivity)),
cleanableInterval(() => {
this._activity = [this._activity[1], false];
this._callback();
}, interval)
);
}
disable() {
runCallbacks(this._eventWatchers);
this._activity = [false, false];
}
}
export default ActivityMonitor;