15
15
***************************************************************************/
16
16
import { sprintf , objectValues } from '@optimizely/js-sdk-utils' ;
17
17
import { LogHandler , ErrorHandler } from '@optimizely/js-sdk-logging' ;
18
- import { LogTierV1EventProcessorConfig } from '../../../event-processor/src' ;
19
18
import * as eventProcessor from '@optimizely/js-sdk-event-processor' ;
20
19
import { FeatureFlag , FeatureVariable } from '../core/project_config/entities' ;
21
20
import { EventDispatcher } from '@optimizely/js-sdk-event-processor' ;
22
- import { UserAttributes , Variation , EventTags , OptimizelyConfig } from '../shared_types' ;
21
+ import {
22
+ UserAttributes ,
23
+ Variation ,
24
+ EventTags ,
25
+ OptimizelyConfig ,
26
+ LogTierV1EventProcessorConfig
27
+ } from '../shared_types' ;
23
28
import { createProjectConfigManager , ProjectConfigManager } from '../core/project_config/project_config_manager' ;
24
29
import { createNotificationCenter , NotificationCenter } from '../core/notification_center' ;
25
30
import { createDecisionService , DecisionService } from '../core/decision_service' ;
@@ -77,88 +82,87 @@ export default class Optimizely {
77
82
78
83
constructor ( config : projectConfig . ProjectConfig ) {
79
84
let clientEngine = config . clientEngine ;
80
- if ( enums . VALID_CLIENT_ENGINES . indexOf ( clientEngine ) === - 1 ) {
81
- config . logger . log (
82
- LOG_LEVEL . INFO ,
83
- sprintf ( LOG_MESSAGES . INVALID_CLIENT_ENGINE , MODULE_NAME , clientEngine )
84
- ) ;
85
- clientEngine = enums . NODE_CLIENT_ENGINE ;
86
- }
87
-
88
- this . clientEngine = clientEngine ;
89
- this . clientVersion = config . clientVersion || enums . NODE_CLIENT_VERSION ;
90
- this . errorHandler = config . errorHandler ;
91
- this . eventDispatcher = config . eventDispatcher ;
92
- this . __isOptimizelyConfigValid = config . isValidInstance ;
93
- this . logger = config . logger ;
94
-
95
- this . projectConfigManager = createProjectConfigManager ( {
96
- datafile : config . datafile ,
97
- datafileOptions : config . datafileOptions ,
98
- jsonSchemaValidator : config . jsonSchemaValidator ,
99
- sdkKey : config . sdkKey ,
100
- } ) ;
101
-
102
- this . __disposeOnUpdate = this . projectConfigManager . onUpdate (
103
- function ( this : Optimizely , configObj : projectConfig . ProjectConfig ) {
104
- this . logger . log (
85
+ if ( enums . VALID_CLIENT_ENGINES . indexOf ( clientEngine ) === - 1 ) {
86
+ config . logger . log (
105
87
LOG_LEVEL . INFO ,
106
- sprintf ( LOG_MESSAGES . UPDATED_OPTIMIZELY_CONFIG , MODULE_NAME , configObj . revision , configObj . projectId )
88
+ sprintf ( LOG_MESSAGES . INVALID_CLIENT_ENGINE , MODULE_NAME , clientEngine )
107
89
) ;
108
- this . notificationCenter . sendNotifications ( NOTIFICATION_TYPES . OPTIMIZELY_CONFIG_UPDATE ) ;
109
- } . bind ( this )
110
- ) ;
90
+ clientEngine = enums . NODE_CLIENT_ENGINE ;
91
+ }
111
92
112
- const projectConfigManagerReadyPromise = this . projectConfigManager . onReady ( ) ;
93
+ this . clientEngine = clientEngine ;
94
+ this . clientVersion = config . clientVersion || enums . NODE_CLIENT_VERSION ;
95
+ this . errorHandler = config . errorHandler ;
96
+ this . eventDispatcher = config . eventDispatcher ;
97
+ this . __isOptimizelyConfigValid = config . isValidInstance ;
98
+ this . logger = config . logger ;
99
+
100
+ this . projectConfigManager = createProjectConfigManager ( {
101
+ datafile : config . datafile ,
102
+ datafileOptions : config . datafileOptions ,
103
+ jsonSchemaValidator : config . jsonSchemaValidator ,
104
+ sdkKey : config . sdkKey ,
105
+ } ) ;
113
106
114
- let userProfileService = null ;
115
- if ( config . userProfileService ) {
116
- try {
117
- if ( userProfileServiceValidator . validate ( config . userProfileService ) ) {
118
- userProfileService = config . userProfileService ;
119
- this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . VALID_USER_PROFILE_SERVICE , MODULE_NAME ) ) ;
107
+ this . __disposeOnUpdate = this . projectConfigManager . onUpdate (
108
+ function ( this : Optimizely , configObj : projectConfig . ProjectConfig ) {
109
+ this . logger . log (
110
+ LOG_LEVEL . INFO ,
111
+ sprintf ( LOG_MESSAGES . UPDATED_OPTIMIZELY_CONFIG , MODULE_NAME , configObj . revision , configObj . projectId )
112
+ ) ;
113
+ this . notificationCenter . sendNotifications ( NOTIFICATION_TYPES . OPTIMIZELY_CONFIG_UPDATE ) ;
114
+ } . bind ( this )
115
+ ) ;
116
+
117
+ const projectConfigManagerReadyPromise = this . projectConfigManager . onReady ( ) ;
118
+
119
+ let userProfileService = null ;
120
+ if ( config . userProfileService ) {
121
+ try {
122
+ if ( userProfileServiceValidator . validate ( config . userProfileService ) ) {
123
+ userProfileService = config . userProfileService ;
124
+ this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . VALID_USER_PROFILE_SERVICE , MODULE_NAME ) ) ;
125
+ }
126
+ } catch ( ex ) {
127
+ this . logger . log ( LOG_LEVEL . WARNING , ex . message ) ;
120
128
}
121
- } catch ( ex ) {
122
- this . logger . log ( LOG_LEVEL . WARNING , ex . message ) ;
123
129
}
124
- }
125
130
126
- this . decisionService = createDecisionService ( {
127
- userProfileService : userProfileService ,
128
- logger : this . logger ,
129
- UNSTABLE_conditionEvaluators : config . UNSTABLE_conditionEvaluators ,
130
- } ) ;
131
-
132
- this . notificationCenter = createNotificationCenter ( {
133
- logger : this . logger ,
134
- errorHandler : this . errorHandler ,
135
- } ) ;
136
-
137
- this . eventProcessor = new eventProcessor . LogTierV1EventProcessor ( {
138
- dispatcher : this . eventDispatcher ,
139
- flushInterval : config . eventFlushInterval ,
140
- batchSize : config . eventBatchSize ,
141
- maxQueueSize : config . eventMaxQueueSize , // TODO: update event-processor to include maxQueueSize
142
- notificationCenter : this . notificationCenter ,
143
- } as LogTierV1EventProcessorConfig ) ;
144
-
145
-
146
- const eventProcessorStartedPromise = this . eventProcessor . start ( ) ;
147
-
148
- this . __readyPromise = Promise . all ( [ projectConfigManagerReadyPromise , eventProcessorStartedPromise ] ) . then ( function ( promiseResults ) {
149
- // Only return status from project config promise because event processor promise does not return any status.
150
- return promiseResults [ 0 ] ;
151
- } )
152
-
153
- this . __readyTimeouts = { } ;
154
- this . __nextReadyTimeoutId = 0 ;
131
+ this . decisionService = createDecisionService ( {
132
+ userProfileService : userProfileService ,
133
+ logger : this . logger ,
134
+ UNSTABLE_conditionEvaluators : config . UNSTABLE_conditionEvaluators ,
135
+ } ) ;
136
+
137
+ this . notificationCenter = createNotificationCenter ( {
138
+ logger : this . logger ,
139
+ errorHandler : this . errorHandler ,
140
+ } ) ;
141
+
142
+ this . eventProcessor = new eventProcessor . LogTierV1EventProcessor ( {
143
+ dispatcher : this . eventDispatcher ,
144
+ flushInterval : config . eventFlushInterval ,
145
+ batchSize : config . eventBatchSize ,
146
+ maxQueueSize : config . eventMaxQueueSize , // TODO: update event-processor to include maxQueueSize
147
+ notificationCenter : this . notificationCenter ,
148
+ } as LogTierV1EventProcessorConfig ) ;
149
+
150
+ const eventProcessorStartedPromise = this . eventProcessor . start ( ) ;
151
+
152
+ this . __readyPromise = Promise . all ( [ projectConfigManagerReadyPromise , eventProcessorStartedPromise ] ) . then ( function ( promiseResults ) {
153
+ // Only return status from project config promise because event processor promise does not return any status.
154
+ return promiseResults [ 0 ] ;
155
+ } )
156
+
157
+ this . __readyTimeouts = { } ;
158
+ this . __nextReadyTimeoutId = 0 ;
155
159
}
156
160
157
161
/**
158
162
* Returns a truthy value if this instance currently has a valid project config
159
163
* object, and the initial configuration object that was passed into the
160
164
* constructor was also valid.
161
- * @return {* }
165
+ * @return {boolean }
162
166
*/
163
167
__isValidInstance ( ) : boolean {
164
168
return this . __isOptimizelyConfigValid && ! ! this . projectConfigManager . getConfig ( ) ;
@@ -451,10 +455,11 @@ export default class Optimizely {
451
455
452
456
/**
453
457
* Force a user into a variation for a given experiment.
454
- * @param {string } experimentKey
455
- * @param {string } userId
456
- * @param {string|null } variationKey user will be forced into. If null, then clear the existing experiment-to-variation mapping.
457
- * @return {boolean } A boolean value that indicates if the set completed successfully.
458
+ * @param {string } experimentKey
459
+ * @param {string } userId
460
+ * @param {string|null } variationKey user will be forced into. If null,
461
+ * then clear the existing experiment-to-variation mapping.
462
+ * @return {boolean } A boolean value that indicates if the set completed successfully.
458
463
*/
459
464
setForcedVariation ( experimentKey : string , userId : string , variationKey : string | null ) : boolean {
460
465
if ( ! this . __validateInputs ( { experiment_key : experimentKey , user_id : userId } ) ) {
@@ -477,8 +482,8 @@ export default class Optimizely {
477
482
478
483
/**
479
484
* Gets the forced variation for a given user and experiment.
480
- * @param {string } experimentKey
481
- * @param {string } userId
485
+ * @param {string } experimentKey
486
+ * @param {string } userId
482
487
* @return {string|null } The forced variation key.
483
488
*/
484
489
getForcedVariation ( experimentKey : string , userId : string ) : string | null {
@@ -505,7 +510,7 @@ export default class Optimizely {
505
510
* @param {unknown } stringInputs Map of string keys and associated values
506
511
* @param {unknown } userAttributes Optional parameter for user's attributes
507
512
* @param {unknown } eventTags Optional parameter for event tags
508
- * @return {boolean } True if inputs are valid
513
+ * @return {boolean } True if inputs are valid
509
514
*
510
515
*/
511
516
__validateInputs (
@@ -720,8 +725,9 @@ export default class Optimizely {
720
725
* being accessed
721
726
* @param {string } userId ID for the user
722
727
* @param {UserAttributes } attributes Optional user attributes
723
- * @return {unknown } Value of the variable cast to the appropriate
724
- * type, or null if the feature key is invalid or the variable key is invalid
728
+ * @return {unknown } Value of the variable cast to the appropriate
729
+ * type, or null if the feature key is invalid or
730
+ * the variable key is invalid
725
731
*/
726
732
727
733
getFeatureVariable (
0 commit comments