import memoize from 'micro-memoize' ; import { createMoizedComponent } from './component' ; import { DEFAULT_OPTIONS } from './constants' ; import { createMoizeInstance } from './instance' ; import { getMaxAgeOptions } from './maxAge' ; import { createOnCacheOperation , getIsEqual , getIsMatchingKey , getTransformKey , } from './options' ; import { clearStats , collectStats , getDefaultProfileName , getStats , getStatsOptions , statsCache , } from './stats' ; import { Expiration , IsEqual , IsMatchingKey , MicroMemoizeOptions , Moize , Moizeable , Moized , OnExpire , Options , Serialize , TransformKey , UpdateCacheForKey , } from './types' ; import { createRefreshableMoized } from './updateCacheForKey' ; import { combine , compose , isMoized , mergeOptions , setName } from './utils' ; export * from './types' ; module description example param fn param options returns const moize = function < Fn extends Moizeable , PassedOptions extends Options > ( fn , passedOptions ? ) { type CombinedOptions = Options & PassedOptions ; const options = passedOptions || DEFAULT_OPTIONS ; if ( isMoized ( fn ) ) { const moizeable = fn . originalFunction as Fn ; const mergedOptions = mergeOptions ( fn . options , options ) as CombinedOptions ; return moize < Fn , CombinedOptions > ( moizeable , mergedOptions ) ; } if ( typeof fn === 'object' ) { return function < CurriedFn extends Moizeable , CurriedOptions extends Options > ( curriedFn , curriedOptions ) { type CombinedCurriedOptions = CombinedOptions & CurriedOptions ; if ( typeof curriedFn === 'function' ) { const mergedOptions = mergeOptions ( fn as CombinedOptions , curriedOptions ) as CombinedCurriedOptions ; return moize ( curriedFn , mergedOptions ) ; } const mergedOptions = mergeOptions ( fn as CombinedOptions , curriedFn as CurriedOptions ) ; return moize ( mergedOptions ) ; } ; } if ( options . isReact ) { return createMoizedComponent ( moize , fn , options ) ; } const coalescedOptions = { ... DEFAULT_OPTIONS , ... options , maxAge : typeof options . maxAge === 'number' && options . maxAge >= 0 ? options . maxAge : DEFAULT_OPTIONS . maxAge , maxArgs : typeof options . maxArgs === 'number' && options . maxArgs >= 0 ? options . maxArgs : DEFAULT_OPTIONS . maxArgs , maxSize : typeof options . maxSize === 'number' && options . maxSize >= 0 ? options . maxSize : DEFAULT_OPTIONS . maxSize , profileName : options . profileName || getDefaultProfileName ( fn ) , } ; const expirations = [ ] ; const { matchesArg : equalsIgnored , isDeepEqual : isDeepEqualIgnored , isPromise , isReact : isReactIgnored , isSerialized : isSerialzedIgnored , isShallowEqual : isShallowEqualIgnored , matchesKey : matchesKeyIgnored , maxAge : maxAgeIgnored , maxArgs : maxArgsIgnored , maxSize , onCacheAdd , onCacheChange , onCacheHit , onExpire : onExpireIgnored , profileName : profileNameIgnored , serializer : serializerIgnored , updateCacheForKey , transformArgs : transformArgsIgnored , updateExpire : updateExpireIgnored , ... customOptions } = coalescedOptions ; const isEqual = getIsEqual ( coalescedOptions ) ; const isMatchingKey = getIsMatchingKey ( coalescedOptions ) ; const maxAgeOptions = getMaxAgeOptions ( expirations , coalescedOptions , isEqual , isMatchingKey ) ; const statsOptions = getStatsOptions ( coalescedOptions ) ; const transformKey = getTransformKey ( coalescedOptions ) ; const microMemoizeOptions = { ... customOptions , isEqual , isMatchingKey , isPromise , maxSize , onCacheAdd : createOnCacheOperation ( combine ( onCacheAdd , maxAgeOptions . onCacheAdd , statsOptions . onCacheAdd ) ) , onCacheChange : createOnCacheOperation ( onCacheChange ) , onCacheHit : createOnCacheOperation ( combine ( onCacheHit , maxAgeOptions . onCacheHit , statsOptions . onCacheHit ) ) , transformKey , } ; const memoized = memoize ( fn , microMemoizeOptions ) ; let moized = createMoizeInstance < Fn , CombinedOptions > ( memoized , { expirations , options : coalescedOptions , originalFunction : fn , } ) ; if ( updateCacheForKey ) { moized = createRefreshableMoized < typeof moized > ( moized ) ; } setName ( moized , ( fn as Moizeable ) . name , options . profileName ) ; return moized ; } ; function name memberof alias description moize . clearStats = clearStats ; function name memberof alias description moize . collectStats = collectStats ; function name memberof alias description param moized returns moize . compose = function ( ... moized ) { return compose < Moize > ( ... moized ) || moize ; } ; function name memberof alias description returns moize . deep = moize ( { isDeepEqual : true } ) ; function name memberof alias description returns moize . getStats = getStats ; function name memberof alias description returns moize . infinite = moize ( { maxSize : Infinity } ) ; function name memberof alias description returns moize . isCollectingStats = function isCollectingStats ( ) : boolean { return statsCache . isCollectingStats ; } ; function name memberof alias description param fn returns moize . isMoized = function isMoized ( fn ) : fn is Moized { return typeof fn === 'function' && ! ! fn . isMoized ; } ; function name memberof alias description param keyMatcher returns moize . matchesArg = function ( argMatcher ) { return moize ( { matchesArg : argMatcher } ) ; } ; function name memberof alias description param keyMatcher returns moize . matchesKey = function ( keyMatcher ) { return moize ( { matchesKey : keyMatcher } ) ; } ; function maxAge < MaxAge extends number > ( maxAge ) ; function maxAge < MaxAge extends number , UpdateExpire extends boolean > ( maxAge , expireOptions ) ; function maxAge < MaxAge extends number , ExpireHandler extends OnExpire > ( maxAge , expireOptions ) ; function maxAge < MaxAge extends number , ExpireHandler extends OnExpire , ExpireOptions extends { onExpire : ExpireHandler ; } > ( maxAge , expireOptions ) ; function maxAge < MaxAge extends number , UpdateExpire extends boolean , ExpireOptions extends { updateExpire : UpdateExpire ; } > ( maxAge , expireOptions ) ; function maxAge < MaxAge extends number , ExpireHandler extends OnExpire , UpdateExpire extends boolean , ExpireOptions extends { onExpire : ExpireHandler ; updateExpire : UpdateExpire ; } > ( maxAge , expireOptions ) ; function maxAge < MaxAge extends number , ExpireHandler extends OnExpire , UpdateExpire extends boolean , ExpireOptions extends { onExpire ? : ExpireHandler ; updateExpire ? : UpdateExpire ; } > ( maxAge , expireOptions ? ) { if ( expireOptions === true ) { return moize ( { maxAge , updateExpire : expireOptions , } ) ; } if ( typeof expireOptions === 'object' ) { const { onExpire , updateExpire } = expireOptions ; return moize ( { maxAge , onExpire , updateExpire , } ) ; } if ( typeof expireOptions === 'function' ) { return moize ( { maxAge , onExpire : expireOptions , updateExpire : true , } ) ; } return moize ( { maxAge } ) ; } function name memberof alias description param maxAge returns moize . maxAge = maxAge ; function name memberof alias description param maxArgs returns moize . maxArgs = function maxArgs ( maxArgs ) { return moize ( { maxArgs } ) ; } ; function name memberof alias description param maxSize returns moize . maxSize = function maxSize ( maxSize ) { return moize ( { maxSize } ) ; } ; function name memberof alias description returns moize . profile = function ( profileName ) { return moize ( { profileName } ) ; } ; function name memberof alias description returns moize . promise = moize ( { isPromise : true , updateExpire : true , } ) ; function name memberof alias description returns moize . react = moize ( { isReact : true } ) ; function name memberof alias description returns moize . serialize = moize ( { isSerialized : true } ) ; function name memberof alias description returns moize . serializeWith = function ( serializer ) { return moize ( { isSerialized : true , serializer } ) ; } ; function name memberof alias description returns moize . shallow = moize ( { isShallowEqual : true } ) ; function name memberof alias description param transformArgs returns moize . transformArgs = < Transformer extends TransformKey > ( transformArgs ) => moize ( { transformArgs } ) ; function name memberof alias description param updateCacheForKey returns moize . updateCacheForKey = < UpdateWhen extends UpdateCacheForKey > ( updateCacheForKey ) => moize ( { updateCacheForKey } ) ; Object . defineProperty ( moize , 'default' , { configurable : false , enumerable : false , value : moize , writable : false , } ) ; export default moize ;