X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=851ba4318fc337474875025a85d900799ce2ebbf;hb=975570b708efc2b28b8040ecf5cdb6eb45a1b55a;hp=92f687d85302cf0cd56949fc800b899390459f47;hpb=b40b5cb39eb44088100f2ce5efc2a6fdf4b84f26;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 92f687d8..851ba431 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,47 +1,50 @@ -export default class Utils { - // STATE - public static isUndefined(value: unknown): boolean { - return typeof value === 'undefined'; - } +const isUndefined = (value: unknown): boolean => { + return typeof value === 'undefined'; +}; - public static ifUndefined(value: T | undefined, isValue: T): T { - if (Utils.isUndefined(value) === true) return isValue; - return value as T; - } +export const ifUndefined = (value: T | undefined, isValue: T): T => { + if (isUndefined(value) === true) return isValue; + return value as T; +}; - public static isIterable>(obj: T): boolean { - if (obj === null || obj === undefined) { - return false; - } - return typeof obj[Symbol.iterator] === 'function'; - } +// const isIterable = (obj: T): boolean => { +// if (obj === null || obj === undefined) { +// return false; +// } +// return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function'; +// }; - // public static ifNotIterableDo(obj: T, cb: () => void): void { - // if (this.isIterable(obj) === false) cb(); - // } +// const ifNotIterableDo = (obj: T, cb: () => void): void => { +// if (isIterable(obj) === false) cb(); +// }; - public static async promiseWithTimeout( - promise: Promise, - timeoutMs: number, - timeoutError: Error, - timeoutCallback: () => void = () => { - /* This is intentional */ - } - ): Promise { - // Create a timeout promise that rejects in timeout milliseconds - const timeoutPromise = new Promise((_, reject) => { - setTimeout(() => { +const isPromisePending = (promise: Promise): boolean => { + return util.inspect(promise).includes('pending'); +}; + +export const promiseWithTimeout = ( + promise: Promise, + timeoutMs: number, + timeoutError: Error, + timeoutCallback: () => void = () => { + /* This is intentional */ + }, +): Promise => { + // Create a timeout promise that rejects in timeout milliseconds + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + if (isPromisePending(promise)) { timeoutCallback(); - reject(timeoutError); - }, timeoutMs); - }); + // FIXME: The original promise shall be canceled + } + reject(timeoutError); + }, timeoutMs); + }); - // Returns a race between timeout promise and the passed promise - return Promise.race([promise, timeoutPromise]); - } + // Returns a race between timeout promise and the passed promise + return Promise.race([promise, timeoutPromise]); +}; - // FUNCTIONAL - // public static compose(...fns: ((arg: T) => T)[]): (x: T) => T { - // return (x: T) => fns.reduceRight((y, fn) => fn(y), x); - // } -} +// export const compose = (...fns: ((arg: T) => T)[]): ((x: T) => T) => { +// return (x: T) => fns.reduceRight((y, fn) => fn(y), x); +// };