@naturalcycles/js-lib 14.84.1 → 14.84.2
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/dist/decorators/asyncMemo.decorator.js +10 -13
- package/dist/decorators/memo.decorator.d.ts +0 -4
- package/dist/decorators/memo.decorator.js +6 -11
- package/dist-esm/decorators/asyncMemo.decorator.js +10 -13
- package/dist-esm/decorators/memo.decorator.js +5 -11
- package/package.json +1 -1
- package/src/decorators/asyncMemo.decorator.ts +10 -14
- package/src/decorators/memo.decorator.ts +5 -12
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports._AsyncMemo = void 0;
|
|
4
4
|
const time_util_1 = require("../time/time.util");
|
|
5
5
|
const decorator_util_1 = require("./decorator.util");
|
|
6
|
-
const memo_decorator_1 = require("./memo.decorator");
|
|
7
6
|
const memo_util_1 = require("./memo.util");
|
|
8
7
|
/**
|
|
9
8
|
* Like @_Memo, but allowing async MemoCache implementation.
|
|
@@ -30,18 +29,6 @@ const _AsyncMemo = (opt) => (target, key, descriptor) => {
|
|
|
30
29
|
// here, no need to check the cache. It's definitely a miss, because the cacheLayers is just created
|
|
31
30
|
// UPD: no! AsyncMemo supports "persistent caches" (e.g Database-backed cache)
|
|
32
31
|
}
|
|
33
|
-
if (args.length === 1 && args[0] === memo_decorator_1.CACHE_DROP) {
|
|
34
|
-
// Special event - CACHE_DROP
|
|
35
|
-
// Function will return undefined
|
|
36
|
-
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`);
|
|
37
|
-
try {
|
|
38
|
-
await Promise.all(cache.get(ctx).map(c => c.clear()));
|
|
39
|
-
}
|
|
40
|
-
catch (err) {
|
|
41
|
-
logger.error(err);
|
|
42
|
-
}
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
32
|
let value;
|
|
46
33
|
try {
|
|
47
34
|
for await (const cacheLayer of cache.get(ctx)) {
|
|
@@ -91,6 +78,16 @@ const _AsyncMemo = (opt) => (target, key, descriptor) => {
|
|
|
91
78
|
}
|
|
92
79
|
}
|
|
93
80
|
};
|
|
81
|
+
descriptor.value.dropCache = async () => {
|
|
82
|
+
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`);
|
|
83
|
+
try {
|
|
84
|
+
await Promise.all([...cache.values()].flatMap(c => c.map(c => c.clear())));
|
|
85
|
+
cache.clear();
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
logger.error(err);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
94
91
|
return descriptor;
|
|
95
92
|
};
|
|
96
93
|
exports._AsyncMemo = _AsyncMemo;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { CommonLogger } from '../log/commonLogger';
|
|
2
2
|
import { MemoCache } from './memo.util';
|
|
3
|
-
/**
|
|
4
|
-
* Symbol to indicate that the Cache should be dropped.
|
|
5
|
-
*/
|
|
6
|
-
export declare const CACHE_DROP: unique symbol;
|
|
7
3
|
export interface MemoOptions {
|
|
8
4
|
/**
|
|
9
5
|
* Default to false
|
|
@@ -5,14 +5,10 @@
|
|
|
5
5
|
// http://inlehmansterms.net/2015/03/01/javascript-memoization/
|
|
6
6
|
// https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports._Memo =
|
|
8
|
+
exports._Memo = void 0;
|
|
9
9
|
const time_util_1 = require("../time/time.util");
|
|
10
10
|
const decorator_util_1 = require("./decorator.util");
|
|
11
11
|
const memo_util_1 = require("./memo.util");
|
|
12
|
-
/**
|
|
13
|
-
* Symbol to indicate that the Cache should be dropped.
|
|
14
|
-
*/
|
|
15
|
-
exports.CACHE_DROP = Symbol('CACHE_DROP');
|
|
16
12
|
/**
|
|
17
13
|
* Memoizes the method of the class, so it caches the output and returns the cached version if the "key"
|
|
18
14
|
* of the cache is the same. Key, by defaul, is calculated as `JSON.stringify(...args)`.
|
|
@@ -43,12 +39,6 @@ const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
43
39
|
const methodSignature = (0, decorator_util_1._getTargetMethodSignature)(target, keyStr);
|
|
44
40
|
descriptor.value = function (...args) {
|
|
45
41
|
const ctx = this;
|
|
46
|
-
if (args.length === 1 && args[0] === exports.CACHE_DROP) {
|
|
47
|
-
// Special event - CACHE_DROP
|
|
48
|
-
// Function will return undefined
|
|
49
|
-
logger.log(`${methodSignature} @_Memo.CACHE_DROP`);
|
|
50
|
-
return cache.get(ctx)?.clear();
|
|
51
|
-
}
|
|
52
42
|
const cacheKey = cacheKeyFn(args);
|
|
53
43
|
if (!cache.has(ctx)) {
|
|
54
44
|
cache.set(ctx, cacheFactory());
|
|
@@ -101,6 +91,11 @@ const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
101
91
|
return res;
|
|
102
92
|
}
|
|
103
93
|
};
|
|
94
|
+
descriptor.value.dropCache = () => {
|
|
95
|
+
logger.log(`${methodSignature} @_Memo.dropCache()`);
|
|
96
|
+
cache.forEach(memoCache => memoCache.clear());
|
|
97
|
+
cache.clear();
|
|
98
|
+
};
|
|
104
99
|
return descriptor;
|
|
105
100
|
};
|
|
106
101
|
exports._Memo = _Memo;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { __asyncValues } from "tslib";
|
|
2
2
|
import { _since } from '../time/time.util';
|
|
3
3
|
import { _getArgsSignature, _getMethodSignature, _getTargetMethodSignature } from './decorator.util';
|
|
4
|
-
import { CACHE_DROP } from './memo.decorator';
|
|
5
4
|
import { jsonMemoSerializer } from './memo.util';
|
|
6
5
|
/**
|
|
7
6
|
* Like @_Memo, but allowing async MemoCache implementation.
|
|
@@ -29,18 +28,6 @@ export const _AsyncMemo = (opt) => (target, key, descriptor) => {
|
|
|
29
28
|
// here, no need to check the cache. It's definitely a miss, because the cacheLayers is just created
|
|
30
29
|
// UPD: no! AsyncMemo supports "persistent caches" (e.g Database-backed cache)
|
|
31
30
|
}
|
|
32
|
-
if (args.length === 1 && args[0] === CACHE_DROP) {
|
|
33
|
-
// Special event - CACHE_DROP
|
|
34
|
-
// Function will return undefined
|
|
35
|
-
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`);
|
|
36
|
-
try {
|
|
37
|
-
await Promise.all(cache.get(ctx).map(c => c.clear()));
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
logger.error(err);
|
|
41
|
-
}
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
31
|
let value;
|
|
45
32
|
try {
|
|
46
33
|
try {
|
|
@@ -100,5 +87,15 @@ export const _AsyncMemo = (opt) => (target, key, descriptor) => {
|
|
|
100
87
|
}
|
|
101
88
|
}
|
|
102
89
|
};
|
|
90
|
+
descriptor.value.dropCache = async () => {
|
|
91
|
+
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`);
|
|
92
|
+
try {
|
|
93
|
+
await Promise.all([...cache.values()].flatMap(c => c.map(c => c.clear())));
|
|
94
|
+
cache.clear();
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
logger.error(err);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
103
100
|
return descriptor;
|
|
104
101
|
};
|
|
@@ -6,10 +6,6 @@
|
|
|
6
6
|
import { _since } from '../time/time.util';
|
|
7
7
|
import { _getArgsSignature, _getMethodSignature, _getTargetMethodSignature } from './decorator.util';
|
|
8
8
|
import { jsonMemoSerializer, MapMemoCache } from './memo.util';
|
|
9
|
-
/**
|
|
10
|
-
* Symbol to indicate that the Cache should be dropped.
|
|
11
|
-
*/
|
|
12
|
-
export const CACHE_DROP = Symbol('CACHE_DROP');
|
|
13
9
|
/**
|
|
14
10
|
* Memoizes the method of the class, so it caches the output and returns the cached version if the "key"
|
|
15
11
|
* of the cache is the same. Key, by defaul, is calculated as `JSON.stringify(...args)`.
|
|
@@ -39,14 +35,7 @@ export const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
39
35
|
const keyStr = String(key);
|
|
40
36
|
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
41
37
|
descriptor.value = function (...args) {
|
|
42
|
-
var _a;
|
|
43
38
|
const ctx = this;
|
|
44
|
-
if (args.length === 1 && args[0] === CACHE_DROP) {
|
|
45
|
-
// Special event - CACHE_DROP
|
|
46
|
-
// Function will return undefined
|
|
47
|
-
logger.log(`${methodSignature} @_Memo.CACHE_DROP`);
|
|
48
|
-
return (_a = cache.get(ctx)) === null || _a === void 0 ? void 0 : _a.clear();
|
|
49
|
-
}
|
|
50
39
|
const cacheKey = cacheKeyFn(args);
|
|
51
40
|
if (!cache.has(ctx)) {
|
|
52
41
|
cache.set(ctx, cacheFactory());
|
|
@@ -99,5 +88,10 @@ export const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
99
88
|
return res;
|
|
100
89
|
}
|
|
101
90
|
};
|
|
91
|
+
descriptor.value.dropCache = () => {
|
|
92
|
+
logger.log(`${methodSignature} @_Memo.dropCache()`);
|
|
93
|
+
cache.forEach(memoCache => memoCache.clear());
|
|
94
|
+
cache.clear();
|
|
95
|
+
};
|
|
102
96
|
return descriptor;
|
|
103
97
|
};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import { _since } from '../time/time.util'
|
|
|
2
2
|
import { Merge } from '../typeFest'
|
|
3
3
|
import { AnyObject } from '../types'
|
|
4
4
|
import { _getArgsSignature, _getMethodSignature, _getTargetMethodSignature } from './decorator.util'
|
|
5
|
-
import {
|
|
5
|
+
import { MemoOptions } from './memo.decorator'
|
|
6
6
|
import { AsyncMemoCache, jsonMemoSerializer } from './memo.util'
|
|
7
7
|
|
|
8
8
|
export type AsyncMemoOptions = Merge<
|
|
@@ -65,19 +65,6 @@ export const _AsyncMemo =
|
|
|
65
65
|
// UPD: no! AsyncMemo supports "persistent caches" (e.g Database-backed cache)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
if (args.length === 1 && args[0] === CACHE_DROP) {
|
|
69
|
-
// Special event - CACHE_DROP
|
|
70
|
-
// Function will return undefined
|
|
71
|
-
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`)
|
|
72
|
-
try {
|
|
73
|
-
await Promise.all(cache.get(ctx)!.map(c => c.clear()))
|
|
74
|
-
} catch (err) {
|
|
75
|
-
logger.error(err)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return
|
|
79
|
-
}
|
|
80
|
-
|
|
81
68
|
let value: any
|
|
82
69
|
|
|
83
70
|
try {
|
|
@@ -146,6 +133,15 @@ export const _AsyncMemo =
|
|
|
146
133
|
}
|
|
147
134
|
}
|
|
148
135
|
} as any
|
|
136
|
+
;(descriptor.value as any).dropCache = async () => {
|
|
137
|
+
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`)
|
|
138
|
+
try {
|
|
139
|
+
await Promise.all([...cache.values()].flatMap(c => c.map(c => c.clear())))
|
|
140
|
+
cache.clear()
|
|
141
|
+
} catch (err) {
|
|
142
|
+
logger.error(err)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
149
145
|
|
|
150
146
|
return descriptor
|
|
151
147
|
}
|
|
@@ -10,11 +10,6 @@ import { AnyObject } from '../types'
|
|
|
10
10
|
import { _getArgsSignature, _getMethodSignature, _getTargetMethodSignature } from './decorator.util'
|
|
11
11
|
import { jsonMemoSerializer, MapMemoCache, MemoCache } from './memo.util'
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Symbol to indicate that the Cache should be dropped.
|
|
15
|
-
*/
|
|
16
|
-
export const CACHE_DROP = Symbol('CACHE_DROP')
|
|
17
|
-
|
|
18
13
|
export interface MemoOptions {
|
|
19
14
|
/**
|
|
20
15
|
* Default to false
|
|
@@ -111,13 +106,6 @@ export const _Memo =
|
|
|
111
106
|
descriptor.value = function (this: typeof target, ...args: any[]): any {
|
|
112
107
|
const ctx = this
|
|
113
108
|
|
|
114
|
-
if (args.length === 1 && args[0] === CACHE_DROP) {
|
|
115
|
-
// Special event - CACHE_DROP
|
|
116
|
-
// Function will return undefined
|
|
117
|
-
logger.log(`${methodSignature} @_Memo.CACHE_DROP`)
|
|
118
|
-
return cache.get(ctx)?.clear()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
109
|
const cacheKey = cacheKeyFn(args)
|
|
122
110
|
|
|
123
111
|
if (!cache.has(ctx)) {
|
|
@@ -195,6 +183,11 @@ export const _Memo =
|
|
|
195
183
|
return res
|
|
196
184
|
}
|
|
197
185
|
} as any
|
|
186
|
+
;(descriptor.value as any).dropCache = () => {
|
|
187
|
+
logger.log(`${methodSignature} @_Memo.dropCache()`)
|
|
188
|
+
cache.forEach(memoCache => memoCache.clear())
|
|
189
|
+
cache.clear()
|
|
190
|
+
}
|
|
198
191
|
|
|
199
192
|
return descriptor
|
|
200
193
|
}
|