From 1475988daff3880f94f792c264c7492f7276c1bc Mon Sep 17 00:00:00 2001 From: Demetrio Girardi Date: Tue, 3 Dec 2024 08:59:43 -0800 Subject: [PATCH] Core: fix bug where queue is processed before processQueue is called (#12528) --- src/prebid.js | 9 ++++----- test/spec/unit/pbjs_api_spec.js | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/prebid.js b/src/prebid.js index 9a77f6d3bd3..2a9b88cd47d 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -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(); @@ -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) { @@ -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); diff --git a/test/spec/unit/pbjs_api_spec.js b/test/spec/unit/pbjs_api_spec.js index e56f3256569..e1f5b3b5b88 100644 --- a/test/spec/unit/pbjs_api_spec.js +++ b/test/spec/unit/pbjs_api_spec.js @@ -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', () => { @@ -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(() => {