Skip to content

Commit

Permalink
fix(Ads): Fix duplicate interstitials when using src= (#6784)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jun 11, 2024
1 parent 309bd72 commit 3949686
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/ads/interstitial_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ shaka.ads.InterstitialAdManager = class {
/** @private {function(!shaka.util.FakeEvent)} */
this.onEvent_ = onEvent;

/** @private {!Set.<string>} */
this.interstitialIds_ = new Set();

/** @private {!Set.<shaka.ads.InterstitialAdManager.Interstitial>} */
this.interstitials_ = new Set();

Expand Down Expand Up @@ -113,6 +116,10 @@ shaka.ads.InterstitialAdManager = class {
}
this.preloadManagerInterstitials_.delete(interstitial);
}
const interstitialId = JSON.stringify(interstitial);
if (this.interstitialIds_.has(interstitialId)) {
this.interstitialIds_.delete(interstitialId);
}
this.interstitials_.delete(interstitial);
} else {
const difference = interstitial.startTime - this.lastTime_;
Expand Down Expand Up @@ -164,6 +171,7 @@ shaka.ads.InterstitialAdManager = class {
if (this.adEventManager_) {
this.adEventManager_.removeAll();
}
this.interstitialIds_.clear();
this.interstitials_.clear();
this.player_.destroyAllPreloads();
this.preloadManagerInterstitials_.clear();
Expand Down Expand Up @@ -203,6 +211,11 @@ shaka.ads.InterstitialAdManager = class {
const interstitialsAd = await this.getInterstitialsInfo_(interstitial);
if (interstitialsAd.length) {
for (const interstitialAd of interstitialsAd) {
const interstitialId = JSON.stringify(interstitialAd);
if (this.interstitialIds_.has(interstitialId)) {
continue;
}
this.interstitialIds_.add(interstitialId);
this.interstitials_.add(interstitialAd);
if (this.lastTime_ != null) {
const difference = interstitial.startTime - this.lastTime_;
Expand Down Expand Up @@ -248,8 +261,12 @@ shaka.ads.InterstitialAdManager = class {
const interstitials = Array.from(this.interstitials_).sort((a, b) => {
return b.startTime - a.startTime;
});
const roundDecimals = (number) => {
return Math.round(number * 1000) / 1000;
};
for (const interstitial of interstitials) {
const difference = this.lastTime_ - interstitial.startTime;
const difference =
this.lastTime_ - roundDecimals(interstitial.startTime);
if (difference > 0 && (difference <= 1 || !interstitial.canJump)) {
currentInterstitial = interstitial;
break;
Expand All @@ -276,7 +293,7 @@ shaka.ads.InterstitialAdManager = class {

if (adPosition == 1 && sequenceLength == 1) {
sequenceLength = Array.from(this.interstitials_).filter((i) => {
return i.startTime == interstitial.startTime;
return Math.abs(i.startTime - interstitial.startTime) < 0.001;
}).length;
}

Expand Down

0 comments on commit 3949686

Please sign in to comment.