X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=cf4baf5d5fe9c8b569359d248daea9b82160e917;hb=f568f36861d62d759a95b17fbe10380eca88a71b;hp=ac217a70746088ac9e53e6f6ed5ec7ecf755e0c4;hpb=3c8798b163014a2f28c3e559bf1685bdc61fd3e0;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index ac217a70..cf4baf5d 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,56 +1,52 @@ import util from 'node:util'; -export default class Utils { - // STATE - public static isUndefined(value: unknown): boolean { - return typeof value === 'undefined'; +const isUndefined = (value: unknown): boolean => { + return typeof value === 'undefined'; +}; + +export const ifUndefined = (value: T | undefined, isValue: T): T => { + if (isUndefined(value) === true) return isValue; + return value as T; +}; + +// const isIterable = (obj: T): boolean => { +// if (obj === null || obj === undefined) { +// return false; +// } +// return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function'; +// }; + +// const ifNotIterableDo = (obj: T, cb: () => void): void => { +// if (isIterable(obj) === false) cb(); +// }; + +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 */ } - - public static ifUndefined(value: T | undefined, isValue: T): T { - if (Utils.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 as unknown as Iterable)[Symbol.iterator] === 'function'; - } - - // public static ifNotIterableDo(obj: T, cb: () => void): void { - // if (this.isIterable(obj) === false) cb(); - // } - - public static isPromisePending(promise: Promise): boolean { - return util.inspect(promise).includes('pending'); - } - - 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(() => { - if (Utils.isPromisePending(promise)) { - timeoutCallback(); - // 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]); - } - - // FUNCTIONAL - // public static compose(...fns: ((arg: T) => T)[]): (x: T) => T { - // return (x: T) => fns.reduceRight((y, fn) => fn(y), x); - // } -} +): Promise => { + // Create a timeout promise that rejects in timeout milliseconds + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + if (isPromisePending(promise)) { + timeoutCallback(); + // 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]); +}; + +// export const compose = (...fns: ((arg: T) => T)[]): ((x: T) => T) => { +// return (x: T) => fns.reduceRight((y, fn) => fn(y), x); +// };