import pickBy from 'lodash/pickBy' ; import { Identifier } from '../types' ; const defaultCacheDuration = 10 * 60 * 1000 ; example interface FetchedOutDates { [ key ] : Date ; [ key ] : Date ; } param newRecordIds param oldRecordFetchedAt param now param cacheDuration const getFetchedAt = ( newRecordIds = [ ] , oldRecordFetchedAt = { } , now = new Date ( ) , cacheDuration = defaultCacheDuration ) : FetchedOutDates => { const newFetchedAt = { } ; newRecordIds . forEach ( recordId => ( newFetchedAt [ recordId ] = now ) ) ; const latestValidDate = new Date ( ) ; latestValidDate . setTime ( latestValidDate . getTime ( ) - cacheDuration ) ; const stillValidFetchedAt = pickBy ( oldRecordFetchedAt , date => date > latestValidDate ) ; return { ... stillValidFetchedAt , ... newFetchedAt , } ; } ; export default getFetchedAt ;