import * as CV from './CacheValue' ; import * as Eq from 'fp-ts/lib/Eq' ; import { constTrue , constFalse } from 'fp-ts/lib/function' ; declare module 'fp-ts/lib/HKT' { interface URItoKind3 < R , E , A > { Strategy : Strategy < R , E , A > ; } } export const URI = 'Strategy' ; export type URI = typeof URI ; export interface Strategy < R , E , A > { readonly inputEq : Eq . Eq < R > ; readonly filter : ( cacheValue : CV . CacheValue < E , A > ) => boolean ; readonly cacheValueEq : Eq . Eq < CV . CacheValue < E , A > > ; } export function fromSuccessFilter < R , E , A > ( inputEq , filter , cacheValueEq ) { return { inputEq , filter : CV . fold ( constTrue , constTrue , constFalse , filter ) , cacheValueEq } ; } param inputEq param cacheValueEq export function available < R , E , A > ( inputEq , cacheValueEq ) { return fromSuccessFilter ( inputEq , constTrue , cacheValueEq ) ; } param inputEq param cacheValueEq export function refetch < R , E , A > ( inputEq , cacheValueEq ) { return fromSuccessFilter ( inputEq , constFalse , cacheValueEq ) ; } /**
 * Returns a cache strategy builder that returns the cached value up to `afterMs` milliseconds old,
 * if available, and otherwise re-fetches
 */ export function expire ( afterMs ) { return < R , E , A > ( inputEq , cacheValueEq ) : Strategy < R , E , A > => { return fromSuccessFilter ( inputEq , ( _ , updated ) => updated . getTime ( ) >= Date . now ( ) - afterMs , cacheValueEq ) ; } ; } export function shallowEqual ( a , b ) { if ( a === b ) { return true ; } if ( typeof a !== typeof b ) { return false ; } if ( typeof a === 'number' && typeof b === 'number' && isNaN ( a ) && isNaN ( b ) ) { return true ; } if ( ( a == null && b != null ) || ( a != null && b == null ) ) { return false ; } if ( typeof a === 'object' && typeof b === 'object' ) { for ( let k in a ) { if ( ( a as any ) [ k ] !== ( b as any ) [ k ] ) { return false ; } } for ( let k in b ) { if ( ( a as any ) [ k ] !== ( b as any ) [ k ] ) { return false ; } } return true ; } return false ; } export const eqShallow = Eq . fromEquals ( shallowEqual ) ; export const strategy = { URI } ;