fix(ui): util module is not available in browser
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / Utils.ts
CommitLineData
f568f368
JB
1const isUndefined = (value: unknown): boolean => {
2 return typeof value === 'undefined';
3};
4
5export const ifUndefined = <T>(value: T | undefined, isValue: T): T => {
6 if (isUndefined(value) === true) return isValue;
7 return value as T;
8};
9
10// const isIterable = <T>(obj: T): boolean => {
11// if (obj === null || obj === undefined) {
12// return false;
13// }
14// return typeof (obj as unknown as Iterable<T>)[Symbol.iterator] === 'function';
15// };
16
17// const ifNotIterableDo = <T>(obj: T, cb: () => void): void => {
18// if (isIterable(obj) === false) cb();
19// };
20
f568f368
JB
21export const promiseWithTimeout = <T>(
22 promise: Promise<T>,
23 timeoutMs: number,
24 timeoutError: Error,
25 timeoutCallback: () => void = () => {
26 /* This is intentional */
f3095697 27 },
f568f368
JB
28): Promise<T> => {
29 // Create a timeout promise that rejects in timeout milliseconds
30 const timeoutPromise = new Promise<never>((_, reject) => {
31 setTimeout(() => {
6ebeab4b
JB
32 // FIXME: The original promise state shall be checked
33 timeoutCallback();
34 // FIXME: The original promise shall be canceled
f568f368
JB
35 reject(timeoutError);
36 }, timeoutMs);
37 });
38
39 // Returns a race between timeout promise and the passed promise
40 return Promise.race<T>([promise, timeoutPromise]);
41};
42
43// export const compose = <T>(...fns: ((arg: T) => T)[]): ((x: T) => T) => {
44// return (x: T) => fns.reduceRight((y, fn) => fn(y), x);
45// };