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

Bug fix to populate userSync default values #1897

Merged
merged 1 commit into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 33 additions & 12 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ const DEFAULT_BIDDER_TIMEOUT = 3000;
const DEFAULT_PUBLISHER_DOMAIN = window.location.origin;
const DEFAULT_COOKIESYNC_DELAY = 100;
const DEFAULT_ENABLE_SEND_ALL_BIDS = false;
const DEFAULT_USERSYNC = {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000
};

const GRANULARITY_OPTIONS = {
LOW: 'low',
Expand All @@ -44,6 +38,8 @@ const ALL_TOPICS = '*';
export function newConfig() {
let listeners = [];

let defaults = {};

let config = {
// `debug` is equivalent to legacy `pbjs.logging` property
_debug: DEFAULT_DEBUG,
Expand Down Expand Up @@ -122,10 +118,7 @@ export function newConfig() {
// calls existing function which may be moved after deprecation
set s2sConfig(val) {
$$PREBID_GLOBAL$$.setS2SConfig(val);
},

// userSync defaults
userSync: DEFAULT_USERSYNC
}
};

function hasGranularity(val) {
Expand Down Expand Up @@ -177,8 +170,35 @@ export function newConfig() {
return;
}

let topics = Object.keys(options);
let topicalConfig = {};

topics.forEach(topic => {
let option = options[topic];

if (typeof defaults[topic] === 'object' && typeof option === 'object') {
option = Object.assign({}, defaults[topic], option);
}

topicalConfig[topic] = config[topic] = option;
});

callSubscribers(topicalConfig);
}

/**
* Sets configuration defaults which setConfig values can be applied on top of
* @param {object} options
*/
function setDefaults(options) {
if (typeof defaults !== 'object') {
utils.logError('defaults must be an object');
return;
}

Object.assign(defaults, options);
// Add default values to config as well
Object.assign(config, options);
callSubscribers(options);
}

/*
Expand Down Expand Up @@ -246,7 +266,8 @@ export function newConfig() {

return {
getConfig,
setConfig
setConfig,
setDefaults
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/userSync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as utils from 'src/utils';
import { config } from 'src/config';

// Set userSync default values
config.setDefaults({
'userSync': {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000
}
});

/**
* Factory function which creates a new UserSyncPool.
*
Expand Down
8 changes: 6 additions & 2 deletions test/spec/config_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const utils = require('src/utils');

let getConfig;
let setConfig;
let setDefaults;

describe('config API', () => {
let logErrorSpy;
beforeEach(() => {
const config = newConfig();
getConfig = config.getConfig;
setConfig = config.setConfig;
setDefaults = config.setDefaults;
logErrorSpy = sinon.spy(utils, 'logError');
});

Expand Down Expand Up @@ -86,12 +88,14 @@ describe('config API', () => {
});

it('gets default userSync config', () => {
expect(getConfig('userSync')).to.eql({
const DEFAULT_USERSYNC = {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000
});
};
setDefaults({'userSync': DEFAULT_USERSYNC});
expect(getConfig('userSync')).to.eql(DEFAULT_USERSYNC);
});

it('has subscribe functionality for adding listeners to config updates', () => {
Expand Down