@@ -5,22 +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' ;
1010
1111export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
1212
1313 private readonly keys : KeyBuilderCS ;
1414 private readonly log : ILogger ;
15+ private readonly storage : StorageAdapter ;
1516
16- constructor ( settings : ISettings , keys : KeyBuilderCS ) {
17+ constructor ( settings : ISettings , keys : KeyBuilderCS , storage : StorageAdapter ) {
1718 this . keys = keys ;
1819 this . log = settings . log ;
20+ this . storage = storage ;
1921 }
2022
2123 clear ( ) {
2224 this . getNames ( ) . forEach ( name => this . remove ( name ) ) ;
23- localStorage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
25+ this . storage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
2426 }
2527
2628 update ( toAdd : IRBSegment [ ] , toRemove : IRBSegment [ ] , changeNumber : number ) : boolean {
@@ -31,29 +33,28 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
3133
3234 private setChangeNumber ( changeNumber : number ) {
3335 try {
34- localStorage . setItem ( this . keys . buildRBSegmentsTillKey ( ) , changeNumber + '' ) ;
35- localStorage . setItem ( this . keys . buildLastUpdatedKey ( ) , Date . now ( ) + '' ) ;
36+ this . storage . setItem ( this . keys . buildRBSegmentsTillKey ( ) , changeNumber + '' ) ;
37+ this . storage . setItem ( this . keys . buildLastUpdatedKey ( ) , Date . now ( ) + '' ) ;
3638 } catch ( e ) {
3739 this . log . error ( LOG_PREFIX + e ) ;
3840 }
3941 }
4042
4143 private updateSegmentCount ( diff : number ) {
4244 const segmentsCountKey = this . keys . buildSplitsWithSegmentCountKey ( ) ;
43- const count = toNumber ( localStorage . getItem ( segmentsCountKey ) ) + diff ;
44- // @ts -expect-error
45- if ( count > 0 ) localStorage . setItem ( segmentsCountKey , count ) ;
46- else localStorage . removeItem ( segmentsCountKey ) ;
45+ const count = toNumber ( this . storage . getItem ( segmentsCountKey ) ) + diff ;
46+ if ( count > 0 ) this . storage . setItem ( segmentsCountKey , count + '' ) ;
47+ else this . storage . removeItem ( segmentsCountKey ) ;
4748 }
4849
4950 private add ( rbSegment : IRBSegment ) : boolean {
5051 try {
5152 const name = rbSegment . name ;
5253 const rbSegmentKey = this . keys . buildRBSegmentKey ( name ) ;
53- const rbSegmentFromLocalStorage = localStorage . getItem ( rbSegmentKey ) ;
54- const previous = rbSegmentFromLocalStorage ? JSON . parse ( rbSegmentFromLocalStorage ) : null ;
54+ const rbSegmentFromStorage = this . storage . getItem ( rbSegmentKey ) ;
55+ const previous = rbSegmentFromStorage ? JSON . parse ( rbSegmentFromStorage ) : null ;
5556
56- localStorage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
57+ this . storage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
5758
5859 let usesSegmentsDiff = 0 ;
5960 if ( previous && usesSegments ( previous ) ) usesSegmentsDiff -- ;
@@ -72,7 +73,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
7273 const rbSegment = this . get ( name ) ;
7374 if ( ! rbSegment ) return false ;
7475
75- localStorage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
76+ this . storage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
7677
7778 if ( usesSegments ( rbSegment ) ) this . updateSegmentCount ( - 1 ) ;
7879
@@ -84,13 +85,13 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
8485 }
8586
8687 private getNames ( ) : string [ ] {
87- const len = localStorage . length ;
88+ const len = this . storage . length ;
8889 const accum = [ ] ;
8990
9091 let cur = 0 ;
9192
9293 while ( cur < len ) {
93- const key = localStorage . key ( cur ) ;
94+ const key = this . storage . key ( cur ) ;
9495
9596 if ( key != null && this . keys . isRBSegmentKey ( key ) ) accum . push ( this . keys . extractKey ( key ) ) ;
9697
@@ -101,7 +102,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
101102 }
102103
103104 get ( name : string ) : IRBSegment | null {
104- const item = localStorage . getItem ( this . keys . buildRBSegmentKey ( name ) ) ;
105+ const item = this . storage . getItem ( this . keys . buildRBSegmentKey ( name ) ) ;
105106 return item && JSON . parse ( item ) ;
106107 }
107108
@@ -113,7 +114,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
113114
114115 getChangeNumber ( ) : number {
115116 const n = - 1 ;
116- let value : string | number | null = localStorage . getItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
117+ let value : string | number | null = this . storage . getItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
117118
118119 if ( value !== null ) {
119120 value = parseInt ( value , 10 ) ;
@@ -125,7 +126,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
125126 }
126127
127128 usesSegments ( ) : boolean {
128- const storedCount = localStorage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
129+ const storedCount = this . storage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
129130 const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber ( storedCount ) ;
130131
131132 return isFiniteNumber ( splitsWithSegmentsCount ) ?
0 commit comments