Skip to content
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
8 changes: 7 additions & 1 deletion src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
this._newSession = false;
this._sequenceNumber = 0;
this._sessionId = null;
this._isInitialized = false;

this._userAgent = (navigator && navigator.userAgent) || null;
};
Expand Down Expand Up @@ -155,6 +156,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
this._onInit[i]();
}
this._onInit = [];
this._isInitialized = true;
};

/**
Expand Down Expand Up @@ -296,7 +298,11 @@ AmplitudeClient.prototype.isNewSession = function isNewSession() {
* @private
*/
AmplitudeClient.prototype.onInit = function (callback) {
this._onInit.push(callback);
if (this._isInitialized) {
callback();
} else {
this._onInit.push(callback);
}
};

/**
Expand Down
16 changes: 16 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ describe('AmplitudeClient', function() {
assert.ok(onInit2Called);
});

it('should not invoke onInit callbacks before init is called', function() {
let onInitCalled = false;
amplitude.onInit(() => { onInitCalled = true; });

assert.ok(onInitCalled === false);
amplitude.init(apiKey);
assert.ok(onInitCalled);
});

it('should immediately invoke onInit callbacks if already initialized', function() {
let onInitCalled = false;
amplitude.init(apiKey);
amplitude.onInit(() => { onInitCalled = true; });
assert.ok(onInitCalled);
});

it('should clear the onInitQueue', function() {
let onInitCalled = false;
let onInit2Called = false;
Expand Down