-
Notifications
You must be signed in to change notification settings - Fork 44
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
fix: Move ready queue processing after identity request #933
base: development
Are you sure you want to change the base?
Changes from 6 commits
6478269
637c107
68c73e1
94627d5
198b448
60ec32c
7354cdf
f411dda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { isEmpty, isFunction } from './utils'; | ||
|
||
export const processReadyQueue = (readyQueue): Function[] => { | ||
if (!isEmpty(readyQueue)) { | ||
readyQueue.forEach(readyQueueItem => { | ||
if (isFunction(readyQueueItem)) { | ||
// debugger; | ||
alexs-mparticle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readyQueueItem(); | ||
} else if (Array.isArray(readyQueueItem)) { | ||
// debugger; | ||
alexs-mparticle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
processPreloadedItem(readyQueueItem); | ||
} | ||
}); | ||
} | ||
return []; | ||
}; | ||
|
||
const processPreloadedItem = (readyQueueItem): void => { | ||
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 (typeof window !== 'undefined' && window.mParticle && 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 { | ||
let computedMPFunction = window.mParticle; | ||
for (const currentMethod of methodArray) { | ||
computedMPFunction = computedMPFunction[currentMethod]; | ||
} | ||
((computedMPFunction as unknown) as Function).apply(this, args); | ||
} catch (e) { | ||
throw new Error('Unable to compute proper mParticle function ' + e); | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { processReadyQueue } from '../../src/pre-init-utils'; | ||
|
||
describe('pre-init-utils', () => { | ||
describe('#processReadyQueue', () => { | ||
it('should return an empty array if readyQueue is empty', () => { | ||
const result = processReadyQueue([]); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should process functions passed as arguments', () => { | ||
const functionSpy = jest.fn(); | ||
const readyQueue: Function[] = [functionSpy, functionSpy, functionSpy]; | ||
const result = processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledTimes(3); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should process functions passed as arrays', () => { | ||
const functionSpy = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: functionSpy, | ||
}; | ||
const readyQueue = [['fakeFunction']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should process functions passed as arrays with arguments', () => { | ||
const functionSpy = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: functionSpy, | ||
args: () => {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm open to discussing this further. Adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's hop on a zoom about this to walk through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
const readyQueue = [['fakeFunction', 'args']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledWith('args'); | ||
}); | ||
|
||
it('should process arrays passed as arguments with multiple methods', () => { | ||
const functionSpy = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: { | ||
anotherFakeFunction: functionSpy, | ||
}, | ||
}; | ||
const readyQueue = [['fakeFunction.anotherFakeFunction', 'foo']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledWith('foo'); | ||
}); | ||
rmi22186 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it('should throw an error if it cannot compute the proper mParticle function', () => { | ||
const readyQueue = [['Identity.login']]; | ||
expect(() => processReadyQueue(readyQueue)).toThrowError("Unable to compute proper mParticle function TypeError: Cannot read properties of undefined (reading 'login')"); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your PR description appropriately states when this is the case, rather than "for some reason", so I'd turn that into a comment.
"Will also continue to clear out the ready queue as part of the initial init flow if an identify request is unnecessary, such as if there is an existing session".