Skip to content

Commit

Permalink
feat(Ads): Allow multiple calls to requestAds in CS (#5542)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Aug 28, 2023
1 parent 4428adf commit 837e0fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/ads/client_side_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ shaka.ads.ClientSideAdManager = class {
this.adsRenderingSettings_ =
adsRenderingSettings || new google.ima.AdsRenderingSettings();

this.eventManager_.listenOnce(this.adsLoader_,
this.eventManager_.listen(this.adsLoader_,
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, (e) => {
this.onAdsManagerLoaded_(
/** @type {!google.ima.AdsManagerLoadedEvent} */ (e));
Expand Down Expand Up @@ -119,6 +119,17 @@ shaka.ads.ClientSideAdManager = class {
imaRequest.adTagUrl || imaRequest.adsResponse,
'The ad tag needs to be set up before requesting ads, ' +
'or adsResponse must be filled.');
// Destroy the current AdsManager, in case the tag you requested previously
// contains post-rolls (don't play those now).
if (this.imaAdsManager_) {
this.imaAdsManager_.destroy();
}
// Your AdsLoader will be set up on page-load. You should re-use the same
// AdsLoader for every request.
if (this.adsLoader_) {
// Reset the IMA SDK.
this.adsLoader_.contentComplete();
}
this.requestAdsStartTime_ = Date.now() / 1000;
this.adsLoader_.requestAds(imaRequest);
}
Expand Down
8 changes: 8 additions & 0 deletions test/ads/ad_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ describe('Ad manager', () => {
requestAds(imaRequest) {
numAdsRequested += 1;
}

contentComplete() {
// Nothing
}
};
window['google'].ima.AdsLoader = mockAdsLoader;

Expand All @@ -102,6 +106,10 @@ describe('Ad manager', () => {
/** @type {!google.ima.AdsManager} */ (this);
}

destroy() {
// Nothing
}

getCuePoints() {
return [];
}
Expand Down

0 comments on commit 837e0fb

Please sign in to comment.