@@ -31,16 +31,14 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
3131 else localStorage . removeItem ( key ) ;
3232 }
3333
34- private _decrementCounts ( split : ISplit | null ) {
34+ private _decrementCounts ( split : ISplit ) {
3535 try {
36- if ( split ) {
37- const ttKey = this . keys . buildTrafficTypeKey ( split . trafficTypeName ) ;
38- this . _decrementCount ( ttKey ) ;
36+ const ttKey = this . keys . buildTrafficTypeKey ( split . trafficTypeName ) ;
37+ this . _decrementCount ( ttKey ) ;
3938
40- if ( usesSegments ( split ) ) {
41- const segmentsCountKey = this . keys . buildSplitsWithSegmentCountKey ( ) ;
42- this . _decrementCount ( segmentsCountKey ) ;
43- }
39+ if ( usesSegments ( split ) ) {
40+ const segmentsCountKey = this . keys . buildSplitsWithSegmentCountKey ( ) ;
41+ this . _decrementCount ( segmentsCountKey ) ;
4442 }
4543 } catch ( e ) {
4644 this . log . error ( LOG_PREFIX + e ) ;
@@ -93,12 +91,14 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
9391 const splitFromLocalStorage = localStorage . getItem ( splitKey ) ;
9492 const previousSplit = splitFromLocalStorage ? JSON . parse ( splitFromLocalStorage ) : null ;
9593
94+ if ( previousSplit ) {
95+ this . _decrementCounts ( previousSplit ) ;
96+ this . removeFromFlagSets ( previousSplit . name , previousSplit . sets ) ;
97+ }
98+
9699 localStorage . setItem ( splitKey , JSON . stringify ( split ) ) ;
97100
98101 this . _incrementCounts ( split ) ;
99- this . _decrementCounts ( previousSplit ) ;
100-
101- if ( previousSplit ) this . removeFromFlagSets ( previousSplit . name , previousSplit . sets ) ;
102102 this . addToFlagSets ( split ) ;
103103
104104 return true ;
@@ -116,7 +116,7 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
116116 localStorage . removeItem ( this . keys . buildSplitKey ( name ) ) ;
117117
118118 this . _decrementCounts ( split ) ;
119- if ( split ) this . removeFromFlagSets ( split . name , split . sets ) ;
119+ this . removeFromFlagSets ( split . name , split . sets ) ;
120120
121121 return true ;
122122 } catch ( e ) {
0 commit comments