From 71491e03468795404751d2edfe58ce78714a5ed1 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 25 Aug 2022 00:14:51 +0200 Subject: refactor(frontend): xtext update improvements --- subprojects/frontend/src/utils/ConditionVariable.ts | 10 +++++----- subprojects/frontend/src/utils/PendingTask.ts | 11 +++++------ subprojects/frontend/src/utils/Timer.ts | 20 ++++++++++---------- 3 files changed, 20 insertions(+), 21 deletions(-) (limited to 'subprojects/frontend/src/utils') diff --git a/subprojects/frontend/src/utils/ConditionVariable.ts b/subprojects/frontend/src/utils/ConditionVariable.ts index c8fae9e8..1d3431f7 100644 --- a/subprojects/frontend/src/utils/ConditionVariable.ts +++ b/subprojects/frontend/src/utils/ConditionVariable.ts @@ -6,22 +6,22 @@ const log = getLogger('utils.ConditionVariable'); export type Condition = () => boolean; export default class ConditionVariable { - condition: Condition; + private readonly condition: Condition; - defaultTimeout: number; + private readonly defaultTimeout: number; - listeners: PendingTask[] = []; + private listeners: PendingTask[] = []; constructor(condition: Condition, defaultTimeout = 0) { this.condition = condition; this.defaultTimeout = defaultTimeout; } - async waitFor(timeoutMs: number | null = null): Promise { + async waitFor(timeoutMs?: number | undefined): Promise { if (this.condition()) { return; } - const timeoutOrDefault = timeoutMs || this.defaultTimeout; + const timeoutOrDefault = timeoutMs ?? this.defaultTimeout; let nowMs = Date.now(); const endMs = nowMs + timeoutOrDefault; while (!this.condition() && nowMs < endMs) { diff --git a/subprojects/frontend/src/utils/PendingTask.ts b/subprojects/frontend/src/utils/PendingTask.ts index 086993d4..3976bdf9 100644 --- a/subprojects/frontend/src/utils/PendingTask.ts +++ b/subprojects/frontend/src/utils/PendingTask.ts @@ -9,13 +9,13 @@ export default class PendingTask { private resolved = false; - private timeout: number | null; + private timeout: number | undefined; constructor( resolveCallback: (value: T) => void, rejectCallback: (reason?: unknown) => void, - timeoutMs?: number, - timeoutCallback?: () => void, + timeoutMs?: number | undefined, + timeoutCallback?: () => void | undefined, ) { this.resolveCallback = resolveCallback; this.rejectCallback = rejectCallback; @@ -28,8 +28,6 @@ export default class PendingTask { } } }, timeoutMs); - } else { - this.timeout = null; } } @@ -53,8 +51,9 @@ export default class PendingTask { private markResolved() { this.resolved = true; - if (this.timeout !== null) { + if (this.timeout !== undefined) { clearTimeout(this.timeout); + this.timeout = undefined; } } } diff --git a/subprojects/frontend/src/utils/Timer.ts b/subprojects/frontend/src/utils/Timer.ts index 14e9eb81..4bb1bb9c 100644 --- a/subprojects/frontend/src/utils/Timer.ts +++ b/subprojects/frontend/src/utils/Timer.ts @@ -1,33 +1,33 @@ export default class Timer { - readonly callback: () => void; + private readonly callback: () => void; - readonly defaultTimeout: number; + private readonly defaultTimeout: number; - timeout: number | null = null; + private timeout: number | undefined; constructor(callback: () => void, defaultTimeout = 0) { this.callback = () => { - this.timeout = null; + this.timeout = undefined; callback(); }; this.defaultTimeout = defaultTimeout; } - schedule(timeout: number | null = null): void { - if (this.timeout === null) { - this.timeout = setTimeout(this.callback, timeout || this.defaultTimeout); + schedule(timeout?: number | undefined): void { + if (this.timeout === undefined) { + this.timeout = setTimeout(this.callback, timeout ?? this.defaultTimeout); } } - reschedule(timeout: number | null = null): void { + reschedule(timeout?: number | undefined): void { this.cancel(); this.schedule(timeout); } cancel(): void { - if (this.timeout !== null) { + if (this.timeout !== undefined) { clearTimeout(this.timeout); - this.timeout = null; + this.timeout = undefined; } } } -- cgit v1.2.3-54-g00ecf