import { Cache , Expiration , Fn , IsEqual , IsMatchingKey , Key , OnCacheOperation , Options , } from './types' ; import { createFindKeyIndex , findExpirationIndex } from './utils' ; private description param expirations param key param shouldRemove export function clearExpiration ( expirations , key , shouldRemove ? ) { const expirationIndex = findExpirationIndex ( expirations , key ) ; if ( expirationIndex !== - 1 ) { clearTimeout ( expirations [ expirationIndex ] . timeoutId ) ; if ( shouldRemove ) { expirations . splice ( expirationIndex , 1 ) ; } } } private description param expirationMethod param maxAge returns export function createTimeout ( expirationMethod , maxAge ) { const timeoutId = setTimeout ( expirationMethod , maxAge ) ; if ( typeof timeoutId . unref === 'function' ) { timeoutId . unref ( ) ; } return timeoutId ; } private description param expirations param options param isEqual param isMatchingKey returns export function createOnCacheAddSetExpiration ( expirations , options , isEqual , isMatchingKey ) { const { maxAge } = options ; return function onCacheAdd ( cache , moizedOptions , moized ) { const key = cache . keys [ 0 ] ; if ( findExpirationIndex ( expirations , key ) === - 1 ) { const expirationMethod = function ( ) { const findKeyIndex = createFindKeyIndex ( isEqual , isMatchingKey ) ; const keyIndex = findKeyIndex ( cache . keys , key ) ; const value = cache . values [ keyIndex ] ; if ( ~ keyIndex ) { cache . keys . splice ( keyIndex , 1 ) ; cache . values . splice ( keyIndex , 1 ) ; if ( typeof options . onCacheChange === 'function' ) { options . onCacheChange ( cache , moizedOptions , moized ) ; } } clearExpiration ( expirations , key , true ) ; if ( typeof options . onExpire === 'function' && options . onExpire ( key ) === false ) { cache . keys . unshift ( key ) ; cache . values . unshift ( value ) ; onCacheAdd ( cache , moizedOptions , moized ) ; if ( typeof options . onCacheChange === 'function' ) { options . onCacheChange ( cache , moizedOptions , moized ) ; } } } ; expirations . push ( { expirationMethod , key , timeoutId : createTimeout ( expirationMethod , maxAge ) , } ) ; } } ; } private description param expirations param options returns export function createOnCacheHitResetExpiration ( expirations , options ) { return function onCacheHit ( cache ) { const key = cache . keys [ 0 ] ; const expirationIndex = findExpirationIndex ( expirations , key ) ; if ( ~ expirationIndex ) { clearExpiration ( expirations , key , false ) ; expirations [ expirationIndex ] . timeoutId = createTimeout ( expirations [ expirationIndex ] . expirationMethod , options . maxAge ) ; } } ; } private description param expirations param options param isEqual param isMatchingKey returns export function getMaxAgeOptions ( expirations , options , isEqual , isMatchingKey ) { const onCacheAdd = typeof options . maxAge === 'number' && isFinite ( options . maxAge ) ? createOnCacheAddSetExpiration ( expirations , options , isEqual , isMatchingKey ) : undefined ; return { onCacheAdd , onCacheHit : onCacheAdd && options . updateExpire ? createOnCacheHitResetExpiration ( expirations , options ) : undefined , } ; }