Skip to content

Conversation

@blazzy
Copy link
Contributor

@blazzy blazzy commented Mar 12, 2019

No description provided.

this._q = []; // queue for proxied functions before script load
this._sending = false;
this._updateScheduled = false;
this._onInit = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be in the snippet right? Similar to the queued functions:
https://github.com/amplitude/Amplitude-JavaScript/blob/master/src/amplitude-snippet.js#L10-L14

The reason is that the AmplitudeClient might not be loaded yet, so you can't use it to queue

https://github.com/amplitude/Amplitude-JavaScript/blob/master/src/index.js#L2-L11

as.onload = function() {if(window.amplitude.runQueuedFunctions) {window.amplitude.runQueuedFunctions();} else {console.log('[Amplitude] Error: could not load SDK');}};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

it('should invoke onInit callbacks', function() {
let onInitCalled = false;
let onInit2Called = false;
amplitude.onInit(() => { onInitCalled = true; });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another test you might want to do (not sure if you can automate it, might have to manually do it), is verify that the functions are being queued even when the SDK hasn't loaded. So like maybe mess with the src of the snippet so that the SDK doesn't load, and verify that the dud queue still has onInit callbacks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we don't need to do anything too complicated. Code within the snippet script tag will always load before the SDK loads from the snippet.

Tested. Looks like it works as hoped.

@blazzy blazzy force-pushed the onInit branch 2 times, most recently from 88b1fca to 2104a86 Compare March 12, 2019 22:38
Copy link
Member

@djih djih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only suggestion is to clear the _onInit queue after running all the functions

}

for (let i = 0; i < this._onInit.length; i++) {
this._onInit[i]();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth checking if this a function or something? I guess we don't really do it in when they give us callback functions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also might want to clear the _onInit queue after you finish running all the functions, in case they call initialize more than once for whatever reason...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to punt on the type-check, but will add logic to clear the queue.

@blazzy blazzy merged commit cd7bfce into master Mar 12, 2019
@jooohhn jooohhn deleted the onInit branch September 18, 2020 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants