|
| 1 | +'use strict'; |
| 2 | +importmimicFn= require('mimic-fn'); |
| 3 | +importmapAgeCleaner= require('map-age-cleaner'); |
| 4 | + |
| 5 | +constcacheStore=newWeakMap<(...arguments_:any[])=>any>(); |
| 6 | +interfaceCacheStorage<KeyType,ValueType>{ |
| 7 | +has:(key:KeyType)=>boolean; |
| 8 | +get:(key:KeyType)=>ValueType|undefined; |
| 9 | +set:(key:KeyType,value:ValueType)=>void; |
| 10 | +delete:(key:KeyType)=>void; |
| 11 | +clear?:()=>void; |
| 12 | +} |
| 13 | + |
| 14 | +interfaceOptions< |
| 15 | +ArgumentsTypeextendsunknown[], |
| 16 | +CacheKeyType, |
| 17 | +ReturnType |
| 18 | +>{ |
| 19 | +/** |
| 20 | +Milliseconds until the cache expires. |
| 21 | +
|
| 22 | +@default Infinity |
| 23 | +*/ |
| 24 | +readonlymaxAge?:number; |
| 25 | + |
| 26 | +/** |
| 27 | +Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). |
| 28 | +
|
| 29 | +A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option). |
| 30 | +
|
| 31 | +You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible: |
| 32 | +
|
| 33 | +``` |
| 34 | +import mem = require('mem'); |
| 35 | +
|
| 36 | +mem(function_, {cacheKey: JSON.stringify}); |
| 37 | +``` |
| 38 | +
|
| 39 | +Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on. |
| 40 | +
|
| 41 | +``` |
| 42 | +import mem = require('mem'); |
| 43 | +import serializeJavascript = require('serialize-javascript'); |
| 44 | +
|
| 45 | +mem(function_, {cacheKey: serializeJavascript}); |
| 46 | +``` |
| 47 | +
|
| 48 | +@default arguments_ => arguments_[0] |
| 49 | +@example arguments_ => JSON.stringify(arguments_) |
| 50 | +*/ |
| 51 | +readonlycacheKey?:(arguments_:ArgumentsType)=>CacheKeyType; |
| 52 | + |
| 53 | +/** |
| 54 | +Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. |
| 55 | +
|
| 56 | +@default new Map() |
| 57 | +@example new WeakMap() |
| 58 | +*/ |
| 59 | +readonlycache?:CacheStorage<CacheKeyType,{data:ReturnType;maxAge:number}>; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | +[Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input. |
| 64 | +
|
| 65 | +@param fn - Function to be memoized. |
| 66 | +
|
| 67 | +@example |
| 68 | +``` |
| 69 | +import mem = require('mem'); |
| 70 | +
|
| 71 | +let i = 0; |
| 72 | +const counter = () => ++i; |
| 73 | +const memoized = mem(counter); |
| 74 | +
|
| 75 | +memoized('foo'); |
| 76 | +//=> 1 |
| 77 | +
|
| 78 | +// Cached as it's the same arguments |
| 79 | +memoized('foo'); |
| 80 | +//=> 1 |
| 81 | +
|
| 82 | +// Not cached anymore as the arguments changed |
| 83 | +memoized('bar'); |
| 84 | +//=> 2 |
| 85 | +
|
| 86 | +memoized('bar'); |
| 87 | +//=> 2 |
| 88 | +``` |
| 89 | +*/ |
| 90 | +constmem=< |
| 91 | +ArgumentsTypeextendsany[], |
| 92 | +ReturnTypeextendsany, |
| 93 | +CacheKeyType, |
| 94 | +FunctionToMemoizeextends(...arguments_:ArgumentsType)=>ReturnType |
| 95 | +>( |
| 96 | +fn:FunctionToMemoize,{ |
| 97 | +cacheKey, |
| 98 | +cache=newMap(), |
| 99 | +maxAge |
| 100 | +}:Options<ArgumentsType,CacheKeyType,ReturnType>={}):FunctionToMemoize=>{ |
| 101 | +if(typeofmaxAge==='number'){ |
| 102 | +// TODO: drop after https://github.com/SamVerschueren/map-age-cleaner/issues/5 |
| 103 | +//@ts-expect-error |
| 104 | +mapAgeCleaner(cache); |
| 105 | +} |
| 106 | + |
| 107 | +constmemoized=function(this:any, ...arguments_){ |
| 108 | +constkey=cacheKey ?cacheKey(arguments_) :arguments_[0]; |
| 109 | + |
| 110 | +constcacheItem=cache.get(key); |
| 111 | +if(cacheItem){ |
| 112 | +returncacheItem.data; |
| 113 | +} |
| 114 | + |
| 115 | +constresult=fn.apply(this,arguments_); |
| 116 | + |
| 117 | +cache.set(key,{ |
| 118 | +data:result, |
| 119 | +maxAge:maxAge ?Date.now()+maxAge :Infinity |
| 120 | +}); |
| 121 | + |
| 122 | +returnresult; |
| 123 | +}asFunctionToMemoize; |
| 124 | + |
| 125 | +try{ |
| 126 | +// The below call will throw in some host environments |
| 127 | +// See https://github.com/sindresorhus/mimic-fn/issues/10 |
| 128 | +mimicFn(memoized,fn); |
| 129 | +}catch{} |
| 130 | + |
| 131 | +cacheStore.set(memoized,cache); |
| 132 | + |
| 133 | +returnmemoized; |
| 134 | +}; |
| 135 | + |
| 136 | +export=mem; |
| 137 | + |
| 138 | +/** |
| 139 | +Clear all cached data of a memoized function. |
| 140 | +
|
| 141 | +@param fn - Memoized function. |
| 142 | +*/ |
| 143 | +mem.clear=(fn:(...arguments_:any[])=>any):void=>{ |
| 144 | +if(!cacheStore.has(fn)){ |
| 145 | +thrownewError('Can\'t clear a function that was not memoized!'); |
| 146 | +} |
| 147 | + |
| 148 | +constcache=cacheStore.get(fn); |
| 149 | +if(typeofcache.clear==='function'){ |
| 150 | +cache.clear(); |
| 151 | +} |
| 152 | +}; |