Skip to content

Commit

Permalink
update to use new helper
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri committed Jan 8, 2021
1 parent aae868c commit facc946
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
33 changes: 14 additions & 19 deletions extensions/amp-video-iframe/0.1/amp-video-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import {getData, listen} from '../../../src/event-helper';
import {installVideoManagerForDoc} from '../../../src/service/video-manager-impl';
import {isLayoutSizeDefined} from '../../../src/layout';
import {measureIntersection} from '../../../src/utils/intersection';
import {once} from '../../../src/utils/function';

/** @private @const */
Expand Down Expand Up @@ -126,6 +127,12 @@ class AmpVideoIframe extends AMP.BaseElement {
* @private
*/
this.boundOnMessage_ = (e) => this.onMessage_(e);

/**
* @param {!Element} element
* @return {!Promise<IntersectionObserverEntry>} element
*/
this.measureIntersection = (element) => measureIntersection(element);
}

/** @override */
Expand Down Expand Up @@ -275,6 +282,7 @@ class AmpVideoIframe extends AMP.BaseElement {

/**
* @param {!Event} event
* @returns {Promise}
* @private
*/
onMessage_(event) {
Expand Down Expand Up @@ -302,8 +310,9 @@ class AmpVideoIframe extends AMP.BaseElement {

if (methodReceived) {
if (methodReceived == 'getIntersection') {
this.postIntersection_(messageId);
return;
return this.measureIntersection(this.element).then((intersection) => {
this.postIntersection_(messageId, intersection);
});
}
userAssert(false, 'Unknown method `%s`.', methodReceived);
return;
Expand Down Expand Up @@ -360,27 +369,13 @@ class AmpVideoIframe extends AMP.BaseElement {
}

/**
* Creates an IntersectionObserver to fire a single intersection event.
*
* @param {number} messageId
* @param {!IntersectionObserverEntry} intersection
* @private
*/
postIntersection_(messageId) {
const intersectionObserver = new IntersectionObserver((entries) => {
this.intersectionCallback_(messageId, entries);
intersectionObserver.disconnect();
});
intersectionObserver.observe(this.element);
}
postIntersection_(messageId, intersection) {
const {intersectionRatio, time} = intersection;

/**
* @param {number} messageId
* @param {Array<IntersectionObserverEntry>} entries
* @private
*/
intersectionCallback_(messageId, entries) {
const lastEntry = entries[entries.length - 1];
const {time, intersectionRatio} = lastEntry;
// Only post ratio > 0 when in autoplay range to prevent internal autoplay
// implementations that differ from ours.
const postedRatio =
Expand Down
16 changes: 7 additions & 9 deletions extensions/amp-video-iframe/0.1/test/test-amp-video-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,11 @@ describes.realWin(
);
}

function stubPostIntersection(videoIframe, time, intersectionRatio) {
function stubMeasureIntersection(videoIframe, time, intersectionRatio) {
const entry = {time, intersectionRatio};
env.sandbox
.stub(videoIframe.implementation_, 'postIntersection_')
.callsFake((id) => {
videoIframe.implementation_.intersectionCallback_(id, [entry]);
});
.stub(videoIframe.implementation_, 'measureIntersection')
.returns(Promise.resolve(entry));
return entry;
}

Expand Down Expand Up @@ -292,10 +290,10 @@ describes.realWin(

const expectedResponseMessage = {
id,
args: stubPostIntersection(videoIframe, time, intersectionRatio),
args: stubMeasureIntersection(videoIframe, time, intersectionRatio),
};

videoIframe.implementation_.onMessage_(message);
await videoIframe.implementation_.onMessage_(message);

expect(postMessage.withArgs(env.sandbox.match(expectedResponseMessage)))
.to.have.been.calledOnce;
Expand All @@ -313,7 +311,7 @@ describes.realWin(

const postMessage = stubPostMessage(videoIframe);

stubPostIntersection(videoIframe, time, intersectionRatio);
stubMeasureIntersection(videoIframe, time, intersectionRatio);

acceptMockedMessages(videoIframe);

Expand All @@ -327,7 +325,7 @@ describes.realWin(
},
};

videoIframe.implementation_.onMessage_(message);
await videoIframe.implementation_.onMessage_(message);

expect(postMessage.withArgs(env.sandbox.match(expectedResponseMessage)))
.to.have.been.calledOnce;
Expand Down

0 comments on commit facc946

Please sign in to comment.