diff --git a/packages/optimizely-sdk/CHANGELOG.MD b/packages/optimizely-sdk/CHANGELOG.MD index 6ddb2b216..f3330c849 100644 --- a/packages/optimizely-sdk/CHANGELOG.MD +++ b/packages/optimizely-sdk/CHANGELOG.MD @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] Changes that have landed but are not yet released. +### Bug Fixes: +- Clear timeout created in onReady call for timeout promise as soon as project config manager's ready promise fulfills + ### New Features - Added 60 second timeout for all datafile requests diff --git a/packages/optimizely-sdk/lib/optimizely/index.js b/packages/optimizely-sdk/lib/optimizely/index.js index 18927ccaa..23f0577e7 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.js +++ b/packages/optimizely-sdk/lib/optimizely/index.js @@ -936,6 +936,14 @@ Optimizely.prototype.onReady = function(options) { onClose: onClose, }; + this.__readyPromise.then(function() { + clearTimeout(readyTimeout); + delete this.__readyTimeouts[timeoutId]; + resolveTimeoutPromise({ + success: true, + }); + }.bind(this)); + return Promise.race([this.__readyPromise, timeoutPromise]); }; diff --git a/packages/optimizely-sdk/lib/optimizely/index.tests.js b/packages/optimizely-sdk/lib/optimizely/index.tests.js index c4acf4919..2be2a668d 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.tests.js +++ b/packages/optimizely-sdk/lib/optimizely/index.tests.js @@ -4831,11 +4831,17 @@ describe('lib/optimizely', function() { describe('onReady method', function() { var clock; + var setTimeoutSpy; + var clearTimeoutSpy; beforeEach(function() { clock = sinon.useFakeTimers(new Date().getTime()); + setTimeoutSpy = sinon.spy(clock, 'setTimeout'); + clearTimeoutSpy = sinon.spy(clock, 'clearTimeout'); }); afterEach(function() { + setTimeoutSpy.restore(); + clearTimeoutSpy.restore(); clock.restore(); }); @@ -4944,6 +4950,32 @@ describe('lib/optimizely', function() { return readyPromise3; }); }); + + it('clears the timeout when the project config manager ready promise fulfills', function() { + projectConfigManager.ProjectConfigManager.callsFake(function(config) { + return { + stop: sinon.stub(), + getConfig: sinon.stub().returns(null), + onUpdate: sinon.stub().returns(function() {}), + onReady: sinon.stub().returns(Promise.resolve({ success: true })), + }; + }); + optlyInstance = new Optimizely({ + clientEngine: 'node-sdk', + errorHandler: errorHandler, + eventDispatcher: eventDispatcher, + jsonSchemaValidator: jsonSchemaValidator, + logger: createdLogger, + sdkKey: '12345', + isValidInstance: true, + }); + return optlyInstance.onReady().then(function() { + sinon.assert.calledOnce(clock.setTimeout); + var timeout = clock.setTimeout.getCall(0).returnValue; + sinon.assert.calledOnce(clock.clearTimeout); + sinon.assert.calledWithExactly(clock.clearTimeout, timeout); + }); + }); }); describe('project config updates', function() {