Skip to content

Commit 0ac6a76

Browse files
committed
Cleaned up other PR by fayyaz
1 parent 1b71750 commit 0ac6a76

13 files changed

+962
-1339
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ jobs:
7070
before_script:
7171
- mkdir $HOME/travisci-tools && pushd $HOME/travisci-tools && git init && git pull https://[email protected]/optimizely/travisci-tools.git && popd
7272
script:
73-
- CLIENT=node $HOME/travisci-tools/trigger-script-with-status-update.sh
74-
- CLIENT=browser $HOME/travisci-tools/trigger-script-with-status-update.sh
73+
- CLIENT=node TESTAPP_BRANCH=fayyaz/added-support-for-es6 $HOME/travisci-tools/trigger-script-with-status-update.sh
74+
- CLIENT=browser TESTAPP_BRANCH=fayyaz/added-support-for-es6 $HOME/travisci-tools/trigger-script-with-status-update.sh
7575
after_success: travis_terminate 0
7676
- <<: *integrationtest
7777
stage: 'Benchmarking tests'

packages/optimizely-sdk/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
Promise: 'readonly',
1212
},
1313
parserOptions: {
14-
ecmaVersion: 5,
14+
ecmaVersion: 6,
15+
sourceType: 'module',
1516
},
1617
rules: {
1718
'no-prototype-builtins': 'off',
Lines changed: 135 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, 2019, Optimizely
2+
* Copyright 2016-2017, 2019, 2020 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,152 +13,160 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var logging = require('@optimizely/js-sdk-logging');
17-
var fns = require('./utils/fns');
18-
var configValidator = require('./utils/config_validator');
19-
var defaultErrorHandler = require('./plugins/error_handler');
20-
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.browser');
21-
var enums = require('./utils/enums');
22-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
23-
var loggerPlugin = require('./plugins/logger');
24-
var Optimizely = require('./optimizely');
25-
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
26-
27-
var logger = logging.getLogger();
28-
logging.setLogHandler(loggerPlugin.createLogger());
29-
logging.setLogLevel(logging.LogLevel.INFO);
16+
import * as sdkLogging from '@optimizely/js-sdk-logging';
17+
import fns from './utils/fns';
18+
import configValidator from './utils/config_validator';
19+
import defaultErrorHandler from './plugins/error_handler';
20+
import defaultEventDispatcher from './plugins/event_dispatcher/index.browser';
21+
import utilEnums from './utils/enums';
22+
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
23+
import loggerPlugin from './plugins/logger';
24+
import Optimizely from './optimizely';
25+
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
26+
27+
var logger = sdkLogging.getLogger();
28+
sdkLogging.setLogHandler(loggerPlugin.createLogger());
29+
sdkLogging.setLogLevel(sdkLogging.LogLevel.INFO);
3030

3131
var MODULE_NAME = 'INDEX_BROWSER';
32-
3332
var DEFAULT_EVENT_BATCH_SIZE = 10;
3433
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
3534

3635
var hasRetriedEvents = false;
36+
37+
export var logging = loggerPlugin;
38+
export var errorHandler = defaultErrorHandler;
39+
export var eventDispatcher = defaultEventDispatcher;
40+
export var enums = utilEnums;
41+
export var setLogger = sdkLogging.setLogHandler;
42+
export var setLogLevel = sdkLogging.setLogLevel;
43+
3744
/**
38-
* Entry point into the Optimizely Browser SDK
45+
* Creates an instance of the Optimizely class
46+
* @param {Object} config
47+
* @param {Object} config.datafile
48+
* @param {Object} config.errorHandler
49+
* @param {Object} config.eventDispatcher
50+
* @param {Object} config.logger
51+
* @param {Object} config.logLevel
52+
* @param {Object} config.userProfileService
53+
* @param {Object} config.eventBatchSize
54+
* @param {Object} config.eventFlushInterval
55+
* @return {Object} the Optimizely object
3956
*/
40-
module.exports = {
41-
logging: loggerPlugin,
42-
errorHandler: defaultErrorHandler,
43-
eventDispatcher: defaultEventDispatcher,
44-
enums: enums,
45-
46-
setLogger: logging.setLogHandler,
47-
setLogLevel: logging.setLogLevel,
48-
49-
/**
50-
* Creates an instance of the Optimizely class
51-
* @param {Object} config
52-
* @param {Object} config.datafile
53-
* @param {Object} config.errorHandler
54-
* @param {Object} config.eventDispatcher
55-
* @param {Object} config.logger
56-
* @param {Object} config.logLevel
57-
* @param {Object} config.userProfileService
58-
* @param {Object} config.eventBatchSize
59-
* @param {Object} config.eventFlushInterval
60-
* @return {Object} the Optimizely object
61-
*/
62-
createInstance: function(config) {
63-
try {
64-
config = config || {};
57+
export var createInstance = function(config) {
58+
try {
59+
config = config || {};
6560

66-
// TODO warn about setting per instance errorHandler / logger / logLevel
67-
if (config.errorHandler) {
68-
logging.setErrorHandler(config.errorHandler);
69-
}
70-
if (config.logger) {
71-
logging.setLogHandler(config.logger);
72-
// respect the logger's shouldLog functionality
73-
logging.setLogLevel(logging.LogLevel.NOTSET);
74-
}
75-
if (config.logLevel !== undefined) {
76-
logging.setLogLevel(config.logLevel);
77-
}
61+
// TODO warn about setting per instance errorHandler / logger / logLevel
62+
if (config.errorHandler) {
63+
sdkLogging.setErrorHandler(config.errorHandler);
64+
}
65+
if (config.logger) {
66+
sdkLogging.setLogHandler(config.logger);
67+
// respect the logger's shouldLog functionality
68+
sdkLogging.setLogLevel(sdkLogging.LogLevel.NOTSET);
69+
}
70+
if (config.logLevel !== undefined) {
71+
sdkLogging.setLogLevel(config.logLevel);
72+
}
7873

79-
try {
80-
configValidator.validate(config);
81-
config.isValidInstance = true;
82-
} catch (ex) {
83-
logger.error(ex);
84-
config.isValidInstance = false;
85-
}
74+
try {
75+
configValidator.validate(config);
76+
config.isValidInstance = true;
77+
} catch (ex) {
78+
logger.error(ex);
79+
config.isValidInstance = false;
80+
}
81+
82+
// Explicitly check for null or undefined
83+
// prettier-ignore
84+
if (config.skipJSONValidation == null) { // eslint-disable-line eqeqeq
85+
config.skipJSONValidation = true;
86+
}
8687

87-
// Explicitly check for null or undefined
88-
// prettier-ignore
89-
if (config.skipJSONValidation == null) { // eslint-disable-line eqeqeq
90-
config.skipJSONValidation = true;
88+
var eventDispatcher;
89+
// prettier-ignore
90+
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
91+
// only wrap the event dispatcher with pending events retry if the user didnt override
92+
eventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
93+
eventDispatcher: defaultEventDispatcher,
94+
});
95+
96+
if (!hasRetriedEvents) {
97+
eventDispatcher.sendPendingEvents();
98+
hasRetriedEvents = true;
9199
}
100+
} else {
101+
eventDispatcher = config.eventDispatcher;
102+
}
92103

93-
var eventDispatcher;
94-
// prettier-ignore
95-
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
96-
// only wrap the event dispatcher with pending events retry if the user didnt override
97-
eventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
98-
eventDispatcher: defaultEventDispatcher,
99-
});
100-
101-
if (!hasRetriedEvents) {
102-
eventDispatcher.sendPendingEvents();
103-
hasRetriedEvents = true;
104-
}
105-
} else {
106-
eventDispatcher = config.eventDispatcher;
104+
config = fns.assign(
105+
{
106+
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
107+
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
108+
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
109+
},
110+
config,
111+
{
112+
eventDispatcher: eventDispatcher,
113+
// always get the OptimizelyLogger facade from logging
114+
logger: logger,
115+
errorHandler: sdkLogging.getErrorHandler(),
107116
}
117+
);
108118

109-
config = fns.assign(
110-
{
111-
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
112-
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
113-
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
114-
},
115-
config,
116-
{
117-
eventDispatcher: eventDispatcher,
118-
// always get the OptimizelyLogger facade from logging
119-
logger: logger,
120-
errorHandler: logging.getErrorHandler(),
121-
}
119+
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
120+
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
121+
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
122+
}
123+
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
124+
logger.warn(
125+
'Invalid eventFlushInterval %s, defaulting to %s',
126+
config.eventFlushInterval,
127+
DEFAULT_EVENT_FLUSH_INTERVAL
122128
);
129+
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
130+
}
123131

124-
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
125-
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
126-
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
127-
}
128-
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
129-
logger.warn(
130-
'Invalid eventFlushInterval %s, defaulting to %s',
131-
config.eventFlushInterval,
132-
DEFAULT_EVENT_FLUSH_INTERVAL
133-
);
134-
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
135-
}
132+
var optimizely = new Optimizely(config);
136133

137-
var optimizely = new Optimizely(config);
138-
139-
try {
140-
if (typeof window.addEventListener === 'function') {
141-
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
142-
window.addEventListener(
143-
unloadEvent,
144-
function() {
145-
optimizely.close();
146-
},
147-
false
148-
);
149-
}
150-
} catch (e) {
151-
logger.error(enums.LOG_MESSAGES.UNABLE_TO_ATTACH_UNLOAD, MODULE_NAME, e.message);
134+
try {
135+
if (typeof window.addEventListener === 'function') {
136+
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
137+
window.addEventListener(
138+
unloadEvent,
139+
function() {
140+
optimizely.close();
141+
},
142+
false
143+
);
152144
}
153-
154-
return optimizely;
155145
} catch (e) {
156-
logger.error(e);
157-
return null;
146+
logger.error(enums.LOG_MESSAGES.UNABLE_TO_ATTACH_UNLOAD, MODULE_NAME, e.message);
158147
}
159-
},
160148

161-
__internalResetRetryState: function() {
162-
hasRetriedEvents = false;
163-
},
149+
return optimizely;
150+
} catch (e) {
151+
logger.error(e);
152+
return null;
153+
}
154+
};
155+
156+
export var __internalResetRetryState = function() {
157+
hasRetriedEvents = false;
158+
};
159+
160+
/**
161+
* Entry point into the Optimizely Browser SDK
162+
*/
163+
export default {
164+
logging,
165+
errorHandler,
166+
eventDispatcher,
167+
enums,
168+
setLogger,
169+
setLogLevel,
170+
createInstance,
171+
__internalResetRetryState,
164172
};

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019, Optimizely
2+
* Copyright 2016-2019, 2020 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,20 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var logging = require('@optimizely/js-sdk-logging');
17-
var configValidator = require('./utils/config_validator');
18-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
19-
var Optimizely = require('./optimizely');
20-
var optimizelyFactory = require('./index.browser');
21-
var packageJSON = require('../package.json');
22-
var testData = require('./tests/test_data');
23-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
24-
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
25-
26-
var chai = require('chai');
16+
import * as logging from '@optimizely/js-sdk-logging';
17+
import configValidator from './utils/config_validator';
18+
import Optimizely from './optimizely';
19+
import optimizelyFactory from './index.browser';
20+
import packageJSON from '../package.json';
21+
import testData from './tests/test_data';
22+
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
23+
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
24+
import chai from 'chai';
25+
import sinon from 'sinon';
2726
var assert = chai.assert;
28-
var sinon = require('sinon');
29-
3027
var LocalStoragePendingEventsDispatcher = eventProcessor.LocalStoragePendingEventsDispatcher;
3128

3229
describe('javascript-sdk', function() {
@@ -57,7 +54,6 @@ describe('javascript-sdk', function() {
5754
sinon.stub(configValidator, 'validate');
5855

5956
xhr = sinon.useFakeXMLHttpRequest();
60-
global.XMLHttpRequest = xhr;
6157
requests = [];
6258
xhr.onCreate = function(req) {
6359
requests.push(req);

0 commit comments

Comments
 (0)