-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
33 lines (28 loc) · 887 Bytes
/
utils.ts
File metadata and controls
33 lines (28 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Set of Misc utilities
*/
/**
* Timed semaphore
*/
export async function waitUntilFalse (callBackToReturnFalse: () => boolean, maxWaitMs: number = 5000): Promise<void> {
const started = Date.now();
while (callBackToReturnFalse()) {
await new Promise((resolve) => { setTimeout(resolve, 100); });
if (Date.now() - started > maxWaitMs) throw new Error(`Timeout after ${maxWaitMs}ms`);
}
}
/**
* Recursively make immutable an object
*/
export function deepFreeze<T extends object> (object: T): T {
// Retrieve the property names defined on object
const propNames = Reflect.ownKeys(object);
// Freeze properties before freezing self
for (const name of propNames) {
const value = (object as any)[name];
if ((value && typeof value === 'object') || typeof value === 'function') {
deepFreeze(value);
}
}
return Object.freeze(object);
}