(description, condition, opt_onError, opt_timeout)
| 458 | * @template T |
| 459 | */ |
| 460 | export function poll(description, condition, opt_onError, opt_timeout) { |
| 461 | return new Promise((resolve, reject) => { |
| 462 | const start = Date.now(); |
| 463 | const end = opt_timeout || 1600; |
| 464 | function poll() { |
| 465 | const ret = condition(); |
| 466 | if (ret) { |
| 467 | clearInterval(interval); |
| 468 | resolve(ret); |
| 469 | } else { |
| 470 | if (Date.now() - start > end) { |
| 471 | clearInterval(interval); |
| 472 | if (opt_onError) { |
| 473 | reject(opt_onError()); |
| 474 | return; |
| 475 | } |
| 476 | reject(new Error('Timeout waiting for ' + description)); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | const interval = setInterval(poll, 8); |
| 481 | poll(); |
| 482 | }); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Polls for the given number of elements to have received layout or |
no test coverage detected