(isReadyFn, delay, timeout)
| 295 | } |
| 296 | |
| 297 | async function waitUntil(isReadyFn, delay, timeout) { |
| 298 | if (typeof delay === "number") { |
| 299 | const msDelay = delay |
| 300 | delay = () => asyncDelay(msDelay) |
| 301 | } |
| 302 | if (typeof delay !== "function") { |
| 303 | throw new Error("delay is not a number or a function.") |
| 304 | } |
| 305 | if (typeof timeout !== "undefined" && typeof timeout !== "number") { |
| 306 | throw new Error("timeout is not a number.") |
| 307 | } |
| 308 | if (typeof timeout === "undefined" || timeout < 0) { |
| 309 | timeout = Number.MAX_SAFE_INTEGER |
| 310 | } |
| 311 | timeout = Date.now() + timeout |
| 312 | while ( |
| 313 | timeout > Date.now() && |
| 314 | Date.now() < serverState.time + SERVER_STATE_VALIDITY_DURATION && |
| 315 | !Boolean(await Promise.resolve(isReadyFn())) |
| 316 | ) { |
| 317 | await delay() |
| 318 | if (!isServerAvailable()) { |
| 319 | // Can fail if ping got frozen/suspended... |
| 320 | if ((await healthCheck()) && isServerAvailable()) { |
| 321 | // Force a recheck of server status before failure... |
| 322 | continue // Continue waiting if last healthCheck confirmed the server is still alive. |
| 323 | } |
| 324 | throw new Error("Connection with server lost.") |
| 325 | } |
| 326 | } |
| 327 | if (Date.now() >= serverState.time + SERVER_STATE_VALIDITY_DURATION) { |
| 328 | console.warn("SERVER_STATE_VALIDITY_DURATION elapsed. Released waitUntil on stale server state.") |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | const TaskStatus = { |
| 333 | init: "init", |
no test coverage detected