diff --git a/extensions/amp-video-iframe/0.1/amp-video-iframe.js b/extensions/amp-video-iframe/0.1/amp-video-iframe.js index 24bd84793e451..cd92ff93552d2 100644 --- a/extensions/amp-video-iframe/0.1/amp-video-iframe.js +++ b/extensions/amp-video-iframe/0.1/amp-video-iframe.js @@ -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 */ @@ -126,6 +127,12 @@ class AmpVideoIframe extends AMP.BaseElement { * @private */ this.boundOnMessage_ = (e) => this.onMessage_(e); + + /** + * @param {!Element} element + * @return {!Promise} element + */ + this.measureIntersection = (element) => measureIntersection(element); } /** @override */ @@ -275,6 +282,7 @@ class AmpVideoIframe extends AMP.BaseElement { /** * @param {!Event} event + * @returns {Promise} * @private */ onMessage_(event) { @@ -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; @@ -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} 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 = diff --git a/extensions/amp-video-iframe/0.1/test/test-amp-video-iframe.js b/extensions/amp-video-iframe/0.1/test/test-amp-video-iframe.js index f9371a03549af..4aeb78fb429fd 100644 --- a/extensions/amp-video-iframe/0.1/test/test-amp-video-iframe.js +++ b/extensions/amp-video-iframe/0.1/test/test-amp-video-iframe.js @@ -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; } @@ -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; @@ -313,7 +311,7 @@ describes.realWin( const postMessage = stubPostMessage(videoIframe); - stubPostIntersection(videoIframe, time, intersectionRatio); + stubMeasureIntersection(videoIframe, time, intersectionRatio); acceptMockedMessages(videoIframe); @@ -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;