@@ -5,25 +5,24 @@ import { isFiniteNumber, isNaNNumber, toNumber } from '../../utils/lang';
5
5
import { setToArray } from '../../utils/lang/sets' ;
6
6
import { usesSegments } from '../AbstractSplitsCacheSync' ;
7
7
import { KeyBuilderCS } from '../KeyBuilderCS' ;
8
- import { IRBSegmentsCacheSync } from '../types' ;
8
+ import { IRBSegmentsCacheSync , StorageAdapter } from '../types' ;
9
9
import { LOG_PREFIX } from './constants' ;
10
- import SplitIO from '../../../types/splitio' ;
11
10
12
11
export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
13
12
14
13
private readonly keys : KeyBuilderCS ;
15
14
private readonly log : ILogger ;
16
- private readonly localStorage : SplitIO . Storage ;
15
+ private readonly storage : StorageAdapter ;
17
16
18
- constructor ( settings : ISettings , keys : KeyBuilderCS , localStorage : SplitIO . Storage ) {
17
+ constructor ( settings : ISettings , keys : KeyBuilderCS , storage : StorageAdapter ) {
19
18
this . keys = keys ;
20
19
this . log = settings . log ;
21
- this . localStorage = localStorage ;
20
+ this . storage = storage ;
22
21
}
23
22
24
23
clear ( ) {
25
24
this . getNames ( ) . forEach ( name => this . remove ( name ) ) ;
26
- this . localStorage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
25
+ this . storage . removeItem ( this . keys . buildRBSegmentsTillKey ( ) ) ;
27
26
}
28
27
29
28
update ( toAdd : IRBSegment [ ] , toRemove : IRBSegment [ ] , changeNumber : number ) : boolean {
@@ -35,29 +34,28 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
35
34
36
35
private setChangeNumber ( changeNumber : number ) {
37
36
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 ( ) + '' ) ;
40
39
} catch ( e ) {
41
40
this . log . error ( LOG_PREFIX + e ) ;
42
41
}
43
42
}
44
43
45
44
private updateSegmentCount ( diff : number ) {
46
45
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 ) ;
51
49
}
52
50
53
51
private add ( rbSegment : IRBSegment ) : boolean {
54
52
try {
55
53
const name = rbSegment . name ;
56
54
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 ;
59
57
60
- this . localStorage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
58
+ this . storage . setItem ( rbSegmentKey , JSON . stringify ( rbSegment ) ) ;
61
59
62
60
let usesSegmentsDiff = 0 ;
63
61
if ( previous && usesSegments ( previous ) ) usesSegmentsDiff -- ;
@@ -76,7 +74,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
76
74
const rbSegment = this . get ( name ) ;
77
75
if ( ! rbSegment ) return false ;
78
76
79
- this . localStorage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
77
+ this . storage . removeItem ( this . keys . buildRBSegmentKey ( name ) ) ;
80
78
81
79
if ( usesSegments ( rbSegment ) ) this . updateSegmentCount ( - 1 ) ;
82
80
@@ -88,13 +86,13 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
88
86
}
89
87
90
88
private getNames ( ) : string [ ] {
91
- const len = this . localStorage . length ;
89
+ const len = this . storage . length ;
92
90
const accum = [ ] ;
93
91
94
92
let cur = 0 ;
95
93
96
94
while ( cur < len ) {
97
- const key = this . localStorage . key ( cur ) ;
95
+ const key = this . storage . key ( cur ) ;
98
96
99
97
if ( key != null && this . keys . isRBSegmentKey ( key ) ) accum . push ( this . keys . extractKey ( key ) ) ;
100
98
@@ -105,7 +103,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
105
103
}
106
104
107
105
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 ) ) ;
109
107
return item && JSON . parse ( item ) ;
110
108
}
111
109
@@ -117,7 +115,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
117
115
118
116
getChangeNumber ( ) : number {
119
117
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 ( ) ) ;
121
119
122
120
if ( value !== null ) {
123
121
value = parseInt ( value , 10 ) ;
@@ -129,7 +127,7 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
129
127
}
130
128
131
129
usesSegments ( ) : boolean {
132
- const storedCount = this . localStorage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
130
+ const storedCount = this . storage . getItem ( this . keys . buildSplitsWithSegmentCountKey ( ) ) ;
133
131
const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber ( storedCount ) ;
134
132
135
133
return isFiniteNumber ( splitsWithSegmentsCount ) ?
0 commit comments