| |
| |
| |
| |
| |
| |
| export function randomID(l = 13): string { |
| return ( |
| "id_" + |
| Math.random() |
| .toString(36) |
| .substring(2, 2 + l) |
| ); |
| |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function waitForCondition( |
| conditionFunc: () => boolean, |
| checkFrequency = 100, |
| timeout = 0 |
| ): Promise<void> { |
| return new Promise((resolve, reject) => { |
| |
| if (conditionFunc()) { |
| resolve(); |
| return; |
| } |
|
|
| |
| const interval = setInterval(() => { |
| if (conditionFunc()) { |
| clearInterval(interval); |
| if (timeoutId) clearTimeout(timeoutId); |
| resolve(); |
| } |
| }, checkFrequency); |
|
|
| let timeoutId: number | null = null; |
| if (timeout > 0) { |
| timeoutId = window.setTimeout(() => { |
| clearInterval(interval); |
| reject( |
| new Error(`waitForCondition timed out after ${timeout}ms`) |
| ); |
| }, timeout); |
| } |
| }); |
| } |
|
|