Skip to content

Commit

Permalink
fix: Move ready queue processing after identity request
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Oct 11, 2024
1 parent d5f9701 commit 8b5ffb4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
6 changes: 6 additions & 0 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './utils';
import { hasMPIDAndUserLoginChanged, hasMPIDChanged } from './user-utils';
import { getNewIdentitiesByName } from './type-utils';
import { processReadyQueue } from './pre-init-utils';

export default function Identity(mpInstance) {
const { getFeatureFlag, extend } = mpInstance._Helpers;
Expand Down Expand Up @@ -1683,6 +1684,11 @@ export default function Identity(mpInstance) {
'Error parsing JSON response from Identity server: ' + e
);
}
mpInstance._Store.isInitialized = true;

mpInstance._preInit.readyQueue = processReadyQueue(
mpInstance._preInit.readyQueue
);
};

// send a user identity change request on identify, login, logout, modify when any values change.
Expand Down
49 changes: 3 additions & 46 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Consent from './consent';
import KitBlocker from './kitBlocking';
import ConfigAPIClient from './configAPIClient';
import IdentityAPIClient from './identityApiClient';
import { isEmpty, isFunction } from './utils';
import { isFunction } from './utils';
import { LocalStorageVault } from './vault';
import { removeExpiredIdentityCacheDates } from './identity-utils';
import IntegrationCapture from './integrationCapture';
Expand Down Expand Up @@ -1361,15 +1361,8 @@ function completeSDKInitialization(apiKey, config, mpInstance) {
);
}

mpInstance._Store.isInitialized = true;

// Call any functions that are waiting for the library to be initialized
try {
mpInstance._preInit.readyQueue = processReadyQueue(
mpInstance._preInit.readyQueue
);
} catch (error) {
mpInstance.Logger.error(error);
if (mpInstance._Store.mpid || mpInstance._Store.webviewBridgeEnabled) {
mpInstance._Store.isInitialized = true;
}

// https://go.mparticle.com/work/SQDSDKS-6040
Expand Down Expand Up @@ -1508,42 +1501,6 @@ function processIdentityCallback(
}
}

function processPreloadedItem(readyQueueItem) {
const args = readyQueueItem;
const method = args.splice(0, 1)[0];
// if the first argument is a method on the base mParticle object, run it
if (mParticle[args[0]]) {
mParticle[method].apply(this, args);
// otherwise, the method is on either eCommerce or Identity objects, ie. "eCommerce.setCurrencyCode", "Identity.login"
} else {
const methodArray = method.split('.');
try {
var computedMPFunction = mParticle;
for (let i = 0; i < methodArray.length; i++) {
const currentMethod = methodArray[i];
computedMPFunction = computedMPFunction[currentMethod];
}
computedMPFunction.apply(this, args);
} catch (e) {
throw new Error('Unable to compute proper mParticle function ' + e);
}
}
}

function processReadyQueue(readyQueue) {
if (!isEmpty(readyQueue)) {
readyQueue.forEach(function(readyQueueItem) {
if (isFunction(readyQueueItem)) {
readyQueueItem();
} else if (Array.isArray(readyQueueItem)) {
processPreloadedItem(readyQueueItem);
}
});

return [];
}
}

function queueIfNotInitialized(func, self) {
if (!self.isInitialized()) {
self.ready(function() {
Expand Down
44 changes: 44 additions & 0 deletions src/pre-init-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { isEmpty, isFunction } from './utils';

export const processReadyQueue = (readyQueue): Function[] => {
// debugger;
console.log('processReadyQueue', readyQueue);
if (!isEmpty(readyQueue)) {
readyQueue.forEach(readyQueueItem => {
if (isFunction(readyQueueItem)) {
// debugger;
readyQueueItem();
} else if (Array.isArray(readyQueueItem)) {
// debugger;
processPreloadedItem(readyQueueItem);
}
});
}
return [];
};

const processPreloadedItem = (readyQueueItem): void => {
// debugger;
const args = readyQueueItem;
const method = args.splice(0, 1)[0];

// TODO: We should check to make sure window.mParticle is not undefined
// or possibly add it as an argument in the constructor
// if the first argument is a method on the base mParticle object, run it
if (window.mParticle[args[0]]) {
window.mParticle[method].apply(this, args);
// otherwise, the method is on either eCommerce or Identity objects, ie. "eCommerce.setCurrencyCode", "Identity.login"
} else {
const methodArray = method.split('.');
try {
var computedMPFunction = window.mParticle;
for (let i = 0; i < methodArray.length; i++) {
const currentMethod = methodArray[i];
computedMPFunction = computedMPFunction[currentMethod];
}
((computedMPFunction as unknown) as Function).apply(this, args);
} catch (e) {
throw new Error('Unable to compute proper mParticle function ' + e);
}
}
};
2 changes: 1 addition & 1 deletion test/src/tests-mparticle-instance-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe('mParticle instance manager', function() {
'apiKey3',
'purchase'
);
instance1Event.should.be.ok();
Should(instance1Event).be.ok();
Should(instance2Event).not.be.ok();
Should(instance3Event).not.be.ok();

Expand Down

0 comments on commit 8b5ffb4

Please sign in to comment.