@blumintinc/typescript-memoize 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +342 -0
- package/dist/es2015/memoize-decorator.d.ts +10 -0
- package/dist/es2015/memoize-decorator.js +219 -0
- package/dist/es2015/memoize-decorator.js.map +1 -0
- package/dist/memoize-decorator.d.ts +10 -0
- package/dist/memoize-decorator.js +249 -0
- package/dist/memoize-decorator.js.map +1 -0
- package/package.json +65 -0
- package/src/memoize-decorator.ts +250 -0
package/README.md
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# typescript-memoize
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/typescript-memoize)
|
|
4
|
+
[](https://raw.githubusercontent.com/darrylhodgins/typescript-memoize/master/LICENSE)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A memoize decorator for Typescript.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install --save typescript-memoize
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
@Memoize(hashFunction?: (...args: any[]) => any)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You can use it in several ways:
|
|
22
|
+
|
|
23
|
+
* Memoize a `get` accessor,
|
|
24
|
+
* Memoize a method which takes no parameters,
|
|
25
|
+
* Memoize a method which varies based on its first parameter only,
|
|
26
|
+
* Memoize a method which varies based on some combination of parameters
|
|
27
|
+
* Memoize a method using objects with deep equality comparison (default)
|
|
28
|
+
|
|
29
|
+
You can call memoized methods *within* the same class, too. This could be useful if you want to memoize the return value for an entire data set, and also a filtered or mapped version of that same set.
|
|
30
|
+
|
|
31
|
+
## Memoize a `get` accessor, or a method which takes no parameters
|
|
32
|
+
|
|
33
|
+
These both work the same way. Subsequent calls to a memoized method without parameters, or to a `get` accessor, always return the same value.
|
|
34
|
+
|
|
35
|
+
I generally consider it an anti-pattern for a call to a `get` accessor to trigger an expensive operation. Simply adding `Memoize()` to a `get` allows for seamless lazy-loading.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import {Memoize,MemoizeExpiring} from 'typescript-memoize';
|
|
39
|
+
|
|
40
|
+
class SimpleFoo {
|
|
41
|
+
|
|
42
|
+
// Memoize a method without parameters
|
|
43
|
+
@Memoize()
|
|
44
|
+
public getAllTheData() {
|
|
45
|
+
// do some expensive operation to get data
|
|
46
|
+
return data;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Memoize a method and expire the value after some time in milliseconds
|
|
50
|
+
@MemoizeExpiring(5000)
|
|
51
|
+
public getDataForSomeTime() {
|
|
52
|
+
// do some expensive operation to get data
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Memoize a getter
|
|
57
|
+
@Memoize()
|
|
58
|
+
public get someValue() {
|
|
59
|
+
// do some expensive operation to calculate value
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
And then we call them from somewhere else in our code:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
let simpleFoo = new SimpleFoo();
|
|
70
|
+
|
|
71
|
+
// Memoizes a calculated value and returns it:
|
|
72
|
+
let methodVal1 = simpleFoo.getAllTheData();
|
|
73
|
+
|
|
74
|
+
// Returns memoized value
|
|
75
|
+
let methodVal2 = simpleFoo.getAllTheData();
|
|
76
|
+
|
|
77
|
+
// Memoizes (lazy-loads) a calculated value and returns it:
|
|
78
|
+
let getterVal1 = simpleFoo.someValue;
|
|
79
|
+
|
|
80
|
+
// Returns memoized value
|
|
81
|
+
let getterVal2 = simpleFoo.someValue;
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Memoize a method which varies based on its first parameter only
|
|
86
|
+
|
|
87
|
+
Subsequent calls to this style of memoized method will always return the same value.
|
|
88
|
+
|
|
89
|
+
I'm not really sure why anyone would use this approach to memoize a method with *more* than one parameter, but it's possible.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import {Memoize} from 'typescript-memoize';
|
|
93
|
+
|
|
94
|
+
class ComplicatedFoo {
|
|
95
|
+
|
|
96
|
+
// Memoize a method without parameters (just like the first example)
|
|
97
|
+
@Memoize()
|
|
98
|
+
public getAllTheData() {
|
|
99
|
+
// do some expensive operation to get data
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Memoize a method with one parameter
|
|
104
|
+
@Memoize()
|
|
105
|
+
public getSomeOfTheData(id: number) {
|
|
106
|
+
let allTheData = this.getAllTheData(); // if you want to!
|
|
107
|
+
// do some expensive operation to get data
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Memoize a method with multiple parameters
|
|
112
|
+
// Only the first parameter will be used for memoization
|
|
113
|
+
@Memoize()
|
|
114
|
+
public getGreeting(name: string, planet: string) {
|
|
115
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
We call these methods from somewhere else in our code:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
let complicatedFoo = new ComplicatedFoo();
|
|
125
|
+
|
|
126
|
+
// Returns calculated value and memoizes it:
|
|
127
|
+
let oneParam1 = complicatedFoo.getSomeOfTheData();
|
|
128
|
+
|
|
129
|
+
// Returns memoized value
|
|
130
|
+
let oneParam2 = complicatedFoo.getSomeOfTheData();
|
|
131
|
+
|
|
132
|
+
// Memoizes a calculated value and returns it:
|
|
133
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
134
|
+
let greeterVal1 = complicatedFoo.getGreeting('Darryl', 'Earth');
|
|
135
|
+
|
|
136
|
+
// Ignores the second parameter, and returns memoized value
|
|
137
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
138
|
+
let greeterVal2 = complicatedFoo.getGreeting('Darryl', 'Mars');
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Memoize a method which varies based on some combination of parameters
|
|
142
|
+
|
|
143
|
+
Pass in a `hashFunction` which takes the same parameters as your target method, to memoize values based on all parameters, or some other custom logic
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import {Memoize} from 'typescript-memoize';
|
|
147
|
+
|
|
148
|
+
class MoreComplicatedFoo {
|
|
149
|
+
|
|
150
|
+
// Memoize a method with multiple parameters
|
|
151
|
+
// Memoize will remember values based on keys like: 'name;planet'
|
|
152
|
+
@Memoize((name: string, planet: string) => {
|
|
153
|
+
return name + ';' + planet;
|
|
154
|
+
})
|
|
155
|
+
public getBetterGreeting(name: string, planet: string) {
|
|
156
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Memoize based on some other logic
|
|
160
|
+
@Memoize(() => {
|
|
161
|
+
return new Date();
|
|
162
|
+
})
|
|
163
|
+
public memoryLeak(greeting: string) {
|
|
164
|
+
return greeting + '!!!!!';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Memoize also accepts parameters via a single object argument
|
|
168
|
+
@Memoize({
|
|
169
|
+
expiring: 10000, // milliseconds
|
|
170
|
+
hashFunction: (name: string, planet: string) => {
|
|
171
|
+
return name + ';' + planet;
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
public getSameBetterGreeting(name: string, planet: string) {
|
|
175
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
We call these methods from somewhere else in our code. By now you should be getting the idea:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
let moreComplicatedFoo = new MoreComplicatedFoo();
|
|
185
|
+
|
|
186
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
187
|
+
let greeterVal1 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Earth');
|
|
188
|
+
|
|
189
|
+
// 'Hello, Darryl! Welcome to Mars'
|
|
190
|
+
let greeterVal2 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Mars');
|
|
191
|
+
|
|
192
|
+
// Fill up the computer with useless greetings:
|
|
193
|
+
let greeting = moreComplicatedFoo.memoryLeak('Hello');
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Memoize accepts one or more "tag" strings that allow the cache to be invalidated on command
|
|
198
|
+
|
|
199
|
+
Passing an array with one or more "tag" strings these will allow you to later clear the cache of results associated with methods or the `get`accessors using the `clear()` function.
|
|
200
|
+
|
|
201
|
+
The `clear()` function also requires an array of "tag" strings.
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import {Memoize} from 'typescript-memoize';
|
|
205
|
+
|
|
206
|
+
class ClearableFoo {
|
|
207
|
+
|
|
208
|
+
// Memoize accepts tags
|
|
209
|
+
@Memoize({ tags: ["foo", "bar"] })
|
|
210
|
+
public getClearableGreeting(name: string, planet: string) {
|
|
211
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
// Memoize accepts tags
|
|
216
|
+
@Memoize({ tags: ["bar"] })
|
|
217
|
+
public getClearableSum(a: number, b: number) {
|
|
218
|
+
return a + b;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
We call these methods from somewhere else in our code.
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import {clear} from 'typescript-memoize';
|
|
228
|
+
|
|
229
|
+
let clearableFoo = new ClearableFoo();
|
|
230
|
+
|
|
231
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
232
|
+
let greeterVal1 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
|
|
233
|
+
|
|
234
|
+
// Ignores the second parameter, and returns memoized value
|
|
235
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
236
|
+
let greeterVal2 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
|
|
237
|
+
|
|
238
|
+
// '3'
|
|
239
|
+
let sum1 = clearableFoo.getClearableSum(2, 1);
|
|
240
|
+
|
|
241
|
+
// Ignores the second parameter, and returns memoized value
|
|
242
|
+
// '3'
|
|
243
|
+
let sum2 = clearableFoo.getClearableSum(2, 2);
|
|
244
|
+
|
|
245
|
+
clear(["foo"]);
|
|
246
|
+
|
|
247
|
+
// The memoized values are cleared, return a new value
|
|
248
|
+
// 'Hello, Darryl! Welcome to Mars'
|
|
249
|
+
let greeterVal3 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
// The memoized value is not associated with 'foo' tag, returns memoized value
|
|
253
|
+
// '3'
|
|
254
|
+
let sum3 = clearableFoo.getClearableSum(2, 2);
|
|
255
|
+
|
|
256
|
+
clear(["bar"]);
|
|
257
|
+
|
|
258
|
+
// The memoized values are cleared, return a new value
|
|
259
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
260
|
+
let greeterVal4 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
// The memoized values are cleared, return a new value
|
|
264
|
+
// '4'
|
|
265
|
+
let sum4 = clearableFoo.getClearableSum(2, 2);
|
|
266
|
+
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Memoize with deep equality comparison
|
|
270
|
+
|
|
271
|
+
By default, memoization now uses deep equality when comparing objects, which is useful for objects and arrays with the same structure but different references. If needed, you can disable deep equality with the `useDeepEqual: false` option:
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
import {Memoize} from 'typescript-memoize';
|
|
275
|
+
|
|
276
|
+
class DeepEqualityFoo {
|
|
277
|
+
// Uses deep equality comparison by default
|
|
278
|
+
@Memoize()
|
|
279
|
+
public processObject(obj: any) {
|
|
280
|
+
// This will only be called once for objects with the same structure,
|
|
281
|
+
// even if they are different instances
|
|
282
|
+
return expensiveOperation(obj);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Disable deep equality if you want reference equality instead
|
|
286
|
+
@Memoize({
|
|
287
|
+
useDeepEqual: false
|
|
288
|
+
})
|
|
289
|
+
public processObjectWithReferenceEquality(obj: any) {
|
|
290
|
+
// This will be called for each new object reference,
|
|
291
|
+
// even if they have the same structure
|
|
292
|
+
return expensiveOperation(obj);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Combine with other options
|
|
296
|
+
@Memoize({
|
|
297
|
+
expiring: 5000, // expire after 5 seconds
|
|
298
|
+
tags: ["objects"]
|
|
299
|
+
})
|
|
300
|
+
public processComplexObject(obj: any) {
|
|
301
|
+
return expensiveOperation(obj);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
We can call these methods and get memoized results based on deep equality:
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
let deepFoo = new DeepEqualityFoo();
|
|
310
|
+
|
|
311
|
+
// First call with an object
|
|
312
|
+
const result1 = deepFoo.processObject({ id: 123, name: "Test" });
|
|
313
|
+
|
|
314
|
+
// Second call with different object instance but same structure
|
|
315
|
+
// Will return the memoized result without calling the original method again
|
|
316
|
+
const result2 = deepFoo.processObject({ id: 123, name: "Test" });
|
|
317
|
+
|
|
318
|
+
// Call with different object structure
|
|
319
|
+
// Will call the original method again
|
|
320
|
+
const result3 = deepFoo.processObject({ id: 456, name: "Different" });
|
|
321
|
+
|
|
322
|
+
// Deep equality works with complex nested objects too
|
|
323
|
+
const result4 = deepFoo.processComplexObject({
|
|
324
|
+
user: {
|
|
325
|
+
id: 123,
|
|
326
|
+
details: {
|
|
327
|
+
age: 30,
|
|
328
|
+
preferences: ["red", "blue"]
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// Using reference equality will call the method for each new object
|
|
334
|
+
const refObj = { id: 123, name: "Test" };
|
|
335
|
+
const result5 = deepFoo.processObjectWithReferenceEquality(refObj);
|
|
336
|
+
// This will be memoized since it's the same reference
|
|
337
|
+
const result6 = deepFoo.processObjectWithReferenceEquality(refObj);
|
|
338
|
+
// This will call the method again since it's a new reference
|
|
339
|
+
const result7 = deepFoo.processObjectWithReferenceEquality({ id: 123, name: "Test" });
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
The deep equality comparison uses the [fast-deep-equal](https://www.npmjs.com/package/fast-deep-equal) package for efficient deep equality comparisons.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface MemoizeArgs {
|
|
2
|
+
expiring?: number;
|
|
3
|
+
hashFunction?: boolean | ((...args: any[]) => any);
|
|
4
|
+
tags?: string[];
|
|
5
|
+
useDeepEqual?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function Memoize(args?: MemoizeArgs | MemoizeArgs['hashFunction']): (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => void;
|
|
8
|
+
export declare function MemoizeExpiring(expiring: number, hashFunction?: MemoizeArgs['hashFunction']): (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => void;
|
|
9
|
+
export declare function clear(tags: string[]): number;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
const equal = require('fast-deep-equal');
|
|
2
|
+
export function Memoize(args) {
|
|
3
|
+
var _a;
|
|
4
|
+
let hashFunction;
|
|
5
|
+
let duration;
|
|
6
|
+
let tags;
|
|
7
|
+
let useDeepEqual = false;
|
|
8
|
+
if (typeof args === 'object') {
|
|
9
|
+
hashFunction = args.hashFunction;
|
|
10
|
+
duration = args.expiring;
|
|
11
|
+
tags = args.tags;
|
|
12
|
+
useDeepEqual = (_a = args.useDeepEqual) !== null && _a !== void 0 ? _a : false;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
hashFunction = args;
|
|
16
|
+
}
|
|
17
|
+
return (target, propertyKey, descriptor) => {
|
|
18
|
+
if (descriptor.value != null) {
|
|
19
|
+
descriptor.value = getNewFunction(descriptor.value, hashFunction, duration, tags, useDeepEqual);
|
|
20
|
+
}
|
|
21
|
+
else if (descriptor.get != null) {
|
|
22
|
+
descriptor.get = getNewFunction(descriptor.get, hashFunction, duration, tags, useDeepEqual);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw 'Only put a Memoize() decorator on a method or get accessor.';
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function MemoizeExpiring(expiring, hashFunction) {
|
|
30
|
+
return Memoize({
|
|
31
|
+
expiring,
|
|
32
|
+
hashFunction
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const clearCacheTagsMap = new Map();
|
|
36
|
+
export function clear(tags) {
|
|
37
|
+
const cleared = new Set();
|
|
38
|
+
for (const tag of tags) {
|
|
39
|
+
const maps = clearCacheTagsMap.get(tag);
|
|
40
|
+
if (maps) {
|
|
41
|
+
for (const mp of maps) {
|
|
42
|
+
if (!cleared.has(mp)) {
|
|
43
|
+
mp.clear();
|
|
44
|
+
cleared.add(mp);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return cleared.size;
|
|
50
|
+
}
|
|
51
|
+
class DeepEqualMap {
|
|
52
|
+
constructor() {
|
|
53
|
+
this.map = new Map();
|
|
54
|
+
}
|
|
55
|
+
has(key) {
|
|
56
|
+
const entries = Array.from(this.map.values());
|
|
57
|
+
for (const entry of entries) {
|
|
58
|
+
if (equal(entry.key, key)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
get(key) {
|
|
65
|
+
const entries = Array.from(this.map.values());
|
|
66
|
+
for (const entry of entries) {
|
|
67
|
+
if (equal(entry.key, key)) {
|
|
68
|
+
return entry.value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
set(key, value) {
|
|
74
|
+
const entries = Array.from(this.map.entries());
|
|
75
|
+
for (const [serializedKey, entry] of entries) {
|
|
76
|
+
if (equal(entry.key, key)) {
|
|
77
|
+
this.map.delete(serializedKey);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const serializedKey = `${Date.now()}_${Math.random()}`;
|
|
82
|
+
this.map.set(serializedKey, { key, value });
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
clear() {
|
|
86
|
+
this.map.clear();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function getNewFunction(originalMethod, hashFunction, duration = 0, tags, useDeepEqual = false) {
|
|
90
|
+
const propMapName = Symbol(`__memoized_map__`);
|
|
91
|
+
const propDeepMapName = Symbol(`__memoized_deep_map__`);
|
|
92
|
+
return function (...args) {
|
|
93
|
+
let returnedValue;
|
|
94
|
+
if (useDeepEqual) {
|
|
95
|
+
if (!this.hasOwnProperty(propDeepMapName)) {
|
|
96
|
+
Object.defineProperty(this, propDeepMapName, {
|
|
97
|
+
configurable: false,
|
|
98
|
+
enumerable: false,
|
|
99
|
+
writable: false,
|
|
100
|
+
value: new DeepEqualMap()
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
let myMap = this[propDeepMapName];
|
|
104
|
+
if (Array.isArray(tags)) {
|
|
105
|
+
for (const tag of tags) {
|
|
106
|
+
const mapWrapper = {
|
|
107
|
+
clear: () => myMap.clear()
|
|
108
|
+
};
|
|
109
|
+
if (clearCacheTagsMap.has(tag)) {
|
|
110
|
+
clearCacheTagsMap.get(tag).push(mapWrapper);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
clearCacheTagsMap.set(tag, [mapWrapper]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
let hashKey;
|
|
118
|
+
if (hashFunction === true) {
|
|
119
|
+
hashKey = args;
|
|
120
|
+
}
|
|
121
|
+
else if (hashFunction) {
|
|
122
|
+
hashKey = hashFunction.apply(this, args);
|
|
123
|
+
}
|
|
124
|
+
else if (args.length > 0) {
|
|
125
|
+
hashKey = args.length === 1 ? args[0] : args;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
hashKey = this;
|
|
129
|
+
}
|
|
130
|
+
const timestampKey = { __timestamp: true, key: hashKey };
|
|
131
|
+
let isExpired = false;
|
|
132
|
+
if (duration > 0) {
|
|
133
|
+
if (!myMap.has(timestampKey)) {
|
|
134
|
+
isExpired = true;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
let timestamp = myMap.get(timestampKey);
|
|
138
|
+
isExpired = (Date.now() - timestamp) > duration;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (myMap.has(hashKey) && !isExpired) {
|
|
142
|
+
returnedValue = myMap.get(hashKey);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
returnedValue = originalMethod.apply(this, args);
|
|
146
|
+
myMap.set(hashKey, returnedValue);
|
|
147
|
+
if (duration > 0) {
|
|
148
|
+
myMap.set(timestampKey, Date.now());
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
if (!this.hasOwnProperty(propMapName)) {
|
|
154
|
+
Object.defineProperty(this, propMapName, {
|
|
155
|
+
configurable: false,
|
|
156
|
+
enumerable: false,
|
|
157
|
+
writable: false,
|
|
158
|
+
value: new Map()
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
let myMap = this[propMapName];
|
|
162
|
+
if (Array.isArray(tags)) {
|
|
163
|
+
for (const tag of tags) {
|
|
164
|
+
if (clearCacheTagsMap.has(tag)) {
|
|
165
|
+
clearCacheTagsMap.get(tag).push(myMap);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
clearCacheTagsMap.set(tag, [myMap]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (hashFunction || args.length > 0 || duration > 0) {
|
|
173
|
+
let hashKey;
|
|
174
|
+
if (hashFunction === true) {
|
|
175
|
+
hashKey = args.map(a => a.toString()).join('!');
|
|
176
|
+
}
|
|
177
|
+
else if (hashFunction) {
|
|
178
|
+
hashKey = hashFunction.apply(this, args);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
hashKey = args[0];
|
|
182
|
+
}
|
|
183
|
+
const timestampKey = `${hashKey}__timestamp`;
|
|
184
|
+
let isExpired = false;
|
|
185
|
+
if (duration > 0) {
|
|
186
|
+
if (!myMap.has(timestampKey)) {
|
|
187
|
+
isExpired = true;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
let timestamp = myMap.get(timestampKey);
|
|
191
|
+
isExpired = (Date.now() - timestamp) > duration;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (myMap.has(hashKey) && !isExpired) {
|
|
195
|
+
returnedValue = myMap.get(hashKey);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
returnedValue = originalMethod.apply(this, args);
|
|
199
|
+
myMap.set(hashKey, returnedValue);
|
|
200
|
+
if (duration > 0) {
|
|
201
|
+
myMap.set(timestampKey, Date.now());
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
const hashKey = this;
|
|
207
|
+
if (myMap.has(hashKey)) {
|
|
208
|
+
returnedValue = myMap.get(hashKey);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
returnedValue = originalMethod.apply(this, args);
|
|
212
|
+
myMap.set(hashKey, returnedValue);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return returnedValue;
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=memoize-decorator.js.map
|
|
@@ -0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KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;oBACvB,aAAa,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;iBACnC;qBAAM;oBACN,aAAa,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACjD,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;iBAClC;aACD;SACD;QAED,OAAO,aAAa,CAAC;IACtB,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface MemoizeArgs {
|
|
2
|
+
expiring?: number;
|
|
3
|
+
hashFunction?: boolean | ((...args: any[]) => any);
|
|
4
|
+
tags?: string[];
|
|
5
|
+
useDeepEqual?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function Memoize(args?: MemoizeArgs | MemoizeArgs['hashFunction']): (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => void;
|
|
8
|
+
export declare function MemoizeExpiring(expiring: number, hashFunction?: MemoizeArgs['hashFunction']): (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => void;
|
|
9
|
+
export declare function clear(tags: string[]): number;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.clear = exports.MemoizeExpiring = exports.Memoize = void 0;
|
|
13
|
+
var equal = require('fast-deep-equal');
|
|
14
|
+
function Memoize(args) {
|
|
15
|
+
var _a;
|
|
16
|
+
var hashFunction;
|
|
17
|
+
var duration;
|
|
18
|
+
var tags;
|
|
19
|
+
var useDeepEqual = true;
|
|
20
|
+
if (typeof args === 'object') {
|
|
21
|
+
hashFunction = args.hashFunction;
|
|
22
|
+
duration = args.expiring;
|
|
23
|
+
tags = args.tags;
|
|
24
|
+
useDeepEqual = (_a = args.useDeepEqual) !== null && _a !== void 0 ? _a : true;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
hashFunction = args;
|
|
28
|
+
}
|
|
29
|
+
return function (target, propertyKey, descriptor) {
|
|
30
|
+
if (descriptor.value != null) {
|
|
31
|
+
descriptor.value = getNewFunction(descriptor.value, hashFunction, duration, tags, useDeepEqual);
|
|
32
|
+
}
|
|
33
|
+
else if (descriptor.get != null) {
|
|
34
|
+
descriptor.get = getNewFunction(descriptor.get, hashFunction, duration, tags, useDeepEqual);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
throw 'Only put a Memoize() decorator on a method or get accessor.';
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.Memoize = Memoize;
|
|
42
|
+
function MemoizeExpiring(expiring, hashFunction) {
|
|
43
|
+
return Memoize({
|
|
44
|
+
expiring: expiring,
|
|
45
|
+
hashFunction: hashFunction
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
exports.MemoizeExpiring = MemoizeExpiring;
|
|
49
|
+
var clearCacheTagsMap = new Map();
|
|
50
|
+
function clear(tags) {
|
|
51
|
+
var cleared = new Set();
|
|
52
|
+
for (var _i = 0, tags_1 = tags; _i < tags_1.length; _i++) {
|
|
53
|
+
var tag = tags_1[_i];
|
|
54
|
+
var maps = clearCacheTagsMap.get(tag);
|
|
55
|
+
if (maps) {
|
|
56
|
+
for (var _a = 0, maps_1 = maps; _a < maps_1.length; _a++) {
|
|
57
|
+
var mp = maps_1[_a];
|
|
58
|
+
if (!cleared.has(mp)) {
|
|
59
|
+
mp.clear();
|
|
60
|
+
cleared.add(mp);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return cleared.size;
|
|
66
|
+
}
|
|
67
|
+
exports.clear = clear;
|
|
68
|
+
var DeepEqualMap = (function () {
|
|
69
|
+
function DeepEqualMap() {
|
|
70
|
+
this.map = new Map();
|
|
71
|
+
}
|
|
72
|
+
DeepEqualMap.prototype.has = function (key) {
|
|
73
|
+
var entries = Array.from(this.map.values());
|
|
74
|
+
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
|
|
75
|
+
var entry = entries_1[_i];
|
|
76
|
+
if (equal(entry.key, key)) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
};
|
|
82
|
+
DeepEqualMap.prototype.get = function (key) {
|
|
83
|
+
var entries = Array.from(this.map.values());
|
|
84
|
+
for (var _i = 0, entries_2 = entries; _i < entries_2.length; _i++) {
|
|
85
|
+
var entry = entries_2[_i];
|
|
86
|
+
if (equal(entry.key, key)) {
|
|
87
|
+
return entry.value;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
};
|
|
92
|
+
DeepEqualMap.prototype.set = function (key, value) {
|
|
93
|
+
var entries = Array.from(this.map.entries());
|
|
94
|
+
for (var _i = 0, entries_3 = entries; _i < entries_3.length; _i++) {
|
|
95
|
+
var _a = entries_3[_i], serializedKey_1 = _a[0], entry = _a[1];
|
|
96
|
+
if (equal(entry.key, key)) {
|
|
97
|
+
this.map.delete(serializedKey_1);
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
var serializedKey = Date.now() + "_" + Math.random();
|
|
102
|
+
this.map.set(serializedKey, { key: key, value: value });
|
|
103
|
+
return this;
|
|
104
|
+
};
|
|
105
|
+
DeepEqualMap.prototype.clear = function () {
|
|
106
|
+
this.map.clear();
|
|
107
|
+
};
|
|
108
|
+
return DeepEqualMap;
|
|
109
|
+
}());
|
|
110
|
+
function getNewFunction(originalMethod, hashFunction, duration, tags, useDeepEqual) {
|
|
111
|
+
if (duration === void 0) { duration = 0; }
|
|
112
|
+
if (useDeepEqual === void 0) { useDeepEqual = true; }
|
|
113
|
+
var propMapName = Symbol("__memoized_map__");
|
|
114
|
+
var propDeepMapName = Symbol("__memoized_deep_map__");
|
|
115
|
+
return function () {
|
|
116
|
+
var args = [];
|
|
117
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
118
|
+
args[_i] = arguments[_i];
|
|
119
|
+
}
|
|
120
|
+
var returnedValue;
|
|
121
|
+
if (useDeepEqual) {
|
|
122
|
+
if (!this.hasOwnProperty(propDeepMapName)) {
|
|
123
|
+
Object.defineProperty(this, propDeepMapName, {
|
|
124
|
+
configurable: false,
|
|
125
|
+
enumerable: false,
|
|
126
|
+
writable: false,
|
|
127
|
+
value: new DeepEqualMap()
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
var myMap_1 = this[propDeepMapName];
|
|
131
|
+
if (Array.isArray(tags)) {
|
|
132
|
+
for (var _a = 0, tags_2 = tags; _a < tags_2.length; _a++) {
|
|
133
|
+
var tag = tags_2[_a];
|
|
134
|
+
var mapWrapper = {
|
|
135
|
+
clear: function () { return myMap_1.clear(); }
|
|
136
|
+
};
|
|
137
|
+
if (clearCacheTagsMap.has(tag)) {
|
|
138
|
+
clearCacheTagsMap.get(tag).push(mapWrapper);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
clearCacheTagsMap.set(tag, [mapWrapper]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var hashKey = void 0;
|
|
146
|
+
if (hashFunction === true) {
|
|
147
|
+
hashKey = args;
|
|
148
|
+
}
|
|
149
|
+
else if (hashFunction) {
|
|
150
|
+
hashKey = hashFunction.apply(this, args);
|
|
151
|
+
}
|
|
152
|
+
else if (args.length > 0) {
|
|
153
|
+
hashKey = args.length === 1 ? args[0] : args;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
hashKey = this;
|
|
157
|
+
}
|
|
158
|
+
var timestampKey = { __timestamp: true, key: hashKey };
|
|
159
|
+
var isExpired = false;
|
|
160
|
+
if (duration > 0) {
|
|
161
|
+
if (!myMap_1.has(timestampKey)) {
|
|
162
|
+
isExpired = true;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
var timestamp = myMap_1.get(timestampKey);
|
|
166
|
+
isExpired = (Date.now() - timestamp) > duration;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (myMap_1.has(hashKey) && !isExpired) {
|
|
170
|
+
returnedValue = myMap_1.get(hashKey);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
returnedValue = originalMethod.apply(this, args);
|
|
174
|
+
myMap_1.set(hashKey, returnedValue);
|
|
175
|
+
if (duration > 0) {
|
|
176
|
+
myMap_1.set(timestampKey, Date.now());
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
if (!this.hasOwnProperty(propMapName)) {
|
|
182
|
+
Object.defineProperty(this, propMapName, {
|
|
183
|
+
configurable: false,
|
|
184
|
+
enumerable: false,
|
|
185
|
+
writable: false,
|
|
186
|
+
value: new Map()
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
var myMap = this[propMapName];
|
|
190
|
+
if (Array.isArray(tags)) {
|
|
191
|
+
for (var _b = 0, tags_3 = tags; _b < tags_3.length; _b++) {
|
|
192
|
+
var tag = tags_3[_b];
|
|
193
|
+
if (clearCacheTagsMap.has(tag)) {
|
|
194
|
+
clearCacheTagsMap.get(tag).push(myMap);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
clearCacheTagsMap.set(tag, [myMap]);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (hashFunction || args.length > 0 || duration > 0) {
|
|
202
|
+
var hashKey = void 0;
|
|
203
|
+
if (hashFunction === true) {
|
|
204
|
+
hashKey = args.map(function (a) { return a.toString(); }).join('!');
|
|
205
|
+
}
|
|
206
|
+
else if (hashFunction) {
|
|
207
|
+
hashKey = hashFunction.apply(this, args);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
hashKey = args[0];
|
|
211
|
+
}
|
|
212
|
+
var timestampKey = hashKey + "__timestamp";
|
|
213
|
+
var isExpired = false;
|
|
214
|
+
if (duration > 0) {
|
|
215
|
+
if (!myMap.has(timestampKey)) {
|
|
216
|
+
isExpired = true;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
var timestamp = myMap.get(timestampKey);
|
|
220
|
+
isExpired = (Date.now() - timestamp) > duration;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (myMap.has(hashKey) && !isExpired) {
|
|
224
|
+
returnedValue = myMap.get(hashKey);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
returnedValue = originalMethod.apply(this, args);
|
|
228
|
+
myMap.set(hashKey, returnedValue);
|
|
229
|
+
if (duration > 0) {
|
|
230
|
+
myMap.set(timestampKey, Date.now());
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
var hashKey = this;
|
|
236
|
+
if (myMap.has(hashKey)) {
|
|
237
|
+
returnedValue = myMap.get(hashKey);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
returnedValue = originalMethod.apply(this, args);
|
|
241
|
+
myMap.set(hashKey, returnedValue);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return returnedValue;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
//# sourceMappingURL=memoize-decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoize-decorator.js","sourceRoot":"","sources":["../src/memoize-decorator.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,IAAM,K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oBAED,IAAI,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE;wBACrC,aAAa,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;qBACnC;yBAAM;wBACN,aAAa,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;wBACjD,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;wBAClC,IAAI,QAAQ,GAAG,CAAC,EAAE;4BACjB,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;yBACpC;qBACD;iBAED;qBAAM;oBACN,IAAM,OAAO,GAAG,IAAI,CAAC;oBACrB,IAAI,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;wBACvB,aAAa,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;qBACnC;yBAAM;wBACN,aAAa,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;wBACjD,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;qBAClC;iBACD;aACD;YAED,OAAO,aAAa,CAAC;QACtB,CAAC,CAAC;IACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blumintinc/typescript-memoize",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Memoize decorator for Typescript with deep equality",
|
|
5
|
+
"main": "./dist/memoize-decorator.js",
|
|
6
|
+
"module": "./dist/es2015/memoize-decorator.js",
|
|
7
|
+
"typings": "./dist/memoize-decorator.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "karma start test/karma.config.js --single-run",
|
|
10
|
+
"prepublish": "npm run build && npm run build:es2015",
|
|
11
|
+
"clean": "rm -rf ./dist",
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"build:es2015": "tsc --module es2015 --target es2015 --outDir dist/es2015",
|
|
14
|
+
"tslint": "tslint --project .",
|
|
15
|
+
"eslint": "npm run tslint"
|
|
16
|
+
},
|
|
17
|
+
"private": false,
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/BluMintInc/typescript-memoize.git"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"typescript",
|
|
31
|
+
"memoize",
|
|
32
|
+
"functional",
|
|
33
|
+
"decorator"
|
|
34
|
+
],
|
|
35
|
+
"author": "Joseph O'Connor",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/BluMintInc/typescript-memoize/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/BluMintInc/typescript-memoize#readme",
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/jasmine": "3.6.2",
|
|
43
|
+
"@types/node": "14.14.9",
|
|
44
|
+
"awesome-typescript-loader": "5.2.1",
|
|
45
|
+
"es6-shim": "0.35.6",
|
|
46
|
+
"jasmine-core": "3.6.0",
|
|
47
|
+
"karma": "6.3.16",
|
|
48
|
+
"karma-chrome-launcher": "3.1.0",
|
|
49
|
+
"karma-es6-shim": "1.0.0",
|
|
50
|
+
"karma-jasmine": "4.0.1",
|
|
51
|
+
"karma-jasmine-html-reporter": "1.5.4",
|
|
52
|
+
"karma-junit-reporter": "2.0.1",
|
|
53
|
+
"karma-sourcemap-loader": "0.3.8",
|
|
54
|
+
"karma-spec-reporter": "0.0.32",
|
|
55
|
+
"karma-threshold-reporter": "0.1.15",
|
|
56
|
+
"karma-typescript": "5.2.0",
|
|
57
|
+
"source-map-loader": "1.1.2",
|
|
58
|
+
"tslib": "2.0.3",
|
|
59
|
+
"tslint": "6.1.3",
|
|
60
|
+
"typescript": "4.1.2"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"fast-deep-equal": "^3.1.3"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
const equal = require('fast-deep-equal');
|
|
2
|
+
|
|
3
|
+
interface MemoizeArgs {
|
|
4
|
+
expiring?: number;
|
|
5
|
+
hashFunction?: boolean | ((...args: any[]) => any);
|
|
6
|
+
tags?: string[];
|
|
7
|
+
useDeepEqual?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function Memoize(args?: MemoizeArgs | MemoizeArgs['hashFunction']) {
|
|
11
|
+
let hashFunction: MemoizeArgs['hashFunction'];
|
|
12
|
+
let duration: MemoizeArgs['expiring'];
|
|
13
|
+
let tags: MemoizeArgs['tags'];
|
|
14
|
+
let useDeepEqual: MemoizeArgs['useDeepEqual'] = true;
|
|
15
|
+
|
|
16
|
+
if (typeof args === 'object') {
|
|
17
|
+
hashFunction = args.hashFunction;
|
|
18
|
+
duration = args.expiring;
|
|
19
|
+
tags = args.tags;
|
|
20
|
+
useDeepEqual = args.useDeepEqual ?? true;
|
|
21
|
+
} else {
|
|
22
|
+
hashFunction = args;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
|
|
26
|
+
if (descriptor.value != null) {
|
|
27
|
+
descriptor.value = getNewFunction(descriptor.value, hashFunction, duration, tags, useDeepEqual);
|
|
28
|
+
} else if (descriptor.get != null) {
|
|
29
|
+
descriptor.get = getNewFunction(descriptor.get, hashFunction, duration, tags, useDeepEqual);
|
|
30
|
+
} else {
|
|
31
|
+
throw 'Only put a Memoize() decorator on a method or get accessor.';
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function MemoizeExpiring(expiring: number, hashFunction?: MemoizeArgs['hashFunction']) {
|
|
37
|
+
return Memoize({
|
|
38
|
+
expiring,
|
|
39
|
+
hashFunction
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const clearCacheTagsMap: Map<string, Map<any, any>[]> = new Map();
|
|
44
|
+
|
|
45
|
+
export function clear (tags: string[]): number {
|
|
46
|
+
const cleared: Set<Map<any, any>> = new Set();
|
|
47
|
+
for (const tag of tags) {
|
|
48
|
+
const maps = clearCacheTagsMap.get(tag);
|
|
49
|
+
if (maps) {
|
|
50
|
+
for (const mp of maps) {
|
|
51
|
+
if (!cleared.has(mp)) {
|
|
52
|
+
mp.clear();
|
|
53
|
+
cleared.add(mp);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return cleared.size;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A wrapper around Map that uses deep equality for key comparison
|
|
62
|
+
class DeepEqualMap<K, V> {
|
|
63
|
+
private map = new Map<string, { key: K, value: V }>();
|
|
64
|
+
|
|
65
|
+
has(key: K): boolean {
|
|
66
|
+
const entries = Array.from(this.map.values());
|
|
67
|
+
for (const entry of entries) {
|
|
68
|
+
if (equal(entry.key, key)) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get(key: K): V | undefined {
|
|
76
|
+
const entries = Array.from(this.map.values());
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
if (equal(entry.key, key)) {
|
|
79
|
+
return entry.value;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
set(key: K, value: V): this {
|
|
86
|
+
const entries = Array.from(this.map.entries());
|
|
87
|
+
for (const [serializedKey, entry] of entries) {
|
|
88
|
+
if (equal(entry.key, key)) {
|
|
89
|
+
this.map.delete(serializedKey);
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const serializedKey = `${Date.now()}_${Math.random()}`;
|
|
95
|
+
this.map.set(serializedKey, { key, value });
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
clear(): void {
|
|
100
|
+
this.map.clear();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getNewFunction(
|
|
105
|
+
originalMethod: () => void,
|
|
106
|
+
hashFunction?: MemoizeArgs['hashFunction'],
|
|
107
|
+
duration: number = 0,
|
|
108
|
+
tags?: MemoizeArgs['tags'],
|
|
109
|
+
useDeepEqual: boolean = true
|
|
110
|
+
) {
|
|
111
|
+
const propMapName = Symbol(`__memoized_map__`);
|
|
112
|
+
const propDeepMapName = Symbol(`__memoized_deep_map__`);
|
|
113
|
+
|
|
114
|
+
// The function returned here gets called instead of originalMethod.
|
|
115
|
+
return function (...args: any[]) {
|
|
116
|
+
let returnedValue: any;
|
|
117
|
+
|
|
118
|
+
// Get or create appropriate map based on deep equality requirement
|
|
119
|
+
if (useDeepEqual) {
|
|
120
|
+
if (!this.hasOwnProperty(propDeepMapName)) {
|
|
121
|
+
Object.defineProperty(this, propDeepMapName, {
|
|
122
|
+
configurable: false,
|
|
123
|
+
enumerable: false,
|
|
124
|
+
writable: false,
|
|
125
|
+
value: new DeepEqualMap<any, any>()
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
let myMap: DeepEqualMap<any, any> = this[propDeepMapName];
|
|
129
|
+
|
|
130
|
+
if (Array.isArray(tags)) {
|
|
131
|
+
for (const tag of tags) {
|
|
132
|
+
// Since DeepEqualMap doesn't match the Map interface exactly,
|
|
133
|
+
// we wrap it in a Map for tag clearing purposes
|
|
134
|
+
const mapWrapper = {
|
|
135
|
+
clear: () => myMap.clear()
|
|
136
|
+
} as any;
|
|
137
|
+
|
|
138
|
+
if (clearCacheTagsMap.has(tag)) {
|
|
139
|
+
clearCacheTagsMap.get(tag).push(mapWrapper);
|
|
140
|
+
} else {
|
|
141
|
+
clearCacheTagsMap.set(tag, [mapWrapper]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let hashKey: any;
|
|
147
|
+
|
|
148
|
+
// If true is passed as first parameter, will automatically use every argument
|
|
149
|
+
if (hashFunction === true) {
|
|
150
|
+
hashKey = args;
|
|
151
|
+
} else if (hashFunction) {
|
|
152
|
+
hashKey = hashFunction.apply(this, args);
|
|
153
|
+
} else if (args.length > 0) {
|
|
154
|
+
hashKey = args.length === 1 ? args[0] : args;
|
|
155
|
+
} else {
|
|
156
|
+
hashKey = this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Handle expiration
|
|
160
|
+
const timestampKey = { __timestamp: true, key: hashKey };
|
|
161
|
+
let isExpired: boolean = false;
|
|
162
|
+
|
|
163
|
+
if (duration > 0) {
|
|
164
|
+
if (!myMap.has(timestampKey)) {
|
|
165
|
+
isExpired = true;
|
|
166
|
+
} else {
|
|
167
|
+
let timestamp = myMap.get(timestampKey);
|
|
168
|
+
isExpired = (Date.now() - timestamp) > duration;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (myMap.has(hashKey) && !isExpired) {
|
|
173
|
+
returnedValue = myMap.get(hashKey);
|
|
174
|
+
} else {
|
|
175
|
+
returnedValue = originalMethod.apply(this, args);
|
|
176
|
+
myMap.set(hashKey, returnedValue);
|
|
177
|
+
if (duration > 0) {
|
|
178
|
+
myMap.set(timestampKey, Date.now());
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
// Original implementation with standard Map (shallow equality)
|
|
183
|
+
if (!this.hasOwnProperty(propMapName)) {
|
|
184
|
+
Object.defineProperty(this, propMapName, {
|
|
185
|
+
configurable: false,
|
|
186
|
+
enumerable: false,
|
|
187
|
+
writable: false,
|
|
188
|
+
value: new Map<any, any>()
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
let myMap: Map<any, any> = this[propMapName];
|
|
192
|
+
|
|
193
|
+
if (Array.isArray(tags)) {
|
|
194
|
+
for (const tag of tags) {
|
|
195
|
+
if (clearCacheTagsMap.has(tag)) {
|
|
196
|
+
clearCacheTagsMap.get(tag).push(myMap);
|
|
197
|
+
} else {
|
|
198
|
+
clearCacheTagsMap.set(tag, [myMap]);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (hashFunction || args.length > 0 || duration > 0) {
|
|
204
|
+
let hashKey: any;
|
|
205
|
+
|
|
206
|
+
// If true is passed as first parameter, will automatically use every argument, passed to string
|
|
207
|
+
if (hashFunction === true) {
|
|
208
|
+
hashKey = args.map(a => a.toString()).join('!');
|
|
209
|
+
} else if (hashFunction) {
|
|
210
|
+
hashKey = hashFunction.apply(this, args);
|
|
211
|
+
} else {
|
|
212
|
+
hashKey = args[0];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const timestampKey = `${hashKey}__timestamp`;
|
|
216
|
+
let isExpired: boolean = false;
|
|
217
|
+
if (duration > 0) {
|
|
218
|
+
if (!myMap.has(timestampKey)) {
|
|
219
|
+
// "Expired" since it was never called before
|
|
220
|
+
isExpired = true;
|
|
221
|
+
} else {
|
|
222
|
+
let timestamp = myMap.get(timestampKey);
|
|
223
|
+
isExpired = (Date.now() - timestamp) > duration;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (myMap.has(hashKey) && !isExpired) {
|
|
228
|
+
returnedValue = myMap.get(hashKey);
|
|
229
|
+
} else {
|
|
230
|
+
returnedValue = originalMethod.apply(this, args);
|
|
231
|
+
myMap.set(hashKey, returnedValue);
|
|
232
|
+
if (duration > 0) {
|
|
233
|
+
myMap.set(timestampKey, Date.now());
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
} else {
|
|
238
|
+
const hashKey = this;
|
|
239
|
+
if (myMap.has(hashKey)) {
|
|
240
|
+
returnedValue = myMap.get(hashKey);
|
|
241
|
+
} else {
|
|
242
|
+
returnedValue = originalMethod.apply(this, args);
|
|
243
|
+
myMap.set(hashKey, returnedValue);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return returnedValue;
|
|
249
|
+
};
|
|
250
|
+
}
|