-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move validateCache logic outside SplitsCacheInLocal
- Loading branch information
1 parent
e13f819
commit b32e3ee
Showing
5 changed files
with
48 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ISettings } from '../../types'; | ||
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser'; | ||
import { isNaNNumber } from '../../utils/lang'; | ||
import { getStorageHash } from '../KeyBuilder'; | ||
import { LOG_PREFIX } from './constants'; | ||
import type { SplitsCacheInLocal } from './SplitsCacheInLocal'; | ||
import { KeyBuilderCS } from '../KeyBuilderCS'; | ||
|
||
/** | ||
* Clean cache if: | ||
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp` | ||
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified | ||
*/ | ||
export function validateCache(settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal): boolean { | ||
const { log } = settings; | ||
|
||
// Check expiration and clear cache if needed | ||
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS; | ||
let value: string | number | null = localStorage.getItem(keys.buildLastUpdatedKey()); | ||
if (value !== null) { | ||
value = parseInt(value, 10); | ||
if (!isNaNNumber(value) && value < expirationTimestamp) splits.clear(); | ||
} | ||
|
||
// Check hash and clear cache if needed | ||
const storageHashKey = keys.buildHashKey(); | ||
const storageHash = localStorage.getItem(storageHashKey); | ||
const currentStorageHash = getStorageHash(settings); | ||
|
||
if (storageHash !== currentStorageHash) { | ||
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Updating cache'); | ||
try { | ||
if (splits.getChangeNumber() > -1) splits.clear(); | ||
|
||
localStorage.setItem(storageHashKey, currentStorageHash); | ||
} catch (e) { | ||
log.error(LOG_PREFIX + e); | ||
} | ||
} | ||
|
||
// Check if the cache is ready | ||
return splits.getChangeNumber() > -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters