Skip to content

Commit

Permalink
fix(ads): Fixed workflow when dealing with a list of Ads URLs; remove…
Browse files Browse the repository at this point in the history
…d unnecessary workflow to end media when Ad is completed
  • Loading branch information
rafa8626 committed Feb 15, 2020
1 parent d89f4a5 commit 66dddea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var instances = [];

for (var i = 0, total = players.length; i < total; i++) {
var ads = i === 0 ? [players[i].getAttribute('data-op-ads'),] : null;
var ads = i === 0 ? players[i].getAttribute('data-op-ads') : null;
instances[i] = new OpenPlayer(players[i].id, ads, false, {
ads: {
debug: false,
Expand Down
30 changes: 14 additions & 16 deletions dist/openplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8479,12 +8479,6 @@ var Ads = function () {
this.element.parentElement.classList.remove('op-ads--active');
this.adsActive = false;
clearInterval(this.intervalTimer);

if (this.element.currentTime >= this.element.duration) {
this.destroy();
var endedEvent = events_1.addEvent('ended');
this.element.dispatchEvent(endedEvent);
}
}

break;
Expand All @@ -8506,9 +8500,8 @@ var Ads = function () {
this.destroy();

if (this.element.currentTime >= this.element.duration) {
var _endedEvent = events_1.addEvent('ended');

this.element.dispatchEvent(_endedEvent);
var endedEvent = events_1.addEvent('ended');
this.element.dispatchEvent(endedEvent);
}
}

Expand Down Expand Up @@ -8657,15 +8650,20 @@ var Ads = function () {
}, {
key: "_loadedMetadataHandler",
value: function _loadedMetadataHandler() {
if (Array.isArray(this.ads) && this.currentAdsIndex <= this.ads.length - 1) {
this.adsManager.destroy();
this.adsLoader.contentComplete();
if (Array.isArray(this.ads)) {
this.currentAdsIndex++;
this.playTriggered = true;
this.adsStarted = true;
this.adsDone = false;

this._requestAds();
if (this.currentAdsIndex <= this.ads.length - 1) {
this.adsManager.destroy();
this.adsLoader.contentComplete();
this.playTriggered = true;
this.adsStarted = true;
this.adsDone = false;

this._requestAds();
} else {
this._prepareMedia();
}
} else if (this.element.seekable.length) {
if (this.element.seekable.end(0) > this.lastTimePaused) {
this._prepareMedia();
Expand Down
2 changes: 1 addition & 1 deletion dist/openplayer.min.js

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions src/js/media/ads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,6 @@ class Ads {
this.element.parentElement.classList.remove('op-ads--active');
this.adsActive = false;
clearInterval(this.intervalTimer);
if (this.element.currentTime >= this.element.duration) {
this.destroy();
const endedEvent = addEvent('ended');
this.element.dispatchEvent(endedEvent);
}
}
break;
case google.ima.AdEvent.Type.VOLUME_CHANGED:
Expand Down Expand Up @@ -681,6 +676,7 @@ class Ads {
};
const errorEvent = addEvent('playererror', details);
this.element.dispatchEvent(errorEvent);

if (Array.isArray(this.ads) && this.ads.length > 1 && this.currentAdsIndex <= this.ads.length - 1) {
if (this.currentAdsIndex < this.ads.length - 1) {
this.currentAdsIndex++;
Expand Down Expand Up @@ -852,14 +848,18 @@ class Ads {
* @memberof Ads
*/
private _loadedMetadataHandler() {
if (Array.isArray(this.ads) && this.currentAdsIndex <= this.ads.length - 1) {
this.adsManager.destroy();
this.adsLoader.contentComplete();
if (Array.isArray(this.ads)) {
this.currentAdsIndex++;
this.playTriggered = true;
this.adsStarted = true;
this.adsDone = false;
this._requestAds();
if (this.currentAdsIndex <= this.ads.length - 1) {
this.adsManager.destroy();
this.adsLoader.contentComplete();
this.playTriggered = true;
this.adsStarted = true;
this.adsDone = false;
this._requestAds();
} else {
this._prepareMedia();
}
} else if (this.element.seekable.length) {
if (this.element.seekable.end(0) > this.lastTimePaused) {
this._prepareMedia();
Expand Down Expand Up @@ -889,14 +889,16 @@ class Ads {
};

const waitPromise = (ms: number, isReject: boolean) => new Promise((resolve, reject) => {
if (isReject) { return reject(); }
if (isReject) {
return reject();
}

setTimeout(resolve, ms);
});

waitPromise(50, this.media.ended)
.then(() => this.media.play().then(() => triggerEvent('play'))
.catch(() => triggerEvent('ended')));
.catch(() => triggerEvent('ended')));

}

Expand Down

0 comments on commit 66dddea

Please sign in to comment.