@@ -397,25 +397,21 @@ export class FlagsClient {
397397 this . flagOverrides = { ...cachedOverrides , ...this . flagOverrides } ;
398398 }
399399
400- if ( ! this . bootstrapped ) {
400+ if ( this . bootstrapped ) {
401+ // Apply cached overrides silently, then publish each initial value once.
402+ const previousFlags = this . flags ;
403+ this . updateFlags ( false ) ;
404+ const rawFlagsChanged = previousFlags !== this . flags ;
405+ this . triggerFlagUpdates ( { } ) ;
406+ if ( rawFlagsChanged ) this . triggerFlagsUpdated ( ) ;
407+ } else {
401408 const requestContextVersion = this . contextFetchVersion ;
402409 this . applyFetchedFlagsResult (
403410 await this . maybeFetchFlags ( requestContextVersion ) ,
404411 true ,
405412 requestContextVersion ,
406413 ) ;
407- }
408-
409- // Apply overrides before publishing initial bootstrapped values. Suppress
410- // change events here so overridden flags are not published twice.
411- const previousFlags = this . flags ;
412- this . updateFlags ( ! this . bootstrapped ) ;
413- const flagsChanged = previousFlags !== this . flags ;
414- if ( this . bootstrapped ) {
415- for ( const [ key , flag ] of Object . entries ( this . flags ) ) {
416- this . triggerFlagUpdated ( { key, flag } ) ;
417- }
418- if ( flagsChanged ) this . triggerFlagsUpdated ( ) ;
414+ this . updateFlags ( ) ;
419415 }
420416 initializationSucceeded = true ;
421417 } finally {
@@ -656,27 +652,11 @@ export class FlagsClient {
656652 const updatedFlags = this . mergeFlags ( this . fetchedFlags , this . flagOverrides ) ;
657653 // Nothing has changed, skipping update
658654 if ( deepEqual ( this . flags , updatedFlags ) ) return ;
659- const updates : FlagUpdatedEvent [ ] = [ ] ;
660- if ( triggerEvent ) {
661- const keys = new Set ( [
662- ...Object . keys ( this . flags ) ,
663- ...Object . keys ( updatedFlags ) ,
664- ] ) ;
665- for ( const key of keys ) {
666- const previous = Object . prototype . hasOwnProperty . call ( this . flags , key )
667- ? this . flags [ key ]
668- : undefined ;
669- const flag = Object . prototype . hasOwnProperty . call ( updatedFlags , key )
670- ? updatedFlags [ key ]
671- : undefined ;
672- if ( flagValueChanged ( previous , flag ) ) updates . push ( { key, flag } ) ;
673- }
674- }
675-
655+ const previousFlags = this . flags ;
676656 this . flags = updatedFlags ;
677657 if ( triggerEvent ) {
678- // Raw -data subscribers may synchronously change overrides again.
679- for ( const update of updates ) this . triggerFlagUpdated ( update ) ;
658+ // Publish values before raw -data subscribers can change overrides again.
659+ this . triggerFlagUpdates ( previousFlags ) ;
680660 this . triggerFlagsUpdated ( ) ;
681661 }
682662 }
@@ -1055,8 +1035,22 @@ export class FlagsClient {
10551035 return mergedFlags ;
10561036 }
10571037
1058- private triggerFlagUpdated ( event : FlagUpdatedEvent ) {
1059- for ( const callback of this . flagUpdatedListeners ) callback ( event ) ;
1038+ private triggerFlagUpdates ( previousFlags : RawFlags ) {
1039+ // Maps distinguish missing flags from inherited properties such as constructor.
1040+ const previous = new Map ( Object . entries ( previousFlags ) ) ;
1041+ const current = new Map ( Object . entries ( this . flags ) ) ;
1042+ const keys = new Set ( [ ...previous . keys ( ) , ...current . keys ( ) ] ) ;
1043+ const updates : FlagUpdatedEvent [ ] = [ ] ;
1044+ for ( const key of keys ) {
1045+ const flag = current . get ( key ) ;
1046+ if ( flagValueChanged ( previous . get ( key ) , flag ) )
1047+ updates . push ( { key, flag } ) ;
1048+ }
1049+
1050+ // Capture the whole batch before callbacks can synchronously change flags.
1051+ for ( const update of updates ) {
1052+ for ( const callback of this . flagUpdatedListeners ) callback ( update ) ;
1053+ }
10601054 }
10611055
10621056 private triggerFlagsUpdated ( ) {
0 commit comments