Skip to content

Commit

Permalink
userId module: fix auctionDelay submodules with callbacks (#5891)
Browse files Browse the repository at this point in the history
* clearTimeout only after all submodules are done

* check that setTimeout function was not cleared
  • Loading branch information
FilipStamenkovic authored Nov 10, 2020
1 parent 9676ce0 commit 690709b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,13 @@ function hasGDPRConsent(consentData) {
* @param {function} cb - callback for after processing is done.
*/
function processSubmoduleCallbacks(submodules, cb) {
const done = cb ? utils.delayExecution(cb, submodules.length) : function () { };
let done = () => {};
if (cb) {
done = utils.delayExecution(() => {
clearTimeout(timeoutID);
cb();
}, submodules.length);
}
submodules.forEach(function (submodule) {
submodule.callback(function callbackCompleted(idObj) {
// if valid, id data should be saved to cookie/html storage
Expand All @@ -338,7 +344,6 @@ function processSubmoduleCallbacks(submodules, cb) {
// clear callback, this prop is used to test if all submodule callbacks are complete below
submodule.callback = undefined;
});
clearTimeout(timeoutID);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ describe('User ID', function () {
beforeEach(function () {
sandbox = sinon.createSandbox();
sandbox.stub(global, 'setTimeout').returns(2);
sandbox.stub(global, 'clearTimeout');
sandbox.stub(events, 'on');
sandbox.stub(coreStorage, 'getCookie');

Expand Down Expand Up @@ -662,6 +663,7 @@ describe('User ID', function () {
requestBidsHook(auctionSpy, {adUnits});

// check auction was delayed
global.clearTimeout.calledOnce.should.equal(false);
global.setTimeout.calledOnce.should.equal(true);
global.setTimeout.calledWith(sinon.match.func, 33);
auctionSpy.calledOnce.should.equal(false);
Expand Down Expand Up @@ -696,6 +698,7 @@ describe('User ID', function () {

// check auction was delayed
// global.setTimeout.calledOnce.should.equal(true);
global.clearTimeout.calledOnce.should.equal(false);
global.setTimeout.calledWith(sinon.match.func, 33);
auctionSpy.calledOnce.should.equal(false);

Expand Down

0 comments on commit 690709b

Please sign in to comment.