@@ -219,7 +219,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
219
219
*/
220
220
constructor ( config : optimizely . Config ) {
221
221
this . initialConfig = config ;
222
- this . userPromiseResolver = ( ) => { } ;
222
+ this . userPromiseResolver = ( ) => { } ;
223
223
224
224
const configWithClientInfo = {
225
225
...config ,
@@ -244,9 +244,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
244
244
} ) ;
245
245
246
246
this . dataReadyPromise = Promise . all ( [ this . userPromise , this . _client ! . onReady ( ) ] ) . then ( res => {
247
- if ( ! res [ 0 ] . success ) {
248
- return res [ 0 ] ;
249
- }
250
247
251
248
// Client and user can become ready synchronously and/or asynchronously. This flag specifically indicates that they became ready asynchronously.
252
249
this . isReadyPromiseFulfilled = true ;
@@ -309,8 +306,18 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
309
306
} , timeout ) as any ;
310
307
} ) ;
311
308
312
- return Promise . race ( [ this . dataReadyPromise , timeoutPromise ] ) . then ( res => {
309
+ return Promise . race ( [ this . dataReadyPromise , timeoutPromise ] ) . then ( async res => {
313
310
clearTimeout ( timeoutId ) ;
311
+ if ( res . success ) {
312
+ const isSegmentsFetched = await this . fetchQualifiedSegments ( ) ;
313
+ if ( ! isSegmentsFetched ) {
314
+ return {
315
+ success : false ,
316
+ reason : 'USER_NOT_READY' ,
317
+ message : 'Failed to fetch qualified segments' ,
318
+ }
319
+ }
320
+ }
314
321
return res ;
315
322
} ) ;
316
323
}
@@ -357,7 +364,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
357
364
}
358
365
359
366
public async setUser ( userInfo : UserInfo ) : Promise < void > {
360
- this . isUserPromiseResolved = false ;
361
367
this . isUserReady = true ;
362
368
363
369
//reset user info
@@ -377,16 +383,13 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
377
383
if ( userInfo . attributes ) {
378
384
this . user . attributes = userInfo . attributes ;
379
385
}
380
- const isSegmentsFetched = await this . fetchQualifiedSegments ( ) ;
381
386
382
- const segmentsResult : ResolveResult = { success : isSegmentsFetched } ;
383
- if ( ! isSegmentsFetched ) {
384
- segmentsResult . reason = 'USER_NOT_READY' ;
385
- segmentsResult . message = 'Failed to fetch qualified segments' ;
387
+ if ( this . getIsReadyPromiseFulfilled ( ) ) {
388
+ await this . fetchQualifiedSegments ( ) ;
386
389
}
387
390
388
391
if ( ! this . isUserPromiseResolved ) {
389
- this . userPromiseResolver ( segmentsResult ) ;
392
+ this . userPromiseResolver ( { success : true } ) ;
390
393
this . isUserPromiseResolved = true ;
391
394
}
392
395
0 commit comments