Skip to content

Commit

Permalink
prepare 1.5.0 release (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly authored Mar 5, 2018
1 parent ee9f925 commit 4fd7e80
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to the LaunchDarkly client-side JavaScript SDK will be documented in this file. This
project adheres to [Semantic Versioning](http://semver.org).

## [1.5.0] - 2018-03-05
### Added
- The `options` object now supports a `samplingInterval` property. If greater than zero, this causes a fraction of analytics events to be sent to LaunchDarkly: one per that number of events (pseudo-randomly). For instance, setting it to 5 would cause 20% of events to be sent on average.

## [1.4.0] - 2018-02-07
### Added
- The SDK now supports multiple environments. Calling `initialize` returns a new client each time.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ldclient-js",
"version": "1.4.0",
"version": "1.5.0",
"description": "LaunchDarkly SDK for JavaScript",
"author": "LaunchDarkly <[email protected]>",
"license": "Apache-2.0",
Expand Down
1 change: 0 additions & 1 deletion src/EventSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function EventSerializer(config) {
}

function filter_user(user) {
var allPrivateAttrs = {};
var userPrivateAttrs = user.privateAttributeNames || [];

var isPrivateAttr = function(name) {
Expand Down
25 changes: 20 additions & 5 deletions src/__tests__/LDClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ describe('LDClient', function() {
});

client.waitUntilReady().then(handleReady);

client.on('ready', function() {
setTimeout(function() {
expect(handleReady.called).to.be.true;
done();
}, 0);
});
});

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;
Expand All @@ -96,6 +96,21 @@ describe('LDClient', function() {
});
});

it('should emit an error when an invalid samplingInterval is specified', function(done) {
var user = { key: 'user' };
var handleInitialReady = sinon.spy();
var handleReady = sinon.spy();
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
bootstrap: {},
samplingInterval: "totally not a number"
});

client.on('error', function(err) {
expect(err.message).to.be.equal('Invalid sampling interval configured. Sampling interval must be an integer >= 0.');
done();
});
});

it('should emit an error when initialize is called without an environment key', function(done) {
var user = {key: 'user'};
var client = LDClient.initialize('', user, {
Expand Down
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ exports.LDUnexpectedResponseError = createCustomError('LaunchDarklyUnexpectedRes
exports.LDInvalidEnvironmentIdError = createCustomError('LaunchDarklyInvalidEnvironmentIdError');
exports.LDInvalidUserError = createCustomError('LaunchDarklyInvalidUserError');
exports.LDInvalidEventKeyError = createCustomError('LaunchDarklyInvalidEventKeyError');
exports.LDInvalidArgumentError = createCustomError('LaunchDarklyInvalidArgumentError');
exports.LDFlagFetchError = createCustomError('LaunchDarklyFlagFetchError');
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ var messages = require('./messages');
var store = require('./store');
var errors = require('./errors');

function initialize(env, user, options) {
function initialize(env, user, options) {
var flags = {};
var environment;
var events;
var requestor;
var stream;
var sendEvents;
var samplingInterval;
var emitter;
var hash;
var ident;
Expand Down Expand Up @@ -113,11 +114,15 @@ function initialize(env, user, options) {
}

function enqueueEvent(event) {
if (sendEvents && !doNotTrack()) {
if (shouldEnqueueEvent()) {
events.enqueue(event);
}
}

function shouldEnqueueEvent() {
return sendEvents && !doNotTrack() && (samplingInterval === 0 || Math.floor(Math.random() * samplingInterval) === 0)
}

function doNotTrack() {
var flag;
if (navigator && navigator.doNotTrack !== undefined) {
Expand Down Expand Up @@ -263,11 +268,19 @@ function initialize(env, user, options) {
stream = Stream(streamUrl, environment);
events = EventProcessor(eventsUrl + '/a/' + environment + '.gif', EventSerializer(options));
sendEvents = (typeof options.sendEvents === 'undefined') ? true : config.sendEvents;
samplingInterval = parseInt(options.samplingInterval) || 0;
emitter = EventEmitter();
ident = Identity(user, sendIdentifyEvent);
requestor = Requestor(baseUrl, environment, options.useReport);
localStorageKey = lsKey(environment, ident.getUser());

if (options.samplingInterval !== undefined && (isNaN(options.samplingInterval) || options.samplingInterval < 0)) {
samplingInterval = 0;
utils.onNextTick(function() {
emitter.maybeReportError(new errors.LDInvalidArgumentError('Invalid sampling interval configured. Sampling interval must be an integer >= 0.'));
})
}

if (!env) {
utils.onNextTick(function() {
emitter.maybeReportError(new errors.LDInvalidEnvironmentIdError(messages.environmentNotSpecified()));
Expand Down
2 changes: 1 addition & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
return 'Custom event "' + key + '" does not exist';
},
environmentNotFound: function() {
return 'environment not found.' + docLink;
return 'Environment not found.' + docLink;
},
environmentNotSpecified: function() {
return 'No environment specified.' + docLink;
Expand Down

0 comments on commit 4fd7e80

Please sign in to comment.