13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- import {
16
+ import {
17
17
getLogger ,
18
18
setLogHandler ,
19
19
setLogLevel ,
20
20
setErrorHandler ,
21
21
getErrorHandler ,
22
- LogLevel ,
22
+ LogLevel
23
23
} from '@optimizely/js-sdk-logging' ;
24
24
import { 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' ;
28
26
import defaultErrorHandler from './plugins/error_handler' ;
29
27
import defaultEventDispatcher from './plugins/event_dispatcher/index.browser' ;
30
28
import * as enums from './utils/enums' ;
31
29
import loggerPlugin from './plugins/logger' ;
32
30
import Optimizely from './optimizely' ;
33
31
import eventProcessorConfigValidator from './utils/event_processor_config_validator' ;
32
+ import { SDKOptions } from './shared_types' ;
34
33
35
- var logger = getLogger ( ) ;
34
+ const logger = getLogger ( ) ;
36
35
setLogHandler ( loggerPlugin . createLogger ( ) ) ;
37
36
setLogLevel ( LogLevel . INFO ) ;
38
37
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
42
41
43
- var hasRetriedEvents = false ;
42
+ let hasRetriedEvents = false ;
44
43
45
44
/**
46
45
* 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
58
49
*/
59
- var createInstance = function ( config ) {
50
+ const createInstance = function ( config : SDKOptions ) : Optimizely | null {
60
51
try {
61
- config = config || { } ;
62
52
63
53
// TODO warn about setting per instance errorHandler / logger / logLevel
64
54
if ( config . errorHandler ) {
@@ -81,7 +71,7 @@ var createInstance = function(config) {
81
71
config . isValidInstance = false ;
82
72
}
83
73
84
- var eventDispatcher ;
74
+ let eventDispatcher ;
85
75
// prettier-ignore
86
76
if ( config . eventDispatcher == null ) { // eslint-disable-line eqeqeq
87
77
// only wrap the event dispatcher with pending events retry if the user didnt override
@@ -97,42 +87,40 @@ var createInstance = function(config) {
97
87
eventDispatcher = config . eventDispatcher ;
98
88
}
99
89
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 ;
114
92
115
93
if ( ! eventProcessorConfigValidator . validateEventBatchSize ( config . eventBatchSize ) ) {
116
94
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 ;
118
96
}
119
97
if ( ! eventProcessorConfigValidator . validateEventFlushInterval ( config . eventFlushInterval ) ) {
120
98
logger . warn (
121
99
'Invalid eventFlushInterval %s, defaulting to %s' ,
122
100
config . eventFlushInterval ,
123
101
DEFAULT_EVENT_FLUSH_INTERVAL
124
102
) ;
125
- config . eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL ;
103
+ eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL ;
126
104
}
127
105
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 ) ;
129
117
130
118
try {
131
119
if ( typeof window . addEventListener === 'function' ) {
132
- var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload' ;
120
+ const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload' ;
133
121
window . addEventListener (
134
122
unloadEvent ,
135
- function ( ) {
123
+ ( ) => {
136
124
optimizely . close ( ) ;
137
125
} ,
138
126
false
@@ -149,7 +137,7 @@ var createInstance = function(config) {
149
137
}
150
138
} ;
151
139
152
- var __internalResetRetryState = function ( ) {
140
+ const __internalResetRetryState = function ( ) : void {
153
141
hasRetriedEvents = false ;
154
142
} ;
155
143
@@ -164,16 +152,16 @@ export {
164
152
setLogHandler as setLogger ,
165
153
setLogLevel ,
166
154
createInstance ,
167
- __internalResetRetryState ,
168
- }
155
+ __internalResetRetryState ,
156
+ } ;
169
157
170
158
export default {
171
159
logging : loggerPlugin ,
172
160
errorHandler : defaultErrorHandler ,
173
161
eventDispatcher : defaultEventDispatcher ,
174
- enums : enums ,
162
+ enums,
175
163
setLogger : setLogHandler ,
176
- setLogLevel : setLogLevel ,
177
- createInstance : createInstance ,
178
- __internalResetRetryState : __internalResetRetryState ,
164
+ setLogLevel,
165
+ createInstance,
166
+ __internalResetRetryState,
179
167
} ;
0 commit comments