Add activity tracker

This commit is contained in:
2022-07-18 23:46:55 +00:00
parent 8fe408bcc6
commit f12cc164b6
6 changed files with 119 additions and 9 deletions

40
js/plugin/activity.ts Normal file
View File

@@ -0,0 +1,40 @@
export const ActivityMixin = (SuperClass) => {
return class ActivityMixinClass extends SuperClass {
activityTriggered = false;
_activityCooldown = 15000;
_activityTimeout;
constructor() {
super();
for (const ev of ["pointerdown", "pointermove", "keydown"]) {
window.addEventListener(ev, () => this.activityTrigger());
}
this.addEventListener("fully-update", () => {
this.activityTrigger();
});
}
activityTrigger() {
if (!this.activityTriggered) {
this.sendUpdate({
activity: true,
});
}
this.activityTriggered = true;
clearTimeout(this._activityTimeout);
this._activityTimeout = setTimeout(
() => this.activityReset(),
this._activityCooldown
);
}
activityReset() {
clearTimeout(this._activityTimeout);
if (this.activityTriggered) {
this.sendUpdate({
activity: false,
});
}
this.activityTriggered = false;
}
};
};