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

Core: fix bug where queue is processed before processQueue is called #12528

Merged
merged 1 commit into from
Dec 3, 2024
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
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
Loading