summaryrefslogtreecommitdiff
path: root/ui/lib/watchdog.js
blob: c95fd4db8fa1655d020d2a275ec931b462d14c43 (plain)
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
export class Watchdog {
  constructor(onExpired) {
    this.timeout = null;
    this.onExpired = onExpired;
  }

  reset(delay) {
    if (this.timeout !== null) {
      clearTimeout(this.timeout);
    }
    this.timeout = setTimeout(this.expire.bind(this), delay);
  }

  stop() {
    if (this.timeout !== null) {
      clearTimeout(this.timeout);
      this.timeout = null;
    }
  }

  expire() {
    if (this.timeout !== null) {
      this.timeout = null;
    }
    this.onExpired();
  }
}