MCPcopy
hub / github.com/ampproject/amphtml / layoutCallback

Method layoutCallback

src/custom-element.js:1623–1681  ·  view source on GitHub ↗

* Instructs the element to layout its content and load its resources if * necessary by calling the BaseElement.layoutCallback method that * should be implemented by BaseElement subclasses. Must return a promise * that will yield when the layout and associated loadings are comp

(signal)

Source from the content-addressed store, hash-verified

1621 * TODO(#31915): remove once R1 migration is complete.
1622 */
1623 layoutCallback(signal) {
1624 assertNotTemplate(this);
1625 devAssert(this.isBuilt(), 'Must be built to receive viewport events');
1626 // A lot of tests call layoutCallback manually, and don't pass a signal.
1627 if ((!getMode().test || signal) && signal.aborted) {
1628 return Promise.reject(cancellation());
1629 }
1630
1631 this.dispatchCustomEventForTesting(AmpEvents_Enum.LOAD_START);
1632 const isLoadEvent = this.layoutCount_ == 0; // First layout is "load".
1633 this.signals_.reset(CommonSignals_Enum.UNLOAD);
1634 if (isLoadEvent) {
1635 this.signals_.signal(CommonSignals_Enum.LOAD_START);
1636 }
1637
1638 // Potentially start the loading indicator.
1639 this.toggleLoading(true);
1640
1641 const promise = tryResolve(() => this.impl_.layoutCallback());
1642 this.preconnect(/* onLayout */ true);
1643 this.classList.add('i-amphtml-layout');
1644
1645 return promise.then(
1646 () => {
1647 if ((!getMode().test || signal) && signal.aborted) {
1648 throw cancellation();
1649 }
1650 if (isLoadEvent) {
1651 this.signals_.signal(CommonSignals_Enum.LOAD_END);
1652 }
1653 this.setReadyStateInternal(ReadyState_Enum.COMPLETE);
1654 this.layoutCount_++;
1655 this.toggleLoading(false);
1656 // Check if this is the first success layout that needs
1657 // to call firstLayoutCompleted.
1658 if (!this.isFirstLayoutCompleted_) {
1659 this.impl_.firstLayoutCompleted();
1660 this.isFirstLayoutCompleted_ = true;
1661 this.dispatchCustomEventForTesting(AmpEvents_Enum.LOAD_END);
1662 }
1663 },
1664 (reason) => {
1665 if ((!getMode().test || signal) && signal.aborted) {
1666 throw cancellation();
1667 }
1668 // add layoutCount_ by 1 despite load fails or not
1669 if (isLoadEvent) {
1670 this.signals_.rejectSignal(
1671 CommonSignals_Enum.LOAD_END,
1672 /** @type {!Error} */ (reason)
1673 );
1674 }
1675 this.setReadyStateInternal(ReadyState_Enum.ERROR, reason);
1676 this.layoutCount_++;
1677 this.toggleLoading(false);
1678 throw reason;
1679 }
1680 );

Callers

nothing calls this directly

Calls 15

isBuiltMethod · 0.95
toggleLoadingMethod · 0.95
preconnectMethod · 0.95
setReadyStateInternalMethod · 0.95
devAssertFunction · 0.90
getModeFunction · 0.90
cancellationFunction · 0.90
tryResolveFunction · 0.90
assertNotTemplateFunction · 0.85
rejectSignalMethod · 0.80
resetMethod · 0.45

Tested by

no test coverage detected