Skip to content

Commit

Permalink
Remove BaseElement.viewportCallback from ads. (#30859)
Browse files Browse the repository at this point in the history
* amp-ads / a4a: remove BaseElement.viewportCallback

* private --> protected

* fix doubleclick test

* protected fix, this.isInViewport fix, delay observe

* Update test-doubleclick-fluid.js

* make code fine with a test that calls layooutCallback twice in a row
  • Loading branch information
samouri committed Oct 30, 2020
1 parent d69a716 commit a7c8190
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
20 changes: 17 additions & 3 deletions extensions/amp-a4a/0.1/amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ import {installUrlReplacementsForEmbed} from '../../../src/service/url-replaceme
import {isAdPositionAllowed} from '../../../src/ad-helper';
import {isArray, isEnumValue, isObject} from '../../../src/types';
import {listenOnce} from '../../../src/event-helper';
import {
observeWithSharedInOb,
unobserveWithSharedInOb,
} from '../../../src/viewport-observer';
import {padStart} from '../../../src/string';
import {parseJson} from '../../../src/json';
import {processHead} from './head-validation';
Expand Down Expand Up @@ -367,6 +371,9 @@ export class AmpA4A extends AMP.BaseElement {
* @private {?function(!Element)}
*/
this.transferDomBody_ = null;

/** @private {function(boolean)} */
this.boundViewportCallback_ = this.viewportCallbackTemp.bind(this);
}

/** @override */
Expand Down Expand Up @@ -1235,7 +1242,9 @@ export class AmpA4A extends AMP.BaseElement {
if (this.isRefreshing) {
this.destroyFrame(true);
}
return this.attemptToRenderCreative();
return this.attemptToRenderCreative().then(() => {
observeWithSharedInOb(this.element, this.boundViewportCallback_);
});
}

/**
Expand Down Expand Up @@ -1326,6 +1335,7 @@ export class AmpA4A extends AMP.BaseElement {

/** @override */
unlayoutCallback() {
unobserveWithSharedInOb(this.element);
this.tearDownSlot();
return true;
}
Expand Down Expand Up @@ -1402,8 +1412,12 @@ export class AmpA4A extends AMP.BaseElement {
}
}

/** @override */
viewportCallback(inViewport) {
// TODO: Rename to viewportCallback once BaseElement.viewportCallback has been removed.
/**
* @param {boolean} inViewport
* @protected
*/
viewportCallbackTemp(inViewport) {
if (this.xOriginIframeHandler_) {
this.xOriginIframeHandler_.viewportCallback(inViewport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,8 @@ export class AmpAdNetworkDoubleclickImpl extends AmpA4A {
}

/** @override */
viewportCallback(inViewport) {
super.viewportCallback(inViewport);
viewportCallbackTemp(inViewport) {
super.viewportCallbackTemp(inViewport);
if (this.reattemptToExpandFluidCreative_ && !inViewport) {
// If the initial expansion attempt failed (e.g., the slot was within the
// viewport), then we will re-attempt to expand it here whenever the slot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ describes.realWin('DoubleClick Fast Fetch Fluid', realWinConfig, (env) => {
impl.isVerifiedAmpCreative_ = true;
impl.reattemptToExpandFluidCreative_ = true;
// Should do nothing
impl.viewportCallback(true);
impl.viewportCallbackTemp(true);
expect(attemptChangeHeightStub).to.not.be.called;
impl.viewportCallback(false);
impl.viewportCallbackTemp(false);
expect(attemptChangeHeightStub).to.be.calledOnce;
});

Expand Down
8 changes: 6 additions & 2 deletions extensions/amp-ad/0.1/amp-ad-xorigin-iframe-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export class AmpAdXOriginIframeHandler {
/** @private @const {!../../../src/service/viewport/viewport-interface.ViewportInterface} */
this.viewport_ = Services.viewportForDoc(this.baseInstance_.getAmpDoc());

/** @private {boolean} */
this.inViewport_ = false;

/** @private {boolean} */
this.sendPositionPending_ = false;
}
Expand Down Expand Up @@ -111,7 +114,7 @@ export class AmpAdXOriginIframeHandler {
this.iframe,
'send-embed-state',
true,
() => this.sendEmbedInfo_(this.baseInstance_.isInViewport())
() => this.sendEmbedInfo_(this.inViewport_)
);

// Enable creative position observer if inabox experiment enabled OR
Expand Down Expand Up @@ -197,7 +200,7 @@ export class AmpAdXOriginIframeHandler {

this.unlisteners_.push(
this.baseInstance_.getAmpDoc().onVisibilityChanged(() => {
this.sendEmbedInfo_(this.baseInstance_.isInViewport());
this.sendEmbedInfo_(this.inViewport_);
})
);

Expand Down Expand Up @@ -593,6 +596,7 @@ export class AmpAdXOriginIframeHandler {
* @param {boolean} inViewport
*/
viewportCallback(inViewport) {
this.inViewport_ = inViewport;
this.sendEmbedInfo_(inViewport);
}

Expand Down
9 changes: 0 additions & 9 deletions extensions/amp-fx-flying-carpet/0.1/amp-fx-flying-carpet.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ export class AmpFlyingCarpet extends AMP.BaseElement {
);
}

/** @override */
viewportCallback(inViewport) {
Services.ownersForDoc(this.element).updateInViewport(
this.element,
this.children_,
inViewport
);
}

/**
* Asserts that the flying carpet does not appear in the first or last
* viewport.
Expand Down

0 comments on commit a7c8190

Please sign in to comment.