@@ -7,20 +7,23 @@ import { usesSegments } from '../AbstractSplitsCacheSync';
77import { KeyBuilderCS } from '../KeyBuilderCS' ;
88import { IRBSegmentsCacheSync } from '../types' ;
99import { LOG_PREFIX } from './constants' ;
10+ import SplitIO from '../../../types/splitio' ;
1011
1112export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
1213
1314 private readonly keys : KeyBuilderCS ;
1415 private readonly log : ILogger ;
16+ private readonly localStorage : SplitIO . Storage ;
1517
16- constructor ( settings : ISettings , keys : KeyBuilderCS ) {
18+ constructor ( settings : ISettings , keys : KeyBuilderCS , localStorage : SplitIO . Storage ) {
1719 this . keys = keys ;
1820 this . log = settings . log ;
21+ this . localStorage = localStorage ;
1922 }
2023
2124 clear ( ) {
2225 this . getNames ( ) . forEach ( name => this . remove ( name ) ) ;
23- localStorage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
26+ this . localStorage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
2427 }
2528
2629 update ( toAdd : IRBSegment [ ] , toRemove : IRBSegment [ ] , changeNumber : number ) : boolean {
@@ -31,29 +34,29 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
3134
3235 private setChangeNumber ( changeNumber : number ) {
3336 try {
34- localStorage . setItem ( this . keys . buildRBSegmentsTillKey ( ) , changeNumber + '' ) ;
35- localStorage . setItem ( this . keys . buildLastUpdatedKey ( ) , Date . now ( ) + '' ) ;
37+ this . localStorage . setItem ( this . keys . buildRBSegmentsTillKey ( ) , changeNumber + '' ) ;
38+ this . localStorage . setItem ( this . keys . buildLastUpdatedKey ( ) , Date . now ( ) + '' ) ;
3639 } catch ( e ) {
3740 this . log . error ( LOG_PREFIX + e ) ;
3841 }
3942 }
4043
4144 private updateSegmentCount ( diff : number ) {
4245 const segmentsCountKey = this . keys . buildSplitsWithSegmentCountKey ( ) ;
43- const count = toNumber ( localStorage . getItem ( segmentsCountKey ) ) + diff ;
46+ const count = toNumber ( this . localStorage . getItem ( segmentsCountKey ) ) + diff ;
4447 // @ts -expect-error
45- if ( count > 0 ) localStorage . setItem ( segmentsCountKey , count ) ;
46- else localStorage . removeItem ( segmentsCountKey ) ;
48+ if ( count > 0 ) this . localStorage . setItem ( segmentsCountKey , count ) ;
49+ else this . localStorage . removeItem ( segmentsCountKey ) ;
4750 }
4851
4952 private add ( rbSegment : IRBSegment ) : boolean {
5053 try {
5154 const name = rbSegment . name ;
5255 const rbSegmentKey = this . keys . buildRBSegmentKey ( name ) ;
53- const rbSegmentFromLocalStorage = localStorage . getItem ( rbSegmentKey ) ;
56+ const rbSegmentFromLocalStorage = this . localStorage . getItem ( rbSegmentKey ) ;
5457 const previous = rbSegmentFromLocalStorage ? JSON . parse ( rbSegmentFromLocalStorage ) : null ;
5558
56- localStorage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
59+ this . localStorage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
5760
5861 let usesSegmentsDiff = 0 ;
5962 if ( previous && usesSegments ( previous ) ) usesSegmentsDiff -- ;
@@ -72,7 +75,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
7275 const rbSegment = this . get ( name ) ;
7376 if ( ! rbSegment ) return false ;
7477
75- localStorage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
78+ this . localStorage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
7679
7780 if ( usesSegments ( rbSegment ) ) this . updateSegmentCount ( - 1 ) ;
7881
@@ -84,13 +87,13 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
8487 }
8588
8689 private getNames ( ) : string [ ] {
87- const len = localStorage . length ;
90+ const len = this . localStorage . length ;
8891 const accum = [ ] ;
8992
9093 let cur = 0 ;
9194
9295 while ( cur < len ) {
93- const key = localStorage . key ( cur ) ;
96+ const key = this . localStorage . key ( cur ) ;
9497
9598 if ( key != null && this . keys . isRBSegmentKey ( key ) ) accum . push ( this . keys . extractKey ( key ) ) ;
9699
@@ -101,7 +104,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
101104 }
102105
103106 get ( name : string ) : IRBSegment | null {
104- const item = localStorage . getItem ( this . keys . buildRBSegmentKey ( name ) ) ;
107+ const item = this . localStorage . getItem ( this . keys . buildRBSegmentKey ( name ) ) ;
105108 return item && JSON . parse ( item ) ;
106109 }
107110
@@ -113,7 +116,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
113116
114117 getChangeNumber ( ) : number {
115118 const n = - 1 ;
116- let value : string | number | null = localStorage . getItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
119+ let value : string | number | null = this . localStorage . getItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
117120
118121 if ( value !== null ) {
119122 value = parseInt ( value , 10 ) ;
@@ -125,7 +128,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
125128 }
126129
127130 usesSegments ( ) : boolean {
128- const storedCount = localStorage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
131+ const storedCount = this . localStorage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
129132 const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber ( storedCount ) ;
130133
131134 return isFiniteNumber ( splitsWithSegmentsCount ) ?
0 commit comments