From 232fbcafa863a3c28ab907b112c5257f0b6dc8f1 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 26 Oct 2021 21:40:36 +0200 Subject: chore(web): refactor websocket state machine --- language-web/src/main/js/utils/Timer.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 language-web/src/main/js/utils/Timer.ts (limited to 'language-web/src/main/js/utils') diff --git a/language-web/src/main/js/utils/Timer.ts b/language-web/src/main/js/utils/Timer.ts new file mode 100644 index 00000000..efde6633 --- /dev/null +++ b/language-web/src/main/js/utils/Timer.ts @@ -0,0 +1,33 @@ +export class Timer { + readonly callback: () => void; + + readonly defaultTimeout: number; + + timeout: NodeJS.Timeout | null = null; + + constructor(callback: () => void, defaultTimeout = 0) { + this.callback = () => { + this.timeout = null; + callback(); + }; + this.defaultTimeout = defaultTimeout; + } + + schedule(timeout: number | null = null): void { + if (this.timeout === null) { + this.timeout = setTimeout(this.callback, timeout || this.defaultTimeout); + } + } + + reschedule(timeout: number | null = null): void { + this.cancel(); + this.schedule(timeout); + } + + cancel(): void { + if (this.timeout !== null) { + clearTimeout(this.timeout); + this.timeout = null; + } + } +} -- cgit v1.2.3-54-g00ecf