forked from planttheidea/micro-memoize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
132 lines (111 loc) · 3.15 KB
/
index.d.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
export interface Dictionary<Type> {
[key: string]: Type;
[index: number]: Type;
}
export type AnyFn = (...args: any[]) => any;
export type Key = any[];
export type RawKey = Key | IArguments;
export type Value = any;
export interface CacheSnapshot {
keys: Key[];
size: number;
values: Value[];
}
export class Cache<Fn extends AnyFn> {
readonly canTransformKey: boolean;
readonly getKeyIndex: KeyIndexGetter;
readonly options: NormalizedOptions<Fn>;
readonly shouldCloneArguments: boolean;
readonly shouldUpdateOnAdd: boolean;
readonly shouldUpdateOnChange: boolean;
readonly shouldUpdateOnHit: boolean;
/**
* The prevents call arguments which have cached results.
*/
keys: Key[];
/**
* The results of previous cached calls.
*/
values: Value[];
constructor(options: NormalizedOptions<Fn>);
/**
* The number of cached [key,value] results.
*/
get size(): number;
/**
* A copy of the cache at a moment in time. This is useful
* to compare changes over time, since the cache mutates
* internally for performance reasons.
*/
get snapshot(): CacheSnapshot;
/**
* Order the array based on a Least-Recently-Used basis.
*/
orderByLru(key: Key, value: Value, startingIndex: number): void;
/**
* Update the promise method to auto-remove from cache if rejected, and
* if resolved then fire cache hit / changed.
*/
updateAsyncCache(memoized: Memoized<Fn>): void;
}
export type EqualityComparator = (object1: any, object2: any) => boolean;
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean;
export type CacheModifiedHandler<Fn extends AnyFn> = (
cache: Cache<Fn>,
options: NormalizedOptions<Fn>,
memoized: Memoized<Fn>,
) => void;
export type KeyTransformer = (args: Key) => Key;
export type KeyIndexGetter = (keyToMatch: RawKey) => number;
export interface StandardOptions<Fn extends AnyFn> {
isEqual?: EqualityComparator;
isMatchingKey?: MatchingKeyComparator;
isPromise?: boolean;
maxSize?: number;
onCacheAdd?: CacheModifiedHandler<Fn>;
onCacheChange?: CacheModifiedHandler<Fn>;
onCacheHit?: CacheModifiedHandler<Fn>;
transformKey?: KeyTransformer;
}
export interface Options<Fn extends AnyFn>
extends StandardOptions<Fn>,
Dictionary<any> {}
export interface NormalizedOptions<Fn extends AnyFn> extends Options<Fn> {
isEqual: EqualityComparator;
isPromise: boolean;
maxSize: number;
}
export type Memoized<Fn extends AnyFn> = Fn &
Dictionary<any> & {
cache: Cache<Fn>;
fn: Fn;
isMemoized: true;
options: NormalizedOptions<Fn>;
};
/**
* @deprecated
* All types declared on this namespace are available for direct import
* from the package. In a future release, this namespace will be removed
* in favor of those direct imports.
*/
export declare namespace MicroMemoize {
export type {
Cache,
CacheModifiedHandler,
EqualityComparator,
Key,
KeyIndexGetter,
KeyTransformer,
MatchingKeyComparator,
Memoized,
NormalizedOptions,
Options,
StandardOptions,
RawKey,
Value,
};
}
export default function memoize<Fn extends AnyFn>(
fn: Fn | Memoized<Fn>,
options?: Options<Fn>,
): Memoized<Fn>;