-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
55 lines (47 loc) · 1.45 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type {Cache} from './cache';
import type {Decorator, MemoiseCacheGetFn, SerialiserFn} from './core';
import {
applyDecorator,
defaultSerialiser,
identitySerialiser,
MEMOISE_CACHE,
MEMOISE_CACHE_TYPED,
memoiseArglessFunction,
memoiseFunction
} from './core';
/**
* Memoise the method's return value based on call arguments
* @param serialiser Serialiser to use for generating the cache key. Defaults to {@link defaultSerialiser}.
*/
function Memoise<T, A extends [any, ...any[]], R>(serialiser?: SerialiserFn<T, A>): Decorator<T, A, R> {
return applyDecorator(true, serialiser ?? defaultSerialiser);
}
/** Memoise the method's return value disregarding call arguments */
function MemoiseAll<T, R>(): Decorator<T, [], R> {
return applyDecorator(false);
}
/** Memoise based on the 1st argument */
function MemoiseIdentity<T, A, R>(): Decorator<T, [A], R> {
return applyDecorator(true, identitySerialiser);
}
export {
Memoise,
MemoiseAll,
MemoiseIdentity,
MEMOISE_CACHE,
MEMOISE_CACHE_TYPED,
defaultSerialiser,
memoiseArglessFunction,
memoiseFunction
};
export type {SerialiserFn, Cache};
declare global {
interface Function {
/** Always defined on decorated methods and explicitly memoised functions */
readonly [MEMOISE_CACHE]?: Cache;
}
interface CallableFunction {
/** Always returns non-undefined on decorated methods and explicitly memoised functions */
readonly [MEMOISE_CACHE_TYPED]: MemoiseCacheGetFn;
}
}