* Calls the callback when document's state satisfies the stateFn. * @param {Document} doc * @param {function(Document):boolean} stateFn * @param {function(Document):void} callback
(doc, stateFn, callback)
| 38 | * @param {function(Document):void} callback |
| 39 | */ |
| 40 | function onDocumentState(doc, stateFn, callback) { |
| 41 | let ready = stateFn(doc); |
| 42 | if (ready) { |
| 43 | callback(doc); |
| 44 | } else { |
| 45 | const readyListener = () => { |
| 46 | if (stateFn(doc)) { |
| 47 | if (!ready) { |
| 48 | ready = true; |
| 49 | callback(doc); |
| 50 | } |
| 51 | doc.removeEventListener('readystatechange', readyListener); |
| 52 | } |
| 53 | }; |
| 54 | doc.addEventListener('readystatechange', readyListener); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns a promise that is resolved when document is ready. |
no test coverage detected