Skip to content

Commit

Permalink
amp-video-iframe: use InOb instead of ce.getIntersectionChangeEntry.
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri committed Dec 23, 2020
1 parent cc2202b commit d5e3fa4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 16 additions & 1 deletion extensions/amp-video-iframe/0.1/amp-video-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,27 @@ class AmpVideoIframe extends AMP.BaseElement {
}

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

/**
* @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
12 changes: 7 additions & 5 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 @@ -107,11 +107,13 @@ describes.realWin(
);
}

function stubIntersectionEntry(element, time, intersectionRatio) {
function stubPostIntersection(videoIframe, time, intersectionRatio) {
const entry = {time, intersectionRatio};
env.sandbox
./*OK*/ stub(element, 'getIntersectionChangeEntry')
.returns(entry);
.stub(videoIframe.implementation_, 'postIntersection_')
.callsFake((id) => {
videoIframe.implementation_.intersectionCallback_(id, [entry]);
});
return entry;
}

Expand Down Expand Up @@ -281,7 +283,7 @@ describes.realWin(

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

videoIframe.implementation_.onMessage_(message);
Expand All @@ -302,7 +304,7 @@ describes.realWin(

const postMessage = stubPostMessage(videoIframe);

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

acceptMockedMessages(videoIframe);

Expand Down

0 comments on commit d5e3fa4

Please sign in to comment.