MCPcopy Create free account
hub / github.com/ampproject/amphtml / onDocumentState

Function onDocumentState

third_party/subscriptions-project/swg.js:10614–10631  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

10612 * @param {function(!Document)} callback
10613 */
10614function 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.

Callers 1

onDocumentReadyFunction · 0.70

Calls 2

callbackFunction · 0.50
addEventListenerMethod · 0.45

Tested by

no test coverage detected