Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move public winningBids to auction #1828

Merged
merged 4 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels})
let _callback = callback;
let _timer;
let _timeout = cbTimeout;
let _winningBid;

function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests) };
function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); }
Expand Down Expand Up @@ -196,6 +197,8 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels})
addBidReceived,
executeCallback,
callBids,
setWinningBid: (winningBid) => { _winningBid = winningBid },
getWinningBid: () => _winningBid,
getTimeout: () => _timeout,
getAuctionId: () => _auctionId,
getAuctionStatus: () => _auctionStatus,
Expand Down
14 changes: 14 additions & 0 deletions src/auctionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export function newAuctionManager() {
let _auctions = [];
let _public = {};

_public.addWinningBid = function(bid) {
const auction = _auctions.find(auction => auction.getAuctionId() === bid.auctionId);
if (auction) {
auction.setWinningBid(bid);
} else {
utils.logWarn(`Auction not found when adding winning bid`);
}
}

_public.getAllWinningBids = function() {
return _auctions.map(auction => auction.getWinningBid())
.reduce(flatten, []);
}

_public.getBidsRequested = function() {
return _auctions.map(auction => auction.getBidRequests())
.reduce(flatten, []);
Expand Down
6 changes: 2 additions & 4 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ var eventValidators = {
};

/* Public vars */
$$PREBID_GLOBAL$$._winningBids = [];

$$PREBID_GLOBAL$$.bidderSettings = $$PREBID_GLOBAL$$.bidderSettings || {};

// current timeout set in `requestBids` or to default `bidderTimeout`
Expand Down Expand Up @@ -235,7 +233,7 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) {
bid.ad = utils.replaceAuctionPrice(bid.ad, bid.cpm);
bid.url = utils.replaceAuctionPrice(bid.url, bid.cpm);
// save winning bids
$$PREBID_GLOBAL$$._winningBids.push(bid);
auctionManager.addWinningBid(bid);

// emit 'bid won' event here
events.emit(BID_WON, bid);
Expand Down Expand Up @@ -545,7 +543,7 @@ $$PREBID_GLOBAL$$.aliasBidder = function (bidderCode, alias) {
* @return {Array<AdapterBidResponse>} A list of bids that have won their respective auctions.
*/
$$PREBID_GLOBAL$$.getAllWinningBids = function () {
return $$PREBID_GLOBAL$$._winningBids;
return auctionManager.getAllWinningBids();
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/secureCreatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import events from './events';
import { fireNativeTrackers } from './native';
import { EVENTS } from './constants';
import { auctionManager } from './auctionManager';

const BID_WON = EVENTS.BID_WON;

Expand All @@ -31,7 +32,7 @@ function receiveMessage(ev) {
sendAdToCreative(adObject, data.adServerDomain, ev.source);

// save winning bids
$$PREBID_GLOBAL$$._winningBids.push(adObject);
auctionManager.addWinningBid(adObject);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one last _winningBids down on line 47

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


events.emit(BID_WON, adObject);
}
Expand Down
16 changes: 3 additions & 13 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ describe('Unit: Prebid Module', function () {

function pushBidResponseToAuction(obj) {
adResponse = Object.assign({
auctionId: 1,
adId: bidId,
width: 300,
height: 250,
Expand All @@ -694,6 +695,7 @@ describe('Unit: Prebid Module', function () {
bidsReceived.push(adResponse);
return bidsReceived;
}
auction.getAuctionId = () => 1;
}

beforeEach(function () {
Expand Down Expand Up @@ -723,7 +725,6 @@ describe('Unit: Prebid Module', function () {

afterEach(function () {
auction.getBidsReceived = getBidResponses;
$$PREBID_GLOBAL$$._winningBids = [];
utils.logError.restore();
utils.logMessage.restore();
utils.inIframe.restore();
Expand Down Expand Up @@ -809,7 +810,7 @@ describe('Unit: Prebid Module', function () {
ad: "<script type='text/javascript' src='http://server.example.com/ad/ad.js'></script>"
});
$$PREBID_GLOBAL$$.renderAd(doc, bidId);
assert.equal($$PREBID_GLOBAL$$._winningBids[0], adResponse);
assert.deepEqual($$PREBID_GLOBAL$$.getAllWinningBids()[0], adResponse);
});
});

Expand Down Expand Up @@ -1398,17 +1399,6 @@ describe('Unit: Prebid Module', function () {
});
});

describe('getAllWinningBids', () => {
it('should return all winning bids', () => {
const bids = {name: 'a winning bid'};
$$PREBID_GLOBAL$$._winningBids = bids;

assert.deepEqual($$PREBID_GLOBAL$$.getAllWinningBids(), bids);

$$PREBID_GLOBAL$$._winningBids = [];
});
});

describe('emit event', () => {
let auctionManagerStub;
beforeEach(() => {
Expand Down