2021-04-13 10:57:08 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-02-09 04:38:04 +01:00
|
|
|
|
2021-08-07 01:59:28 +02:00
|
|
|
export class ChangeTimer {
|
|
|
|
private value: number | null = null;
|
2021-09-16 14:47:05 +02:00
|
|
|
private action: (() => void) | null = null;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.fireImmediately = this.fireImmediately.bind(this);
|
|
|
|
}
|
2021-02-08 19:45:42 +01:00
|
|
|
|
2021-08-07 23:38:21 +02:00
|
|
|
schedule(action: () => void, delay: number): void {
|
2021-08-07 01:59:28 +02:00
|
|
|
this.clear();
|
2021-09-16 14:47:05 +02:00
|
|
|
this.action = action;
|
|
|
|
this.value = setTimeout(this.fireImmediately, delay);
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
2021-08-07 23:38:21 +02:00
|
|
|
clear(): void {
|
2021-08-07 01:59:28 +02:00
|
|
|
if (this.value) {
|
|
|
|
clearTimeout(this.value);
|
|
|
|
this.value = null;
|
|
|
|
}
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
2021-09-16 14:47:05 +02:00
|
|
|
|
|
|
|
fireImmediately(): void {
|
|
|
|
if (this.action) {
|
|
|
|
this.action();
|
|
|
|
this.action = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clear();
|
|
|
|
}
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|