* 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)
| 64 | * @param {function(!Document)} callback |
| 65 | */ |
| 66 | function onDocumentState(doc, condition, callback) { |
| 67 | if (condition(doc)) { |
| 68 | // Execute callback right now. |
| 69 | callback(doc); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Execute callback (once!) after condition is satisfied. |
| 74 | let callbackHasExecuted = false; |
| 75 | const readyListener = () => { |
| 76 | if (condition(doc) && !callbackHasExecuted) { |
| 77 | callback(doc); |
| 78 | callbackHasExecuted = true; |
| 79 | doc.removeEventListener('readystatechange', readyListener); |
| 80 | } |
| 81 | }; |
| 82 | doc.addEventListener('readystatechange', readyListener); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns a promise that is resolved when document is ready. |
no test coverage detected