All files checkCacheValid.ts

100% Statements 14/14
100% Branches 9/9
100% Functions 1/1
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  1x 1x                 1x 5x 5x 5x 5x 2x   3x 3x 2x   1x   1x              
import { DEFAULT_KEY } from "./constants";
 
export interface State {
	DEFAULT_KEY?: number | null | undefined,
	[x: string]: any
}
 
/**
 * Checks if the cache TTL is still valid.
 * 
 * @param {function} getState 
 * @param {string} reducerKey
 * @param {string} [cacheKey=DEFAULT_KEY]
 * @returns {boolean}
 */
export const checkCacheValid = (getState: () => State, reducerKey: string, cacheKey: string = DEFAULT_KEY): boolean => {
	const state = getState();
	const cacheUntil: number = state && state[reducerKey] && state[reducerKey][cacheKey];
	if (!cacheUntil) {
		return false
	}
 
	const currentTime: number = Date.now();
	if (cacheUntil > currentTime) {
		return true
	}
 
	return false;
 
};
 
export default checkCacheValid;