@@ -74,7 +74,7 @@ const Flagsmith = class {
74
74
}
75
75
}
76
76
77
- getFlags = ( ) => {
77
+ _getFlags = ( ) => {
78
78
const { identity, api } = this ;
79
79
this . log ( "Get Flags" )
80
80
this . isLoading = true ;
@@ -116,6 +116,7 @@ const Flagsmith = class {
116
116
this . flags = flags ;
117
117
this . traits = userTraits ;
118
118
this . updateStorage ( ) ;
119
+ this . initializing = false ;
119
120
this . _onChange ( this . oldFlags , {
120
121
isFromServer : true ,
121
122
flagsChanged,
@@ -205,6 +206,24 @@ const Flagsmith = class {
205
206
}
206
207
} ;
207
208
209
+ getFlags = ( ) => {
210
+ if ( this . initializing ) {
211
+ const flags = new Promise ( ( ) => {
212
+ const interval = setInterval ( async ( resolve ) => {
213
+ if ( this . initialised && ! this . initializing ) {
214
+ clearInterval ( interval )
215
+ return this . _getFlags ( ) . then ( resolve )
216
+ }
217
+ } , 3000 )
218
+ } )
219
+ return flags
220
+ }
221
+ if ( ! this . initialised || this . initializing ) {
222
+ return Promise . resolve ( ) ;
223
+ }
224
+ return this . _getFlags ( )
225
+ }
226
+
208
227
analyticsFlags = ( ) => {
209
228
const { api } = this ;
210
229
@@ -246,6 +265,7 @@ const Flagsmith = class {
246
265
getFlagInterval : NodeJS . Timer | null = null
247
266
headers ?: object | null = null
248
267
initialised = false
268
+ initializing = true
249
269
oldFlags :IFlags | null = null
250
270
onChange :IInitConfig [ 'onChange' ] | null = null
251
271
onError :IInitConfig [ 'onError' ] | null = null
@@ -437,19 +457,19 @@ const Flagsmith = class {
437
457
}
438
458
if ( shouldFetchFlags ) {
439
459
// We want to resolve init since we have cached flags
440
- this . getFlags ( ) ;
460
+ this . _getFlags ( ) ;
441
461
}
442
462
} else {
443
463
if ( ! preventFetch ) {
444
- await this . getFlags ( ) ;
464
+ await this . _getFlags ( ) ;
445
465
}
446
466
}
447
467
} catch ( e ) {
448
468
this . log ( "Exception fetching cached logs" , e ) ;
449
469
}
450
470
} else {
451
471
if ( ! preventFetch ) {
452
- await this . getFlags ( ) ;
472
+ await this . _getFlags ( ) ;
453
473
} else {
454
474
if ( defaultFlags ) {
455
475
this . _onChange ( null ,
@@ -473,7 +493,7 @@ const Flagsmith = class {
473
493
} catch ( e ) { }
474
494
}
475
495
} else if ( ! preventFetch ) {
476
- await this . getFlags ( ) ;
496
+ await this . _getFlags ( ) ;
477
497
} else {
478
498
if ( defaultFlags ) {
479
499
this . _onChange ( null , { isFromServer : false , flagsChanged : getChanges ( { } , defaultFlags ) , traitsChanged : getChanges ( { } , traits ) } , this . _loadedState ( null , FlagSource . DEFAULT_FLAGS ) ) ;
@@ -506,7 +526,21 @@ const Flagsmith = class {
506
526
}
507
527
508
528
getAllFlags ( ) {
509
- return this . flags ;
529
+ if ( this . initialised ) {
530
+ if ( this . initializing ) {
531
+ const flags = new Promise ( ( resolve ) => {
532
+ const interval = setInterval ( ( ) => {
533
+ if ( this . initialised && ! this . initializing ) {
534
+ clearInterval ( interval )
535
+ return resolve ( true )
536
+ }
537
+ } , 3000 )
538
+ } )
539
+ return flags . then ( ( ) => { return this . flags } )
540
+ } else {
541
+ return this . flags ;
542
+ }
543
+ }
510
544
}
511
545
512
546
identify ( userId : string , traits ?: ITraits ) {
@@ -523,8 +557,8 @@ const Flagsmith = class {
523
557
...traits
524
558
} ;
525
559
}
526
- if ( this . initialised ) {
527
- return this . getFlags ( ) ;
560
+ if ( this . initialised && ! this . initializing ) {
561
+ return this . _getFlags ( ) ;
528
562
}
529
563
return Promise . resolve ( ) ;
530
564
}
@@ -561,8 +595,8 @@ const Flagsmith = class {
561
595
logout ( ) {
562
596
this . identity = null ;
563
597
this . traits = { } ;
564
- if ( this . initialised ) {
565
- return this . getFlags ( ) ;
598
+ if ( this . initialised && ! this . initializing ) {
599
+ return this . _getFlags ( ) ;
566
600
}
567
601
return Promise . resolve ( ) ;
568
602
}
@@ -571,7 +605,7 @@ const Flagsmith = class {
571
605
if ( this . getFlagInterval ) {
572
606
clearInterval ( this . getFlagInterval ) ;
573
607
}
574
- this . getFlagInterval = setInterval ( this . getFlags , ticks ) ;
608
+ this . getFlagInterval = setInterval ( this . _getFlags , ticks ) ;
575
609
}
576
610
577
611
stopListening ( ) {
@@ -651,8 +685,8 @@ const Flagsmith = class {
651
685
this . log ( "Set traits prior to identifying" , this . withTraits ) ;
652
686
return
653
687
}
654
- if ( this . initialised ) {
655
- return this . getFlags ( )
688
+ if ( this . initialised && ! this . initializing ) {
689
+ return this . _getFlags ( )
656
690
}
657
691
} ;
658
692
@@ -810,7 +844,7 @@ const Flagsmith = class {
810
844
this . log ( 'updated_at is new, but flags are loading' , e . data , this . timestamp ) ;
811
845
} else {
812
846
this . log ( 'updated_at is new, fetching flags' , e . data , this . timestamp ) ;
813
- this . getFlags ( ) ;
847
+ this . _getFlags ( ) ;
814
848
}
815
849
} else {
816
850
this . log ( 'updated_at is outdated, skipping get flags' , e . data , this . timestamp ) ;
0 commit comments