(predicateBody, polling, timeout, ...args)
| 1 | async function waitForPredicatePageFunction(predicateBody, polling, timeout, ...args) { |
| 2 | const predicate = new Function('...args', predicateBody); |
| 3 | let timedOut = false; |
| 4 | if (timeout) |
| 5 | setTimeout(() => (timedOut = true), timeout); |
| 6 | if (polling === 'raf') |
| 7 | return await pollRaf(); |
| 8 | if (polling === 'mutation') |
| 9 | return await pollMutation(); |
| 10 | if (typeof polling === 'number') |
| 11 | return await pollInterval(polling); |
| 12 | |
| 13 | /** |
| 14 | * @returns {!Promise<*>} |
| 15 | */ |
| 16 | async function pollMutation() { |
| 17 | const success = await predicate(...args); |
| 18 | if (success) |
| 19 | return Promise.resolve(success); |
| 20 | let fulfill; |
| 21 | const result = new Promise((x) => (fulfill = x)); |
| 22 | const observer = new MutationObserver(async () => { |
| 23 | if (timedOut) { |
| 24 | observer.disconnect(); |
| 25 | fulfill(); |
| 26 | } |
| 27 | const success = await predicate(...args); |
| 28 | if (success) { |
| 29 | observer.disconnect(); |
| 30 | fulfill(success); |
| 31 | } |
| 32 | }); |
| 33 | observer.observe(document, { |
| 34 | childList: true, |
| 35 | subtree: true, |
| 36 | attributes: true, |
| 37 | }); |
| 38 | if (timeout) |
| 39 | setTimeout(() => { |
| 40 | observer.disconnect(); |
| 41 | fulfill(); |
| 42 | }, timeout); |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | async function pollRaf() { |
| 47 | let fulfill; |
| 48 | const result = new Promise((x) => (fulfill = x)); |
| 49 | await onRaf(); |
| 50 | return result; |
| 51 | |
| 52 | async function onRaf() { |
| 53 | if (timedOut) { |
| 54 | fulfill(); |
| 55 | return; |
| 56 | } |
| 57 | const success = await predicate(...args); |
| 58 | if (success) |
| 59 | fulfill(success); |
| 60 | else |
nothing calls this directly
no test coverage detected