File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " @reflag/browser-sdk " : patch
3+ ---
4+
5+ Avoid reporting ` CLIENT_NOT_INITIALIZED ` when a flag is evaluated from the initial flag state while client initialization is completing.
Original file line number Diff line number Diff line change @@ -501,7 +501,6 @@ function shouldShowToolbar(opts: InitOptions) {
501501 */
502502export class ReflagClient {
503503 private state : State = "idle" ;
504- private initializationFinished = false ;
505504 private contextUpdateLoading = false ;
506505 private readonly publishableKey : string ;
507506 private context : ReflagContext ;
@@ -740,7 +739,6 @@ export class ReflagClient {
740739 "ms" +
741740 ( this . config . offline ? " (offline mode)" : "" ) ,
742741 ) ;
743- this . initializationFinished = true ;
744742 this . setState ( "initialized" ) ;
745743 }
746744
@@ -1422,9 +1420,7 @@ export class ReflagClient {
14221420 getFlag ( flagKey : string ) : Flag {
14231421 const f = this . getFlags ( ) [ flagKey ] ;
14241422 const evaluatedBeforeInitialization =
1425- ! this . initializationFinished &&
1426- ! this . config . offline &&
1427- ! this . config . bootstrapped ;
1423+ ! this . flagsClient . hasFlagState ( ) && ! this . config . offline ;
14281424
14291425 // eslint-disable-next-line @typescript-eslint/no-this-alias
14301426 const self = this ;
Original file line number Diff line number Diff line change @@ -298,6 +298,7 @@ export class FlagsClient {
298298 private initialized = false ;
299299 private bootstrapped = false ;
300300 private initializationComplete = false ;
301+ private flagStateAvailable = false ;
301302
302303 private rateLimiter : RateLimiter ;
303304 private readonly logger : Logger ;
@@ -425,6 +426,10 @@ export class FlagsClient {
425426 return this . fetchedFlags ;
426427 }
427428
429+ hasFlagState ( ) : boolean {
430+ return this . flagStateAvailable ;
431+ }
432+
428433 requestOptInFlags ( ) {
429434 this . optInFlagsRequested = true ;
430435
@@ -545,6 +550,8 @@ export class FlagsClient {
545550 triggerEvent = true ,
546551 flagStateVersion ?: number ,
547552 ) {
553+ // Mark the state available before notifying consumers from updateFlags().
554+ this . flagStateAvailable = true ;
548555 // Create a new fetched flags object making sure to clone the flags
549556 this . fetchedFlags = { ...fetchedFlags } ;
550557 this . fetchedFlagStateVersion = flagStateVersion ;
Original file line number Diff line number Diff line change @@ -485,6 +485,35 @@ describe(`sends "check" events `, () => {
485485 ) ;
486486 } ) ;
487487
488+ it ( "does not add initialization diagnostics once flag state is available" , async ( ) => {
489+ const sendCheckEventSpy = vi . spyOn (
490+ FlagsClient . prototype ,
491+ "sendCheckEvent" ,
492+ ) ;
493+ const client = new ReflagClient ( { publishableKey : KEY } ) ;
494+ const evaluatedDuringInitialization = vi . fn ( ) ;
495+
496+ client . on ( "flagsUpdated" , ( ) => {
497+ expect ( client . getState ( ) ) . toBe ( "initializing" ) ;
498+ expect ( client . getFlag ( "flagA" ) . isEnabled ) . toBe ( true ) ;
499+ evaluatedDuringInitialization ( ) ;
500+ } ) ;
501+
502+ await client . initialize ( ) ;
503+
504+ expect ( evaluatedDuringInitialization ) . toHaveBeenCalledOnce ( ) ;
505+ expect ( sendCheckEventSpy ) . toHaveBeenCalledWith (
506+ expect . objectContaining ( {
507+ action : "check-is-enabled" ,
508+ key : "flagA" ,
509+ value : true ,
510+ version : 1 ,
511+ evaluationErrors : flagsResult . flagA . evaluationErrors ,
512+ } ) ,
513+ expect . any ( Function ) ,
514+ ) ;
515+ } ) ;
516+
488517 it ( "does not add initialization diagnostics to bootstrapped evaluations" , ( ) => {
489518 const sendCheckEventSpy = vi . spyOn (
490519 FlagsClient . prototype ,
You can’t perform that action at this time.
0 commit comments