diff --git a/src/identity.js b/src/identity.js index d04ea230..13cb7ca7 100644 --- a/src/identity.js +++ b/src/identity.js @@ -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; @@ -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. diff --git a/src/mp-instance.js b/src/mp-instance.js index 14a1ffd9..e83bdb44 100644 --- a/src/mp-instance.js +++ b/src/mp-instance.js @@ -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'; @@ -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 @@ -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() { diff --git a/src/pre-init-utils.ts b/src/pre-init-utils.ts new file mode 100644 index 00000000..1bcc0854 --- /dev/null +++ b/src/pre-init-utils.ts @@ -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); + } + } +}; diff --git a/test/src/tests-mparticle-instance-manager.js b/test/src/tests-mparticle-instance-manager.js index 18a90d3b..7b61d340 100644 --- a/test/src/tests-mparticle-instance-manager.js +++ b/test/src/tests-mparticle-instance-manager.js @@ -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();