Skip to content

Commit 310ab05

Browse files
Polishing
1 parent 6b5eaca commit 310ab05

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/storages/inLocalStorage/validateCache.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
77
import type { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
88
import { KeyBuilderCS } from '../KeyBuilderCS';
99
import SplitIO from '../../../types/splitio';
10+
import { StorageAdapter } from '../types';
1011

1112
const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
1213
const MILLIS_IN_A_DAY = 86400000;
@@ -16,11 +17,11 @@ const MILLIS_IN_A_DAY = 86400000;
1617
*
1718
* @returns `true` if cache should be cleared, `false` otherwise
1819
*/
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) {
2021
const { log } = settings;
2122

2223
// Check expiration
23-
const lastUpdatedTimestamp = parseInt(localStorage.getItem(keys.buildLastUpdatedKey()) as string, 10);
24+
const lastUpdatedTimestamp = parseInt(storage.getItem(keys.buildLastUpdatedKey()) as string, 10);
2425
if (!isNaNNumber(lastUpdatedTimestamp)) {
2526
const cacheExpirationInDays = isFiniteNumber(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS;
2627
const expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays;
@@ -32,12 +33,12 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
3233

3334
// Check hash
3435
const storageHashKey = keys.buildHashKey();
35-
const storageHash = localStorage.getItem(storageHashKey);
36+
const storageHash = storage.getItem(storageHashKey);
3637
const currentStorageHash = getStorageHash(settings);
3738

3839
if (storageHash !== currentStorageHash) {
3940
try {
40-
localStorage.setItem(storageHashKey, currentStorageHash);
41+
storage.setItem(storageHashKey, currentStorageHash);
4142
} catch (e) {
4243
log.error(LOG_PREFIX + e);
4344
}
@@ -50,7 +51,7 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
5051

5152
// Clear on init
5253
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);
5455

5556
if (isNaNNumber(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) {
5657
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
6768
*
6869
* @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
6970
*/
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(() => {
7274
const currentTimestamp = Date.now();
7375
const isThereCache = splits.getChangeNumber() > -1;
7476

75-
if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
77+
if (validateExpiration(options, storage, settings, keys, currentTimestamp, isThereCache)) {
7678
splits.clear();
7779
rbSegments.clear();
7880
segments.clear();
7981
largeSegments.clear();
8082

8183
// Update last clear timestamp
8284
try {
83-
localStorage.setItem(keys.buildLastClear(), currentTimestamp + '');
85+
storage.setItem(keys.buildLastClear(), currentTimestamp + '');
8486
} catch (e) {
8587
settings.log.error(LOG_PREFIX + e);
8688
}
8789

88-
resolve(false);
90+
return false;
8991
}
9092

9193
// Check if ready from cache
92-
resolve(isThereCache);
94+
return isThereCache;
9395
});
9496
}

0 commit comments

Comments
 (0)