Skip to content

Commit

Permalink
Core: fix bug where queue is processed before processQueue is called (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Dec 3, 2024
1 parent 6398c68 commit 1475988
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,12 @@ pbjsInstance.que.push(() => listenMessagesFromCreative());
* by prebid once it's done loading. If it runs after prebid loads, then this monkey-patch causes their
* function to execute immediately.
*
* @memberof pbjs
* @param {function} command A function which takes no arguments. This is guaranteed to run exactly once, and only after
* the Prebid script has been fully loaded.
* @alias module:pbjs.cmd.push
* @alias module:pbjs.que.push
*/
pbjsInstance.cmd.push = function (command) {
function quePush(command) {
if (typeof command === 'function') {
try {
command.call();
Expand All @@ -982,9 +982,7 @@ pbjsInstance.cmd.push = function (command) {
} else {
logError('Commands written into $$PREBID_GLOBAL$$.cmd.push must be wrapped in a function');
}
};

pbjsInstance.que.push = pbjsInstance.cmd.push;
}

function processQueue(queue) {
queue.forEach(function (cmd) {
Expand All @@ -1003,6 +1001,7 @@ function processQueue(queue) {
* @alias module:pbjs.processQueue
*/
pbjsInstance.processQueue = function () {
pbjsInstance.que.push = pbjsInstance.cmd.push = quePush;
insertLocatorFrame();
hook.ready();
processQueue(pbjsInstance.que);
Expand Down
20 changes: 16 additions & 4 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,21 @@ describe('Unit: Prebid Module', function () {
getBidToRender.getHooks({hook: getBidToRenderHook}).remove();
});

it('should insert a locator frame on the page', () => {
$$PREBID_GLOBAL$$.processQueue();
expect(window.frames[PB_LOCATOR]).to.exist;
describe('processQueue', () => {
it('should insert a locator frame on the page', () => {
$$PREBID_GLOBAL$$.processQueue();
expect(window.frames[PB_LOCATOR]).to.exist;
});

['cmd', 'que'].forEach(prop => {
it(`should patch ${prop}.push`, () => {
$$PREBID_GLOBAL$$[prop].push = false;
$$PREBID_GLOBAL$$.processQueue();
let ran = false;
$$PREBID_GLOBAL$$[prop].push(() => { ran = true; });
expect(ran).to.be.true;
})
})
})

describe('and global adUnits', () => {
Expand All @@ -262,10 +274,10 @@ describe('Unit: Prebid Module', function () {

beforeEach(() => {
$$PREBID_GLOBAL$$.requestBids.before(deferringHook, 99);
$$PREBID_GLOBAL$$.adUnits.splice(0, $$PREBID_GLOBAL$$.adUnits.length, ...startingAdUnits);
hookRan = new Promise((resolve) => {
done = resolve;
});
$$PREBID_GLOBAL$$.adUnits.splice(0, $$PREBID_GLOBAL$$.adUnits.length, ...startingAdUnits);
});

afterEach(() => {
Expand Down

0 comments on commit 1475988

Please sign in to comment.