@@ -7,6 +7,7 @@ import type { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
7
7
import type { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal' ;
8
8
import { KeyBuilderCS } from '../KeyBuilderCS' ;
9
9
import SplitIO from '../../../types/splitio' ;
10
+ import { StorageAdapter } from '../types' ;
10
11
11
12
const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10 ;
12
13
const MILLIS_IN_A_DAY = 86400000 ;
@@ -16,11 +17,11 @@ const MILLIS_IN_A_DAY = 86400000;
16
17
*
17
18
* @returns `true` if cache should be cleared, `false` otherwise
18
19
*/
19
- function validateExpiration ( options : SplitIO . InLocalStorageOptions , settings : ISettings , keys : KeyBuilderCS , currentTimestamp : number , isThereCache : boolean ) {
20
+ function validateExpiration ( options : SplitIO . InLocalStorageOptions , storage : StorageAdapter , settings : ISettings , keys : KeyBuilderCS , currentTimestamp : number , isThereCache : boolean ) {
20
21
const { log } = settings ;
21
22
22
23
// Check expiration
23
- const lastUpdatedTimestamp = parseInt ( localStorage . getItem ( keys . buildLastUpdatedKey ( ) ) as string , 10 ) ;
24
+ const lastUpdatedTimestamp = parseInt ( storage . getItem ( keys . buildLastUpdatedKey ( ) ) as string , 10 ) ;
24
25
if ( ! isNaNNumber ( lastUpdatedTimestamp ) ) {
25
26
const cacheExpirationInDays = isFiniteNumber ( options . expirationDays ) && options . expirationDays >= 1 ? options . expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS ;
26
27
const expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays ;
@@ -32,12 +33,12 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
32
33
33
34
// Check hash
34
35
const storageHashKey = keys . buildHashKey ( ) ;
35
- const storageHash = localStorage . getItem ( storageHashKey ) ;
36
+ const storageHash = storage . getItem ( storageHashKey ) ;
36
37
const currentStorageHash = getStorageHash ( settings ) ;
37
38
38
39
if ( storageHash !== currentStorageHash ) {
39
40
try {
40
- localStorage . setItem ( storageHashKey , currentStorageHash ) ;
41
+ storage . setItem ( storageHashKey , currentStorageHash ) ;
41
42
} catch ( e ) {
42
43
log . error ( LOG_PREFIX + e ) ;
43
44
}
@@ -50,7 +51,7 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
50
51
51
52
// Clear on init
52
53
if ( options . clearOnInit ) {
53
- const lastClearTimestamp = parseInt ( localStorage . getItem ( keys . buildLastClear ( ) ) as string , 10 ) ;
54
+ const lastClearTimestamp = parseInt ( storage . getItem ( keys . buildLastClear ( ) ) as string , 10 ) ;
54
55
55
56
if ( isNaNNumber ( lastClearTimestamp ) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY ) {
56
57
log . info ( LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache' ) ;
@@ -67,28 +68,29 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
67
68
*
68
69
* @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
69
70
*/
70
- export function validateCache ( options : SplitIO . InLocalStorageOptions , settings : ISettings , keys : KeyBuilderCS , splits : SplitsCacheInLocal , rbSegments : RBSegmentsCacheInLocal , segments : MySegmentsCacheInLocal , largeSegments : MySegmentsCacheInLocal ) : Promise < boolean > {
71
- return new Promise < boolean > ( ( resolve ) => {
71
+ export function validateCache ( options : SplitIO . InLocalStorageOptions , storage : StorageAdapter , settings : ISettings , keys : KeyBuilderCS , splits : SplitsCacheInLocal , rbSegments : RBSegmentsCacheInLocal , segments : MySegmentsCacheInLocal , largeSegments : MySegmentsCacheInLocal ) : Promise < boolean > {
72
+
73
+ return Promise . resolve ( ) . then ( ( ) => {
72
74
const currentTimestamp = Date . now ( ) ;
73
75
const isThereCache = splits . getChangeNumber ( ) > - 1 ;
74
76
75
- if ( validateExpiration ( options , settings , keys , currentTimestamp , isThereCache ) ) {
77
+ if ( validateExpiration ( options , storage , settings , keys , currentTimestamp , isThereCache ) ) {
76
78
splits . clear ( ) ;
77
79
rbSegments . clear ( ) ;
78
80
segments . clear ( ) ;
79
81
largeSegments . clear ( ) ;
80
82
81
83
// Update last clear timestamp
82
84
try {
83
- localStorage . setItem ( keys . buildLastClear ( ) , currentTimestamp + '' ) ;
85
+ storage . setItem ( keys . buildLastClear ( ) , currentTimestamp + '' ) ;
84
86
} catch ( e ) {
85
87
settings . log . error ( LOG_PREFIX + e ) ;
86
88
}
87
89
88
- resolve ( false ) ;
90
+ return false ;
89
91
}
90
92
91
93
// Check if ready from cache
92
- resolve ( isThereCache ) ;
94
+ return isThereCache ;
93
95
} ) ;
94
96
}
0 commit comments