Skip to content

Commit c6586af

Browse files
Merge branch 'master' of github.com:optimizely/javascript-sdk into get_feat_var_1.0
2 parents 38f4fb7 + a7060ea commit c6586af

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

packages/optimizely-sdk/CHANGELOG.MD

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
Changes that have landed but are not yet released.
99

10+
### New Features
11+
- Updated the `close` method to return a `Promise` representing the process of closing the instance. When `close` is called, any events waiting to be sent as part of a batched event request will be immediately batched and sent to the event dispatcher.
12+
- If any such requests were sent to the event dispatcher, `close` returns a `Promise` that fulfills after the event dispatcher calls the response callback for each request. Otherwise, `close` returns an immediately-fulfilled `Promise`.
13+
- The `Promise` returned from `close` is fulfilled with a result object containing `success` (boolean) and `reason` (string, only when success is `false`) properties. In the result object, `success` is `true` if all events in the queue at the time close was called were combined into requests, sent to the event dispatcher, and the event dispatcher called the callbacks for each request. `success` is false if an unexpected error was encountered during the close process.
14+
1015
## [3.2.1] - July 1st, 2019
1116

1217
### Changed

packages/optimizely-sdk/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![license](https://img.shields.io/github/license/optimizely/javascript-sdk.svg)](https://choosealicense.com/licenses/apache-2.0/)
77

88

9-
Optimizely X Full Stack is A/B testing and feature management for product development teams. Experiment in any application. Make every feature on your roadmap an opportunity to learn. Learn more at the [landing page](https://www.optimizely.com/products/full-stack/), or see the [documentation](https://developers.optimizely.com/x/solutions/sdks/reference/index.html?language=node).
9+
Optimizely X Full Stack is A/B testing and feature management for product development teams. Experiment in any application. Make every feature on your roadmap an opportunity to learn. Learn more at the [landing page](https://www.optimizely.com/products/full-stack/), or see the [documentation](https://docs.developers.optimizely.com/full-stack/docs).
1010

1111
This directory contains the source code for the JavaScript SDK, which is usable in Node.js, browsers, and beyond.
1212

packages/optimizely-sdk/lib/optimizely/index.js

+49-5
Original file line numberDiff line numberDiff line change
@@ -879,11 +879,38 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey
879879
};
880880

881881
/**
882-
* Cleanup method for killing an running timers and flushing eventQueue
882+
* Stop background processes belonging to this instance, including:
883+
*
884+
* - Active datafile requests
885+
* - Pending datafile requests
886+
* - Pending event queue flushes
887+
*
888+
* In-flight datafile requests will be aborted. Any events waiting to be sent
889+
* as part of a batched event request will be immediately batched and sent to
890+
* the event dispatcher.
891+
*
892+
* If any such requests were sent to the event dispatcher, returns a Promise
893+
* that fulfills after the event dispatcher calls the response callback for each
894+
* request. Otherwise, returns an immediately-fulfilled Promise.
895+
*
896+
* Returned Promises are fulfilled with result objects containing these
897+
* properties:
898+
* - success (boolean): true if all events in the queue at the time close was
899+
* called were combined into requests, sent to the
900+
* event dispatcher, and the event dispatcher called the
901+
* callbacks for each request. false if an unexpected
902+
* error was encountered during the close process.
903+
* - reason (string=): If success is false, this is a string property with
904+
* an explanatory message.
905+
*
906+
* NOTE: After close is called, this instance is no longer usable - any events
907+
* generated will no longer be sent to the event dispatcher.
908+
*
909+
* @return {Promise}
883910
*/
884911
Optimizely.prototype.close = function() {
885912
try {
886-
this.eventProcessor.stop();
913+
var eventProcessorStoppedPromise = this.eventProcessor.stop();
887914
if (this.__disposeOnUpdate) {
888915
this.__disposeOnUpdate();
889916
this.__disposeOnUpdate = null;
@@ -897,9 +924,26 @@ Optimizely.prototype.close = function() {
897924
readyTimeoutRecord.onClose();
898925
}.bind(this));
899926
this.__readyTimeouts = {};
900-
} catch (e) {
901-
this.logger.log(LOG_LEVEL.ERROR, e.message);
902-
this.errorHandler.handleError(e);
927+
return eventProcessorStoppedPromise.then(
928+
function() {
929+
return {
930+
success: true,
931+
};
932+
},
933+
function(err) {
934+
return {
935+
success: false,
936+
reason: String(err),
937+
};
938+
}
939+
);
940+
} catch (err) {
941+
this.logger.log(LOG_LEVEL.ERROR, err.message);
942+
this.errorHandler.handleError(err);
943+
return Promise.resolve({
944+
success: false,
945+
reason: String(err),
946+
});
903947
}
904948
};
905949

packages/optimizely-sdk/lib/optimizely/index.tests.js

+83
Original file line numberDiff line numberDiff line change
@@ -5490,6 +5490,89 @@ describe('lib/optimizely', function() {
54905490
assert.deepEqual(eventDispatcherCall[0], expectedObj);
54915491
});
54925492
});
5493+
5494+
describe('close method', function() {
5495+
var eventProcessorStopPromise;
5496+
var optlyInstance;
5497+
var mockEventProcessor;
5498+
beforeEach(function() {
5499+
mockEventProcessor = {
5500+
process: sinon.stub(),
5501+
start: sinon.stub(),
5502+
stop: sinon.stub(),
5503+
};
5504+
sinon.stub(eventProcessor, 'LogTierV1EventProcessor').returns(mockEventProcessor);
5505+
});
5506+
5507+
afterEach(function() {
5508+
eventProcessor.LogTierV1EventProcessor.restore();
5509+
});
5510+
5511+
describe('when the event processor stop method returns a promise that fulfills', function() {
5512+
beforeEach(function() {
5513+
eventProcessorStopPromise = Promise.resolve();
5514+
mockEventProcessor.stop.returns(eventProcessorStopPromise);
5515+
optlyInstance = new Optimizely({
5516+
clientEngine: 'node-sdk',
5517+
datafile: testData.getTestProjectConfig(),
5518+
eventBuilder: eventBuilder,
5519+
errorHandler: errorHandler,
5520+
eventDispatcher: eventDispatcher,
5521+
jsonSchemaValidator: jsonSchemaValidator,
5522+
logger: createdLogger,
5523+
isValidInstance: true,
5524+
eventBatchSize: 3,
5525+
eventFlushInterval: 100,
5526+
});
5527+
});
5528+
5529+
afterEach(function() {
5530+
return eventProcessorStopPromise.catch(function() {
5531+
// Handle rejected promise - don't want test to fail
5532+
});
5533+
});
5534+
5535+
it('returns a promise that fulfills with a successful result object', function() {
5536+
return optlyInstance.close().then(function(result) {
5537+
assert.deepEqual(result, { success: true });
5538+
});
5539+
});
5540+
});
5541+
5542+
describe('when the event processor stop method returns a promise that rejects', function() {
5543+
beforeEach(function() {
5544+
eventProcessorStopPromise = Promise.reject(new Error('Failed to stop'));
5545+
mockEventProcessor.stop.returns(eventProcessorStopPromise);
5546+
optlyInstance = new Optimizely({
5547+
clientEngine: 'node-sdk',
5548+
datafile: testData.getTestProjectConfig(),
5549+
eventBuilder: eventBuilder,
5550+
errorHandler: errorHandler,
5551+
eventDispatcher: eventDispatcher,
5552+
jsonSchemaValidator: jsonSchemaValidator,
5553+
logger: createdLogger,
5554+
isValidInstance: true,
5555+
eventBatchSize: 3,
5556+
eventFlushInterval: 100,
5557+
});
5558+
});
5559+
5560+
afterEach(function() {
5561+
return eventProcessorStopPromise.catch(function() {
5562+
// Handle rejected promise - don't want test to fail
5563+
});
5564+
});
5565+
5566+
it('returns a promise that fulfills with an unsuccessful result object', function() {
5567+
return optlyInstance.close().then(function(result) {
5568+
assert.deepEqual(result, {
5569+
success: false,
5570+
reason: 'Error: Failed to stop',
5571+
});
5572+
});
5573+
});
5574+
});
5575+
});
54935576
});
54945577

54955578
describe('event processor defaults', function() {

0 commit comments

Comments
 (0)