From a96c52b21e7e590bbdd70b80896780a446fa2e8b Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Mon, 13 Dec 2021 02:07:04 +0100 Subject: build: separate module for frontend This allows us to simplify the webpack configuration and the gradle build scripts. --- subprojects/frontend/src/utils/PendingTask.ts | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 subprojects/frontend/src/utils/PendingTask.ts (limited to 'subprojects/frontend/src/utils/PendingTask.ts') diff --git a/subprojects/frontend/src/utils/PendingTask.ts b/subprojects/frontend/src/utils/PendingTask.ts new file mode 100644 index 00000000..51b79fb0 --- /dev/null +++ b/subprojects/frontend/src/utils/PendingTask.ts @@ -0,0 +1,60 @@ +import { getLogger } from './logger'; + +const log = getLogger('utils.PendingTask'); + +export class PendingTask { + private readonly resolveCallback: (value: T) => void; + + private readonly rejectCallback: (reason?: unknown) => void; + + private resolved = false; + + private timeout: number | null; + + constructor( + resolveCallback: (value: T) => void, + rejectCallback: (reason?: unknown) => void, + timeoutMs?: number, + timeoutCallback?: () => void, + ) { + this.resolveCallback = resolveCallback; + this.rejectCallback = rejectCallback; + if (timeoutMs) { + this.timeout = setTimeout(() => { + if (!this.resolved) { + this.reject(new Error('Request timed out')); + if (timeoutCallback) { + timeoutCallback(); + } + } + }, timeoutMs); + } else { + this.timeout = null; + } + } + + resolve(value: T): void { + if (this.resolved) { + log.warn('Trying to resolve already resolved promise'); + return; + } + this.markResolved(); + this.resolveCallback(value); + } + + reject(reason?: unknown): void { + if (this.resolved) { + log.warn('Trying to reject already resolved promise'); + return; + } + this.markResolved(); + this.rejectCallback(reason); + } + + private markResolved() { + this.resolved = true; + if (this.timeout !== null) { + clearTimeout(this.timeout); + } + } +} -- cgit v1.2.3-70-g09d2