aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/utils
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-08-25 00:14:51 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-08-25 00:14:51 +0200
commit71491e03468795404751d2edfe58ce78714a5ed1 (patch)
tree14131145cdd704b606f3677e61981be6ab921241 /subprojects/frontend/src/utils
parentfix(frontend): editor font thickness (diff)
downloadrefinery-71491e03468795404751d2edfe58ce78714a5ed1.tar.gz
refinery-71491e03468795404751d2edfe58ce78714a5ed1.tar.zst
refinery-71491e03468795404751d2edfe58ce78714a5ed1.zip
refactor(frontend): xtext update improvements
Diffstat (limited to 'subprojects/frontend/src/utils')
-rw-r--r--subprojects/frontend/src/utils/ConditionVariable.ts10
-rw-r--r--subprojects/frontend/src/utils/PendingTask.ts11
-rw-r--r--subprojects/frontend/src/utils/Timer.ts20
3 files changed, 20 insertions, 21 deletions
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');
6export type Condition = () => boolean; 6export type Condition = () => boolean;
7 7
8export default class ConditionVariable { 8export default class ConditionVariable {
9 condition: Condition; 9 private readonly condition: Condition;
10 10
11 defaultTimeout: number; 11 private readonly defaultTimeout: number;
12 12
13 listeners: PendingTask<void>[] = []; 13 private listeners: PendingTask<void>[] = [];
14 14
15 constructor(condition: Condition, defaultTimeout = 0) { 15 constructor(condition: Condition, defaultTimeout = 0) {
16 this.condition = condition; 16 this.condition = condition;
17 this.defaultTimeout = defaultTimeout; 17 this.defaultTimeout = defaultTimeout;
18 } 18 }
19 19
20 async waitFor(timeoutMs: number | null = null): Promise<void> { 20 async waitFor(timeoutMs?: number | undefined): Promise<void> {
21 if (this.condition()) { 21 if (this.condition()) {
22 return; 22 return;
23 } 23 }
24 const timeoutOrDefault = timeoutMs || this.defaultTimeout; 24 const timeoutOrDefault = timeoutMs ?? this.defaultTimeout;
25 let nowMs = Date.now(); 25 let nowMs = Date.now();
26 const endMs = nowMs + timeoutOrDefault; 26 const endMs = nowMs + timeoutOrDefault;
27 while (!this.condition() && nowMs < endMs) { 27 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<T> {
9 9
10 private resolved = false; 10 private resolved = false;
11 11
12 private timeout: number | null; 12 private timeout: number | undefined;
13 13
14 constructor( 14 constructor(
15 resolveCallback: (value: T) => void, 15 resolveCallback: (value: T) => void,
16 rejectCallback: (reason?: unknown) => void, 16 rejectCallback: (reason?: unknown) => void,
17 timeoutMs?: number, 17 timeoutMs?: number | undefined,
18 timeoutCallback?: () => void, 18 timeoutCallback?: () => void | undefined,
19 ) { 19 ) {
20 this.resolveCallback = resolveCallback; 20 this.resolveCallback = resolveCallback;
21 this.rejectCallback = rejectCallback; 21 this.rejectCallback = rejectCallback;
@@ -28,8 +28,6 @@ export default class PendingTask<T> {
28 } 28 }
29 } 29 }
30 }, timeoutMs); 30 }, timeoutMs);
31 } else {
32 this.timeout = null;
33 } 31 }
34 } 32 }
35 33
@@ -53,8 +51,9 @@ export default class PendingTask<T> {
53 51
54 private markResolved() { 52 private markResolved() {
55 this.resolved = true; 53 this.resolved = true;
56 if (this.timeout !== null) { 54 if (this.timeout !== undefined) {
57 clearTimeout(this.timeout); 55 clearTimeout(this.timeout);
56 this.timeout = undefined;
58 } 57 }
59 } 58 }
60} 59}
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 @@
1export default class Timer { 1export default class Timer {
2 readonly callback: () => void; 2 private readonly callback: () => void;
3 3
4 readonly defaultTimeout: number; 4 private readonly defaultTimeout: number;
5 5
6 timeout: number | null = null; 6 private timeout: number | undefined;
7 7
8 constructor(callback: () => void, defaultTimeout = 0) { 8 constructor(callback: () => void, defaultTimeout = 0) {
9 this.callback = () => { 9 this.callback = () => {
10 this.timeout = null; 10 this.timeout = undefined;
11 callback(); 11 callback();
12 }; 12 };
13 this.defaultTimeout = defaultTimeout; 13 this.defaultTimeout = defaultTimeout;
14 } 14 }
15 15
16 schedule(timeout: number | null = null): void { 16 schedule(timeout?: number | undefined): void {
17 if (this.timeout === null) { 17 if (this.timeout === undefined) {
18 this.timeout = setTimeout(this.callback, timeout || this.defaultTimeout); 18 this.timeout = setTimeout(this.callback, timeout ?? this.defaultTimeout);
19 } 19 }
20 } 20 }
21 21
22 reschedule(timeout: number | null = null): void { 22 reschedule(timeout?: number | undefined): void {
23 this.cancel(); 23 this.cancel();
24 this.schedule(timeout); 24 this.schedule(timeout);
25 } 25 }
26 26
27 cancel(): void { 27 cancel(): void {
28 if (this.timeout !== null) { 28 if (this.timeout !== undefined) {
29 clearTimeout(this.timeout); 29 clearTimeout(this.timeout);
30 this.timeout = null; 30 this.timeout = undefined;
31 } 31 }
32 } 32 }
33} 33}