@@ -676,24 +676,20 @@ export class ReflagClient {
676676 * @param user
677677 */
678678 async updateUser ( user : { [ key : string ] : string | number | undefined } ) {
679- const userIdChanged = user . id && user . id !== this . context . user ?. id ;
680679 const newUserContext = {
681680 ...this . context . user ,
682681 ...user ,
683682 id : user . id ?? this . context . user ?. id ,
684683 } ;
685684
686- // Nothing has changed, skipping update
687- if ( deepEqual ( this . context . user , newUserContext ) ) return ;
688- this . context . user = newUserContext ;
689- void this . user ( ) ;
690-
691- // Update the feedback user if the user ID has changed
692- if ( userIdChanged ) {
693- void this . updateAutoFeedbackUser ( String ( user . id ) ) ;
694- }
695-
696- await this . flagsClient . setContext ( this . context ) ;
685+ return this . applyContext (
686+ {
687+ user : newUserContext ,
688+ company : this . context . company ,
689+ other : this . context . other ,
690+ } ,
691+ { warnOnMissingIds : false } ,
692+ ) ;
697693 }
698694
699695 /**
@@ -710,12 +706,14 @@ export class ReflagClient {
710706 id : company . id ?? this . context . company ?. id ,
711707 } ;
712708
713- // Nothing has changed, skipping update
714- if ( deepEqual ( this . context . company , newCompanyContext ) ) return ;
715- this . context . company = newCompanyContext ;
716- void this . company ( ) ;
717-
718- await this . flagsClient . setContext ( this . context ) ;
709+ return this . applyContext (
710+ {
711+ user : this . context . user ,
712+ company : newCompanyContext ,
713+ other : this . context . other ,
714+ } ,
715+ { warnOnMissingIds : false } ,
716+ ) ;
719717 }
720718
721719 /**
@@ -733,11 +731,14 @@ export class ReflagClient {
733731 ...otherContext ,
734732 } ;
735733
736- // Nothing has changed, skipping update
737- if ( deepEqual ( this . context . other , newOtherContext ) ) return ;
738- this . context . other = newOtherContext ;
739-
740- await this . flagsClient . setContext ( this . context ) ;
734+ return this . applyContext (
735+ {
736+ user : this . context . user ,
737+ company : this . context . company ,
738+ other : newOtherContext ,
739+ } ,
740+ { warnOnMissingIds : false } ,
741+ ) ;
741742 }
742743
743744 /**
@@ -746,9 +747,15 @@ export class ReflagClient {
746747 *
747748 * @param context The context to update.
748749 */
749- async setContext ( { otherContext, ...context } : ReflagDeprecatedContext ) {
750- const userIdChanged =
751- context . user ?. id && context . user . id !== this . context . user ?. id ;
750+ async setContext ( context : ReflagDeprecatedContext ) {
751+ return this . applyContext ( context , { warnOnMissingIds : true } ) ;
752+ }
753+
754+ private async applyContext (
755+ { otherContext, ...context } : ReflagDeprecatedContext ,
756+ { warnOnMissingIds } : { warnOnMissingIds : boolean } ,
757+ ) {
758+ const previousContext = this . context ;
752759
753760 // Create a new context object making sure to clone the user and company objects
754761 const newContext = {
@@ -757,32 +764,49 @@ export class ReflagClient {
757764 other : { ...otherContext , ...context . other } ,
758765 } ;
759766
760- if ( ! context . user ?. id ) {
767+ if ( warnOnMissingIds && ! context . user ?. id ) {
761768 this . logger . warn ( "No user Id provided in context, user will be ignored" ) ;
762769 }
763- if ( ! context . company ?. id ) {
770+ if ( warnOnMissingIds && ! context . company ?. id ) {
764771 this . logger . warn (
765772 "No company Id provided in context, company will be ignored" ,
766773 ) ;
767774 }
768775
769776 // Nothing has changed, skipping update
770- if ( deepEqual ( this . context , newContext ) ) return ;
777+ if ( deepEqual ( previousContext , newContext ) ) return ;
778+
779+ const userChanged = ! deepEqual ( previousContext . user , newContext . user ) ;
780+ const companyChanged = ! deepEqual (
781+ previousContext . company ,
782+ newContext . company ,
783+ ) ;
784+ const userIdChanged =
785+ ! ! newContext . user ?. id && newContext . user . id !== previousContext . user ?. id ;
786+
771787 this . context = newContext ;
772788
773- if ( context . company ) {
789+ if ( companyChanged ) {
774790 void this . company ( ) ;
775791 }
776792
777- if ( context . user ) {
793+ if ( userChanged ) {
778794 void this . user ( ) ;
779795 // Update the automatic feedback user if the user ID has changed
780796 if ( userIdChanged ) {
781- void this . updateAutoFeedbackUser ( String ( context . user . id ) ) ;
797+ void this . updateAutoFeedbackUser ( String ( newContext . user ! . id ) ) ;
782798 }
783799 }
784800
785- await this . flagsClient . setContext ( this . context ) ;
801+ const shouldTrackLoading = this . state === "initialized" ;
802+ if ( shouldTrackLoading ) {
803+ this . setState ( "initializing" ) ;
804+ }
805+
806+ const didApply = await this . flagsClient . setContext ( this . context ) ;
807+ if ( didApply && this . state === "initializing" ) {
808+ this . setState ( "initialized" ) ;
809+ }
786810 }
787811
788812 /**
0 commit comments