@@ -5,25 +5,24 @@ import { isFiniteNumber, isNaNNumber, toNumber } from '../../utils/lang';
55import { setToArray } from '../../utils/lang/sets' ;
66import { usesSegments } from '../AbstractSplitsCacheSync' ;
77import { KeyBuilderCS } from '../KeyBuilderCS' ;
8- import { IRBSegmentsCacheSync } from '../types' ;
8+ import { IRBSegmentsCacheSync , StorageAdapter } from '../types' ;
99import { LOG_PREFIX } from './constants' ;
10- import SplitIO from '../../../types/splitio' ;
1110
1211export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
1312
1413 private readonly keys : KeyBuilderCS ;
1514 private readonly log : ILogger ;
16- private readonly localStorage : SplitIO . Storage ;
15+ private readonly storage : StorageAdapter ;
1716
18- constructor ( settings : ISettings , keys : KeyBuilderCS , localStorage : SplitIO . Storage ) {
17+ constructor ( settings : ISettings , keys : KeyBuilderCS , storage : StorageAdapter ) {
1918 this . keys = keys ;
2019 this . log = settings . log ;
21- this . localStorage = localStorage ;
20+ this . storage = storage ;
2221 }
2322
2423 clear ( ) {
2524 this . getNames ( ) . forEach ( name => this . remove ( name ) ) ;
26- this . localStorage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
25+ this . storage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
2726 }
2827
2928 update ( toAdd : IRBSegment [ ] , toRemove : IRBSegment [ ] , changeNumber : number ) : boolean {
@@ -35,29 +34,28 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
3534
3635 private setChangeNumber ( changeNumber : number ) {
3736 try {
38- this . localStorage . setItem ( this . keys . buildRBSegmentsTillKey ( ) , changeNumber + '' ) ;
39- this . localStorage . setItem ( this . keys . buildLastUpdatedKey ( ) , Date . now ( ) + '' ) ;
37+ this . storage . setItem ( this . keys . buildRBSegmentsTillKey ( ) , changeNumber + '' ) ;
38+ this . storage . setItem ( this . keys . buildLastUpdatedKey ( ) , Date . now ( ) + '' ) ;
4039 } catch ( e ) {
4140 this . log . error ( LOG_PREFIX + e ) ;
4241 }
4342 }
4443
4544 private updateSegmentCount ( diff : number ) {
4645 const segmentsCountKey = this . keys . buildSplitsWithSegmentCountKey ( ) ;
47- const count = toNumber ( this . localStorage . getItem ( segmentsCountKey ) ) + diff ;
48- // @ts -expect-error
49- if ( count > 0 ) this . localStorage . setItem ( segmentsCountKey , count ) ;
50- else this . localStorage . removeItem ( segmentsCountKey ) ;
46+ const count = toNumber ( this . storage . getItem ( segmentsCountKey ) ) + diff ;
47+ if ( count > 0 ) this . storage . setItem ( segmentsCountKey , count + '' ) ;
48+ else this . storage . removeItem ( segmentsCountKey ) ;
5149 }
5250
5351 private add ( rbSegment : IRBSegment ) : boolean {
5452 try {
5553 const name = rbSegment . name ;
5654 const rbSegmentKey = this . keys . buildRBSegmentKey ( name ) ;
57- const rbSegmentFromLocalStorage = this . localStorage . getItem ( rbSegmentKey ) ;
58- const previous = rbSegmentFromLocalStorage ? JSON . parse ( rbSegmentFromLocalStorage ) : null ;
55+ const rbSegmentFromStorage = this . storage . getItem ( rbSegmentKey ) ;
56+ const previous = rbSegmentFromStorage ? JSON . parse ( rbSegmentFromStorage ) : null ;
5957
60- this . localStorage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
58+ this . storage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
6159
6260 let usesSegmentsDiff = 0 ;
6361 if ( previous && usesSegments ( previous ) ) usesSegmentsDiff -- ;
@@ -76,7 +74,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
7674 const rbSegment = this . get ( name ) ;
7775 if ( ! rbSegment ) return false ;
7876
79- this . localStorage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
77+ this . storage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
8078
8179 if ( usesSegments ( rbSegment ) ) this . updateSegmentCount ( - 1 ) ;
8280
@@ -88,13 +86,13 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
8886 }
8987
9088 private getNames ( ) : string [ ] {
91- const len = this . localStorage . length ;
89+ const len = this . storage . length ;
9290 const accum = [ ] ;
9391
9492 let cur = 0 ;
9593
9694 while ( cur < len ) {
97- const key = this . localStorage . key ( cur ) ;
95+ const key = this . storage . key ( cur ) ;
9896
9997 if ( key != null && this . keys . isRBSegmentKey ( key ) ) accum . push ( this . keys . extractKey ( key ) ) ;
10098
@@ -105,7 +103,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
105103 }
106104
107105 get ( name : string ) : IRBSegment | null {
108- const item = this . localStorage . getItem ( this . keys . buildRBSegmentKey ( name ) ) ;
106+ const item = this . storage . getItem ( this . keys . buildRBSegmentKey ( name ) ) ;
109107 return item && JSON . parse ( item ) ;
110108 }
111109
@@ -117,7 +115,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
117115
118116 getChangeNumber ( ) : number {
119117 const n = - 1 ;
120- let value : string | number | null = this . localStorage . getItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
118+ let value : string | number | null = this . storage . getItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
121119
122120 if ( value !== null ) {
123121 value = parseInt ( value , 10 ) ;
@@ -129,7 +127,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
129127 }
130128
131129 usesSegments ( ) : boolean {
132- const storedCount = this . localStorage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
130+ const storedCount = this . storage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
133131 const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber ( storedCount ) ;
134132
135133 return isFiniteNumber ( splitsWithSegmentsCount ) ?
0 commit comments