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
22 changes: 22 additions & 0 deletions src/__tests__/LDClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ describe('LDClient', function() {
});
});

it('should resolve waitUntilReady promise after ready event was already emitted', function(done) {
var user = { key: 'user' };
var handleInitialReady = sinon.spy();
var handleReady = sinon.spy();
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
bootstrap: {}
});

client.on('ready', handleInitialReady);

setTimeout(function () {
client.waitUntilReady().then(handleReady);

setTimeout(function () {
expect(handleInitialReady.called).to.be.true;
expect(handleReady.called).to.be.true;
done();
}, 0);
}, 1000);
});


it('should log an error when initialize is called without an environment key', function(done) {
var user = {key: 'user'};
var errorSpy = sinon.spy(console, 'error');
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var flushInterval = 2000;

var seenRequests = {};

var ldPromise = Promise.reject(messages.clientNotReady());

function sendIdentifyEvent(user) {
enqueueEvent({
kind: 'identify',
Expand Down Expand Up @@ -82,9 +84,7 @@ function sendGoalEvent(kind, goal) {
}

function waitUntilReady() {
return new Promise(function(resolve) {
client.on('ready', resolve);
});
return ldPromise;
}

function identify(user, hash, onDone) {
Expand Down Expand Up @@ -388,6 +388,11 @@ function initialize(env, user, options) {

window.addEventListener('message', handleMessage);

var onReady = client.on('ready', function () {
ldPromise = Promise.resolve(client);
client.off(onReady);
});

return client;
}

Expand Down
3 changes: 3 additions & 0 deletions src/messages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports ={
clientNotReady: function() {
return 'LaunchDarkly client is not ready';
},
invalidKey: function() {
return 'Event key must be a string';
},
Expand Down