@@ -12,7 +12,8 @@ import {
12
12
} from './types' ;
13
13
// @ts -ignore
14
14
import deepEqual from 'fast-deep-equal' ;
15
- import { AsyncStorageType } from './async-storage' ;
15
+ import { AsyncStorageType } from './utils/async-storage' ;
16
+ import getChanges from './utils/get-changes' ;
16
17
17
18
enum FlagSource {
18
19
"NONE" = "NONE" ,
@@ -177,8 +178,8 @@ const Flagsmith = class {
177
178
this . oldFlags = {
178
179
...this . flags
179
180
} ;
180
- const flagsEqual = deepEqual ( this . flags , flags ) ;
181
- const traitsEqual = deepEqual ( this . traits , userTraits ) ;
181
+ const flagsChanged = getChanges ( this . oldFlags , flags ) ;
182
+ const traitsChanged = getChanges ( this . withTraits , userTraits ) ;
182
183
this . flags = flags ;
183
184
this . traits = userTraits ;
184
185
this . updateStorage ( ) ;
@@ -232,8 +233,8 @@ const Flagsmith = class {
232
233
}
233
234
this . _onChange ! ( this . oldFlags , {
234
235
isFromServer : true ,
235
- flagsChanged : ! flagsEqual ,
236
- traitsChanged : ! traitsEqual
236
+ flagsChanged,
237
+ traitsChanged
237
238
} , this . _loadedState ( null , FlagSource . SERVER ) ) ;
238
239
} ;
239
240
@@ -574,6 +575,8 @@ const Flagsmith = class {
574
575
if ( AsyncStorage && this . canUseStorage ) {
575
576
const onRetrievedStorage = ( err : Error | null , res : string | null ) => {
576
577
if ( res ) {
578
+ let flagsChanged = null
579
+ let traitsChanged = null
577
580
try {
578
581
const json = JSON . parse ( res ) ;
579
582
let cachePopulated = false ;
@@ -593,6 +596,8 @@ const Flagsmith = class {
593
596
}
594
597
if ( setState ) {
595
598
cachePopulated = true ;
599
+ traitsChanged = getChanges ( this . traits , json . traits )
600
+ flagsChanged = getChanges ( this . flags , json . flags )
596
601
this . setState ( json ) ;
597
602
this . log ( "Retrieved flags from cache" , json ) ;
598
603
}
@@ -601,7 +606,7 @@ const Flagsmith = class {
601
606
if ( cachePopulated ) { // retrieved flags from local storage
602
607
const shouldFetchFlags = ! preventFetch && ( ! this . cacheOptions . skipAPI || ! cachePopulated )
603
608
this . _onChange ! ( null ,
604
- { isFromServer : false , flagsChanged : true , traitsChanged : ! ! this . traits && ! ! Object . keys ( this . traits ) . length } ,
609
+ { isFromServer : false , flagsChanged, traitsChanged } ,
605
610
this . _loadedState ( null , FlagSource . CACHE , shouldFetchFlags )
606
611
) ;
607
612
this . oldFlags = this . flags ;
@@ -628,12 +633,12 @@ const Flagsmith = class {
628
633
} else {
629
634
if ( defaultFlags ) {
630
635
this . _onChange ! ( null ,
631
- { isFromServer : false , flagsChanged : true , traitsChanged : ! ! this . traits && ! ! Object . keys ( this . traits ) . length } ,
636
+ { isFromServer : false , flagsChanged : getChanges ( { } , this . flags ) , traitsChanged : getChanges ( { } , this . traits ) } ,
632
637
this . _loadedState ( null , FlagSource . DEFAULT_FLAGS )
633
638
) ;
634
639
} else if ( this . flags ) { // flags exist due to set state being called e.g. from nextJS serverState
635
640
this . _onChange ?.( null ,
636
- { isFromServer : false , flagsChanged : true , traitsChanged : ! ! this . traits && ! ! Object . keys ( this . traits ) . length } ,
641
+ { isFromServer : false , flagsChanged : getChanges ( { } , this . flags ) , traitsChanged : getChanges ( { } , this . traits ) } ,
637
642
this . _loadedState ( null , FlagSource . DEFAULT_FLAGS )
638
643
) ;
639
644
} else {
@@ -656,13 +661,13 @@ const Flagsmith = class {
656
661
this . getFlags ( resolve , reject ) ;
657
662
} else {
658
663
if ( defaultFlags ) {
659
- this . _onChange ?.( null , { isFromServer : false , flagsChanged : true , traitsChanged :! ! this . traits && ! ! Object . keys ( this . traits ) . length } , this . _loadedState ( null , FlagSource . DEFAULT_FLAGS ) ) ;
660
- } else if ( this . flags ) {
664
+ this . _onChange ?.( null , { isFromServer : false , flagsChanged : getChanges ( { } , defaultFlags ) , traitsChanged :getChanges ( { } , traits ) } , this . _loadedState ( null , FlagSource . DEFAULT_FLAGS ) ) ;
665
+ } else if ( this . flags ) {
661
666
let error = null
662
667
if ( Object . keys ( this . flags ) . length === 0 ) {
663
668
error = WRONG_FLAGSMITH_CONFIG
664
669
}
665
- this . _onChange ?.( null , { isFromServer : false , flagsChanged : true , traitsChanged : ! ! this . traits && ! ! Object . keys ( this . traits ) . length } , this . _loadedState ( error , FlagSource . DEFAULT_FLAGS ) ) ;
670
+ this . _onChange ?.( null , { isFromServer : false , flagsChanged : getChanges ( { } , this . flags ) , traitsChanged : getChanges ( { } , traits ) } , this . _loadedState ( error , FlagSource . DEFAULT_FLAGS ) ) ;
666
671
667
672
}
668
673
resolve ( true ) ;
@@ -674,7 +679,7 @@ const Flagsmith = class {
674
679
} ) ;
675
680
}
676
681
677
- _loadedState ( error = null , source :FlagSource , isFetching = false ) {
682
+ _loadedState ( error : any = null , source :FlagSource , isFetching = false ) {
678
683
return {
679
684
error,
680
685
isFetching,
0 commit comments