@@ -77,7 +77,8 @@ import {
7777 NOT_TRACKING_USER ,
7878 VARIABLE_REQUESTED_WITH_WRONG_TYPE ,
7979 ONREADY_TIMEOUT ,
80- INSTANCE_CLOSED
80+ INSTANCE_CLOSED ,
81+ SERVICE_STOPPED_BEFORE_RUNNING
8182} from 'error_message' ;
8283
8384import {
@@ -133,10 +134,8 @@ export type OptimizelyOptions = {
133134}
134135
135136export default class Optimizely extends BaseService implements Client {
136- // readyTimeout is specified as any to make this work in both browser & Node
137- // eslint-disable-next-line @typescript-eslint/no-explicit-any
138- private readyTimeouts : { [ key : string ] : { readyTimeout : any ; onClose : ( ) => void } } ;
139- private nextReadyTimeoutId : number ;
137+ private cleanupTasks : Map < number , Fn > = new Map ( ) ;
138+ private nextCleanupTaskId = 0 ;
140139 private clientEngine : string ;
141140 private clientVersion : string ;
142141 private errorNotifier ?: ErrorNotifier ;
@@ -233,22 +232,25 @@ export default class Optimizely extends BaseService implements Client {
233232
234233 this . notificationCenter = createNotificationCenter ( { logger : this . logger , errorNotifier : this . errorNotifier } ) ;
235234
236- this . readyTimeouts = { } ;
237- this . nextReadyTimeoutId = 0 ;
238-
239235 this . start ( ) ;
240236 }
241237
242238 start ( ) : void {
239+ if ( ! this . isNew ( ) ) {
240+ return ;
241+ }
242+
243243 super . start ( ) ;
244244
245245 this . state = ServiceState . Starting ;
246246 this . projectConfigManager . start ( ) ;
247247 this . eventProcessor ?. start ( ) ;
248248 this . odpManager ?. start ( ) ;
249249
250+ const configManOnRunning = this . projectConfigManager . onRunning ( ) ;
251+
250252 Promise . all ( [
251- this . projectConfigManager . onRunning ( ) ,
253+ configManOnRunning ,
252254 this . eventProcessor ? this . eventProcessor . onRunning ( ) : Promise . resolve ( ) ,
253255 this . odpManager ? this . odpManager . onRunning ( ) : Promise . resolve ( ) ,
254256 this . vuidManager ? this . vuidManager . initialize ( ) : Promise . resolve ( ) ,
@@ -260,11 +262,13 @@ export default class Optimizely extends BaseService implements Client {
260262 if ( vuid ) {
261263 this . odpManager ?. setVuid ( vuid ) ;
262264 }
265+ } , ( err ) => {
266+ this . state = ServiceState . Failed ;
267+ this . errorReporter . report ( err ) ;
268+ this . startPromise . reject ( err ) ;
263269 } ) ;
264270 }
265271
266-
267-
268272 /**
269273 * Returns the project configuration retrieved from projectConfigManager
270274 * @return {projectConfig.ProjectConfig }
@@ -1233,24 +1237,30 @@ export default class Optimizely extends BaseService implements Client {
12331237 *
12341238 * @return {Promise }
12351239 */
1236- close ( ) : Promise < unknown > {
1240+ close ( ) : Promise < unknown > {
12371241 this . stop ( ) ;
12381242 return this . onTerminated ( ) ;
12391243 }
12401244
12411245 stop ( ) : void {
1246+ if ( this . isDone ( ) ) {
1247+ return ;
1248+ }
1249+
1250+ this . startPromise . promise . then ( ( ) => { } , ( err ) => { } ) ;
1251+
1252+ if ( ! this . isRunning ( ) ) {
1253+ this . startPromise . reject ( new OptimizelyError ( SERVICE_STOPPED_BEFORE_RUNNING ) ) ;
1254+ }
1255+
12421256 this . state = ServiceState . Stopping ;
12431257
12441258 this . projectConfigManager . stop ( ) ;
12451259 this . eventProcessor ?. stop ( ) ;
12461260 this . odpManager ?. stop ( ) ;
12471261 this . notificationCenter . clearAllNotificationListeners ( ) ;
12481262
1249- Object . keys ( this . readyTimeouts ) . forEach ( ( readyTimeoutId : string ) => {
1250- const readyTimeoutRecord = this . readyTimeouts [ readyTimeoutId ] ;
1251- clearTimeout ( readyTimeoutRecord . readyTimeout ) ;
1252- readyTimeoutRecord . onClose ( ) ;
1253- } ) ;
1263+ this . cleanupTasks . forEach ( ( onClose ) => onClose ( ) ) ;
12541264
12551265 Promise . all ( [
12561266 this . projectConfigManager . onTerminated ( ) ,
@@ -1305,30 +1315,26 @@ export default class Optimizely extends BaseService implements Client {
13051315 }
13061316
13071317 const timeoutPromise = resolvablePromise ( ) ;
1318+ timeoutPromise . promise . catch ( ( ) => { } ) ;
13081319
1309- const timeoutId = this . nextReadyTimeoutId ++ ;
1320+ const cleanupTaskId = this . nextCleanupTaskId ++ ;
13101321
13111322 const onReadyTimeout = ( ) => {
1312- delete this . readyTimeouts [ timeoutId ] ;
1323+ this . cleanupTasks . delete ( cleanupTaskId ) ;
13131324 timeoutPromise . reject ( new OptimizelyError ( ONREADY_TIMEOUT , timeoutValue ) ) ;
13141325 } ;
13151326
13161327 const readyTimeout = setTimeout ( onReadyTimeout , timeoutValue ) ;
1317- const onClose = function ( ) {
1318- timeoutPromise . reject ( new OptimizelyError ( INSTANCE_CLOSED ) ) ;
1319- } ;
1320-
1321- this . readyTimeouts [ timeoutId ] = {
1322- readyTimeout : readyTimeout ,
1323- onClose : onClose ,
1324- } ;
1325-
1326- this . onRunning ( ) . then ( ( ) => {
1328+
1329+ this . cleanupTasks . set ( cleanupTaskId , ( ) => {
13271330 clearTimeout ( readyTimeout ) ;
1328- delete this . readyTimeouts [ timeoutId ] ;
1331+ timeoutPromise . reject ( new OptimizelyError ( INSTANCE_CLOSED ) ) ;
13291332 } ) ;
13301333
1331- return Promise . race ( [ this . onRunning ( ) , timeoutPromise ] ) ;
1334+ return Promise . race ( [ this . onRunning ( ) . then ( ( ) => {
1335+ clearTimeout ( readyTimeout ) ;
1336+ this . cleanupTasks . delete ( cleanupTaskId ) ;
1337+ } ) , timeoutPromise ] ) ;
13321338 }
13331339
13341340 //============ decide ============//
@@ -1351,12 +1357,19 @@ export default class Optimizely extends BaseService implements Client {
13511357 return null ;
13521358 }
13531359
1354- return new OptimizelyUserContext ( {
1360+ const userContext = new OptimizelyUserContext ( {
13551361 optimizely : this ,
13561362 userId : userIdentifier ,
13571363 attributes,
1358- shouldIdentifyUser : true ,
13591364 } ) ;
1365+
1366+ this . onRunning ( ) . then ( ( ) => {
1367+ if ( this . odpManager && this . isOdpIntegrated ( ) ) {
1368+ this . odpManager . identifyUser ( userIdentifier ) ;
1369+ }
1370+ } ) . catch ( ( ) => { } ) ;
1371+
1372+ return userContext ;
13601373 }
13611374
13621375 /**
@@ -1375,7 +1388,6 @@ export default class Optimizely extends BaseService implements Client {
13751388 optimizely : this ,
13761389 userId,
13771390 attributes,
1378- shouldIdentifyUser : false ,
13791391 } ) ;
13801392 }
13811393
@@ -1662,9 +1674,7 @@ export default class Optimizely extends BaseService implements Client {
16621674 * @param {string } userId
16631675 */
16641676 public identifyUser ( userId : string ) : void {
1665- if ( this . odpManager && this . isOdpIntegrated ( ) ) {
1666- this . odpManager . identifyUser ( userId ) ;
1667- }
1677+
16681678 }
16691679
16701680 /**
0 commit comments