1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- import {
16+ import {
1717 getLogger ,
1818 setLogHandler ,
1919 setLogLevel ,
2020 setErrorHandler ,
2121 getErrorHandler ,
22- LogLevel ,
22+ LogLevel
2323} from '@optimizely/js-sdk-logging' ;
2424import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor' ;
25-
26- import fns from './utils/fns' ;
27- import * as configValidator from './utils/config_validator' ;
25+ import configValidator from './utils/config_validator' ;
2826import defaultErrorHandler from './plugins/error_handler' ;
2927import defaultEventDispatcher from './plugins/event_dispatcher/index.browser' ;
3028import * as enums from './utils/enums' ;
3129import loggerPlugin from './plugins/logger' ;
3230import Optimizely from './optimizely' ;
3331import eventProcessorConfigValidator from './utils/event_processor_config_validator' ;
32+ import { SDKOptions } from './shared_types' ;
3433
35- var logger = getLogger ( ) ;
34+ const logger = getLogger ( ) ;
3635setLogHandler ( loggerPlugin . createLogger ( ) ) ;
3736setLogLevel ( LogLevel . INFO ) ;
3837
39- var MODULE_NAME = 'INDEX_BROWSER' ;
40- var DEFAULT_EVENT_BATCH_SIZE = 10 ;
41- var DEFAULT_EVENT_FLUSH_INTERVAL = 1000 ; // Unit is ms, default is 1s
38+ const MODULE_NAME = 'INDEX_BROWSER' ;
39+ const DEFAULT_EVENT_BATCH_SIZE = 10 ;
40+ const DEFAULT_EVENT_FLUSH_INTERVAL = 1000 ; // Unit is ms, default is 1s
4241
43- var hasRetriedEvents = false ;
42+ let hasRetriedEvents = false ;
4443
4544/**
4645 * Creates an instance of the Optimizely class
47- * @param {Object } config
48- * @param {Object|string } config.datafile
49- * @param {Object } config.errorHandler
50- * @param {Object } config.eventDispatcher
51- * @param {Object } config.logger
52- * @param {Object } config.logLevel
53- * @param {Object } config.userProfileService
54- * @param {Object } config.eventBatchSize
55- * @param {Object } config.eventFlushInterval
56- * @param {string } config.sdkKey
57- * @return {Object } the Optimizely object
46+ * @param {SDKOptions } config
47+ * @return {Optimizely|null } the Optimizely object
48+ * null on error
5849 */
59- var createInstance = function ( config ) {
50+ const createInstance = function ( config : SDKOptions ) : Optimizely | null {
6051 try {
61- config = config || { } ;
6252
6353 // TODO warn about setting per instance errorHandler / logger / logLevel
6454 if ( config . errorHandler ) {
@@ -81,7 +71,7 @@ var createInstance = function(config) {
8171 config . isValidInstance = false ;
8272 }
8373
84- var eventDispatcher ;
74+ let eventDispatcher ;
8575 // prettier-ignore
8676 if ( config . eventDispatcher == null ) { // eslint-disable-line eqeqeq
8777 // only wrap the event dispatcher with pending events retry if the user didnt override
@@ -97,42 +87,40 @@ var createInstance = function(config) {
9787 eventDispatcher = config . eventDispatcher ;
9888 }
9989
100- config = fns . assign (
101- {
102- clientEngine : enums . JAVASCRIPT_CLIENT_ENGINE ,
103- eventBatchSize : DEFAULT_EVENT_BATCH_SIZE ,
104- eventFlushInterval : DEFAULT_EVENT_FLUSH_INTERVAL ,
105- } ,
106- config ,
107- {
108- eventDispatcher : eventDispatcher ,
109- // always get the OptimizelyLogger facade from logging
110- logger : logger ,
111- errorHandler : getErrorHandler ( ) ,
112- }
113- ) ;
90+ let eventBatchSize = config . eventBatchSize ;
91+ let eventFlushInterval = config . eventFlushInterval ;
11492
11593 if ( ! eventProcessorConfigValidator . validateEventBatchSize ( config . eventBatchSize ) ) {
11694 logger . warn ( 'Invalid eventBatchSize %s, defaulting to %s' , config . eventBatchSize , DEFAULT_EVENT_BATCH_SIZE ) ;
117- config . eventBatchSize = DEFAULT_EVENT_BATCH_SIZE ;
95+ eventBatchSize = DEFAULT_EVENT_BATCH_SIZE ;
11896 }
11997 if ( ! eventProcessorConfigValidator . validateEventFlushInterval ( config . eventFlushInterval ) ) {
12098 logger . warn (
12199 'Invalid eventFlushInterval %s, defaulting to %s' ,
122100 config . eventFlushInterval ,
123101 DEFAULT_EVENT_FLUSH_INTERVAL
124102 ) ;
125- config . eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL ;
103+ eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL ;
126104 }
127105
128- var optimizely = new Optimizely ( config ) ;
106+ const optimizelyOptions = {
107+ clientEngine : enums . JAVASCRIPT_CLIENT_ENGINE ,
108+ eventDispatcher : eventDispatcher ,
109+ ...config ,
110+ eventBatchSize : eventBatchSize ,
111+ eventFlushInterval : eventFlushInterval ,
112+ logger : logger ,
113+ errorHandler : getErrorHandler ( )
114+ } ;
115+
116+ const optimizely = new Optimizely ( optimizelyOptions ) ;
129117
130118 try {
131119 if ( typeof window . addEventListener === 'function' ) {
132- var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload' ;
120+ const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload' ;
133121 window . addEventListener (
134122 unloadEvent ,
135- function ( ) {
123+ ( ) => {
136124 optimizely . close ( ) ;
137125 } ,
138126 false
@@ -149,7 +137,7 @@ var createInstance = function(config) {
149137 }
150138} ;
151139
152- var __internalResetRetryState = function ( ) {
140+ const __internalResetRetryState = function ( ) : void {
153141 hasRetriedEvents = false ;
154142} ;
155143
@@ -164,16 +152,16 @@ export {
164152 setLogHandler as setLogger ,
165153 setLogLevel ,
166154 createInstance ,
167- __internalResetRetryState ,
168- }
155+ __internalResetRetryState ,
156+ } ;
169157
170158export default {
171159 logging : loggerPlugin ,
172160 errorHandler : defaultErrorHandler ,
173161 eventDispatcher : defaultEventDispatcher ,
174- enums : enums ,
162+ enums,
175163 setLogger : setLogHandler ,
176- setLogLevel : setLogLevel ,
177- createInstance : createInstance ,
178- __internalResetRetryState : __internalResetRetryState ,
164+ setLogLevel,
165+ createInstance,
166+ __internalResetRetryState,
179167} ;
0 commit comments