1 | type AnyFunction = (...arguments_: any) => any;
|
2 | interface CacheStorageContent<ValueType> {
|
3 | data: ValueType;
|
4 | maxAge: number;
|
5 | }
|
6 | interface CacheStorage<KeyType, ValueType> {
|
7 | has: (key: KeyType) => boolean;
|
8 | get: (key: KeyType) => CacheStorageContent<ValueType> | undefined;
|
9 | set: (key: KeyType, value: CacheStorageContent<ValueType>) => void;
|
10 | delete: (key: KeyType) => void;
|
11 | clear?: () => void;
|
12 | }
|
13 | export interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
|
14 | /**
|
15 | Milliseconds until the cache expires.
|
16 |
|
17 | @default Infinity
|
18 | */
|
19 | readonly maxAge?: number;
|
20 | /**
|
21 | 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).
|
22 |
|
23 | A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).
|
24 |
|
25 | You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
|
26 |
|
27 | ```
|
28 | import mem from 'mem';
|
29 |
|
30 | mem(function_, {cacheKey: JSON.stringify});
|
31 | ```
|
32 |
|
33 | 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.
|
34 |
|
35 | ```
|
36 | import mem from 'mem';
|
37 | import serializeJavascript from 'serialize-javascript';
|
38 |
|
39 | mem(function_, {cacheKey: serializeJavascript});
|
40 | ```
|
41 |
|
42 | @default arguments_ => arguments_[0]
|
43 | @example arguments_ => JSON.stringify(arguments_)
|
44 | */
|
45 | readonly cacheKey?: (arguments_: Parameters<FunctionToMemoize>) => CacheKeyType;
|
46 | /**
|
47 | 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.
|
48 |
|
49 | @default new Map()
|
50 | @example new WeakMap()
|
51 | */
|
52 | readonly cache?: CacheStorage<CacheKeyType, ReturnType<FunctionToMemoize>>;
|
53 | }
|
54 | /**
|
55 | [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.
|
56 |
|
57 | @param fn - Function to be memoized.
|
58 |
|
59 | @example
|
60 | ```
|
61 | import mem from 'mem';
|
62 |
|
63 | let index = 0;
|
64 | const counter = () => ++index;
|
65 | const memoized = mem(counter);
|
66 |
|
67 | memoized('foo');
|
68 | //=> 1
|
69 |
|
70 | // Cached as it's the same argument
|
71 | memoized('foo');
|
72 | //=> 1
|
73 |
|
74 | // Not cached anymore as the arguments changed
|
75 | memoized('bar');
|
76 | //=> 2
|
77 |
|
78 | memoized('bar');
|
79 | //=> 2
|
80 | ```
|
81 | */
|
82 | export default function mem<FunctionToMemoize extends AnyFunction, CacheKeyType>(fn: FunctionToMemoize, { cacheKey, cache, maxAge, }?: Options<FunctionToMemoize, CacheKeyType>): FunctionToMemoize;
|
83 | /**
|
84 | @returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
|
85 |
|
86 | @example
|
87 | ```
|
88 | import {memDecorator} from 'mem';
|
89 |
|
90 | class Example {
|
91 | index = 0
|
92 |
|
93 | @memDecorator()
|
94 | counter() {
|
95 | return ++this.index;
|
96 | }
|
97 | }
|
98 |
|
99 | class ExampleWithOptions {
|
100 | index = 0
|
101 |
|
102 | @memDecorator({maxAge: 1000})
|
103 | counter() {
|
104 | return ++this.index;
|
105 | }
|
106 | }
|
107 | ```
|
108 | */
|
109 | export declare function memDecorator<FunctionToMemoize extends AnyFunction, CacheKeyType>(options?: Options<FunctionToMemoize, CacheKeyType>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
110 | /**
|
111 | Clear all cached data of a memoized function.
|
112 |
|
113 | @param fn - Memoized function.
|
114 | */
|
115 | export declare function memClear(fn: AnyFunction): void;
|
116 | export {};
|