* Calls the callback once when document's state satisfies the condition. * @param {!Document} doc * @param {function(!Document):boolean} condition * @param {function(!Document)} callback
(doc, condition, callback)
| 10612 | * @param {function(!Document)} callback |
| 10613 | */ |
| 10614 | function onDocumentState(doc, condition, callback) { |
| 10615 | if (condition(doc)) { |
| 10616 | // Execute callback right now. |
| 10617 | callback(doc); |
| 10618 | return; |
| 10619 | } |
| 10620 | |
| 10621 | // Execute callback (once!) after condition is satisfied. |
| 10622 | let callbackHasExecuted = false; |
| 10623 | const readyListener = () => { |
| 10624 | if (condition(doc) && !callbackHasExecuted) { |
| 10625 | callback(doc); |
| 10626 | callbackHasExecuted = true; |
| 10627 | doc.removeEventListener('readystatechange', readyListener); |
| 10628 | } |
| 10629 | }; |
| 10630 | doc.addEventListener('readystatechange', readyListener); |
| 10631 | } |
| 10632 | |
| 10633 | /** |
| 10634 | * Returns a promise that is resolved when document is ready. |
no test coverage detected