@naturalcycles/js-lib 14.83.0 → 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/decorators/memo.util.d.ts +5 -10
- package/dist/decorators/memo.util.js +1 -16
- package/dist/error/assert.d.ts +4 -4
- package/dist/error/error.model.d.ts +1 -0
- package/dist/promise/pRetry.d.ts +5 -1
- package/dist/promise/pRetry.js +6 -1
- package/dist/promise/pTimeout.d.ts +5 -0
- package/dist/promise/pTimeout.js +5 -1
- package/dist-esm/decorators/asyncMemo.decorator.js +10 -13
- package/dist-esm/decorators/memo.decorator.js +5 -11
- package/dist-esm/decorators/memo.util.js +0 -14
- package/dist-esm/promise/pRetry.js +3 -1
- package/dist-esm/promise/pTimeout.js +2 -1
- package/package.json +1 -1
- package/src/decorators/asyncMemo.decorator.ts +10 -14
- package/src/decorators/memo.decorator.ts +5 -12
- package/src/decorators/memo.util.ts +7 -20
- package/src/error/assert.ts +4 -4
- package/src/error/error.model.ts +2 -0
- package/src/promise/pRetry.ts +12 -2
- package/src/promise/pTimeout.ts +14 -1
|
@@ -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,3 +1,4 @@
|
|
|
1
|
+
import { Promisable } from '../typeFest';
|
|
1
2
|
export declare type MemoSerializer = (args: any[]) => any;
|
|
2
3
|
export declare const jsonMemoSerializer: MemoSerializer;
|
|
3
4
|
export interface MemoCache<KEY = any, VALUE = any> {
|
|
@@ -16,24 +17,18 @@ export interface AsyncMemoCache<KEY = any, VALUE = any> {
|
|
|
16
17
|
* This also means that you CANNOT store `undefined` value in the Cache, as it'll be treated as a MISS.
|
|
17
18
|
* You CAN store `null` value instead, it will be treated as a HIT.
|
|
18
19
|
*/
|
|
19
|
-
get(k: KEY):
|
|
20
|
-
set(k: KEY, v: VALUE):
|
|
20
|
+
get(k: KEY): Promisable<VALUE | undefined>;
|
|
21
|
+
set(k: KEY, v: VALUE): Promisable<void>;
|
|
21
22
|
/**
|
|
22
23
|
* Clear is only called when `.dropCache()` is called.
|
|
23
24
|
* Otherwise the Cache is "persistent" (never cleared).
|
|
24
25
|
*/
|
|
25
|
-
clear():
|
|
26
|
+
clear(): Promisable<void>;
|
|
26
27
|
}
|
|
27
|
-
export declare class MapMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALUE> {
|
|
28
|
+
export declare class MapMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALUE>, AsyncMemoCache<KEY, VALUE> {
|
|
28
29
|
private m;
|
|
29
30
|
has(k: KEY): boolean;
|
|
30
31
|
get(k: KEY): VALUE | undefined;
|
|
31
32
|
set(k: KEY, v: VALUE): void;
|
|
32
33
|
clear(): void;
|
|
33
34
|
}
|
|
34
|
-
export declare class MapAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache<KEY, VALUE> {
|
|
35
|
-
private m;
|
|
36
|
-
get(k: KEY): Promise<VALUE | undefined>;
|
|
37
|
-
set(k: KEY, v: VALUE): Promise<void>;
|
|
38
|
-
clear(): Promise<void>;
|
|
39
|
-
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.MapMemoCache = exports.jsonMemoSerializer = void 0;
|
|
4
4
|
const object_util_1 = require("../object/object.util");
|
|
5
5
|
const jsonMemoSerializer = args => {
|
|
6
6
|
if (args.length === 0)
|
|
@@ -73,18 +73,3 @@ class MapMemoCache {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
exports.MapMemoCache = MapMemoCache;
|
|
76
|
-
class MapAsyncMemoCache {
|
|
77
|
-
constructor() {
|
|
78
|
-
this.m = new Map();
|
|
79
|
-
}
|
|
80
|
-
async get(k) {
|
|
81
|
-
return this.m.get(k);
|
|
82
|
-
}
|
|
83
|
-
async set(k, v) {
|
|
84
|
-
this.m.set(k, v);
|
|
85
|
-
}
|
|
86
|
-
async clear() {
|
|
87
|
-
this.m.clear();
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.MapAsyncMemoCache = MapAsyncMemoCache;
|
package/dist/error/assert.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorData
|
|
1
|
+
import { ErrorData } from '..';
|
|
2
2
|
import { AppError } from './app.error';
|
|
3
3
|
/**
|
|
4
4
|
* Evaluates the `condition` (casts it to Boolean).
|
|
@@ -16,21 +16,21 @@ import { AppError } from './app.error';
|
|
|
16
16
|
* 3. Sets `userFriendly` flag to true, cause it's always better to have at least SOME clue, rather than fully generic "Oops" error.
|
|
17
17
|
*/
|
|
18
18
|
export declare function _assert(condition: any, // will be evaluated as Boolean
|
|
19
|
-
message?: string, errorData?:
|
|
19
|
+
message?: string, errorData?: ErrorData): asserts condition;
|
|
20
20
|
/**
|
|
21
21
|
* Like _assert(), but prints more helpful error message.
|
|
22
22
|
* API is similar to Node's assert.equals().
|
|
23
23
|
*
|
|
24
24
|
* Does SHALLOW, but strict equality (===), use _assertDeepEquals() for deep equality.
|
|
25
25
|
*/
|
|
26
|
-
export declare function _assertEquals<T>(actual: any, expected: T, message?: string, errorData?:
|
|
26
|
+
export declare function _assertEquals<T>(actual: any, expected: T, message?: string, errorData?: ErrorData): asserts actual is T;
|
|
27
27
|
/**
|
|
28
28
|
* Like _assert(), but prints more helpful error message.
|
|
29
29
|
* API is similar to Node's assert.deepEquals().
|
|
30
30
|
*
|
|
31
31
|
* Does DEEP equality via _deepEquals()
|
|
32
32
|
*/
|
|
33
|
-
export declare function _assertDeepEquals<T>(actual: any, expected: T, message?: string, errorData?:
|
|
33
|
+
export declare function _assertDeepEquals<T>(actual: any, expected: T, message?: string, errorData?: ErrorData): asserts actual is T;
|
|
34
34
|
export declare function _assertIsError<ERR extends Error = Error>(err: any, message?: string): asserts err is ERR;
|
|
35
35
|
export declare function _assertIsString(v: any, message?: string): asserts v is string;
|
|
36
36
|
export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
|
package/dist/promise/pRetry.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyFunction, CommonLogger } from '..';
|
|
1
|
+
import { AnyFunction, CommonLogger, ErrorData } from '..';
|
|
2
2
|
export interface PRetryOptions {
|
|
3
3
|
/**
|
|
4
4
|
* If set - will be included in the error message.
|
|
@@ -76,6 +76,10 @@ export interface PRetryOptions {
|
|
|
76
76
|
* @experimental
|
|
77
77
|
*/
|
|
78
78
|
keepStackTrace?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Will be merged with `err.data` object.
|
|
81
|
+
*/
|
|
82
|
+
errorData?: ErrorData;
|
|
79
83
|
}
|
|
80
84
|
/**
|
|
81
85
|
* Returns a Function (!), enhanced with retry capabilities.
|
package/dist/promise/pRetry.js
CHANGED
|
@@ -31,7 +31,7 @@ async function pRetry(fn, opt = {}) {
|
|
|
31
31
|
return await new Promise((resolve, reject) => {
|
|
32
32
|
const rejectWithTimeout = () => {
|
|
33
33
|
timedOut = true; // to prevent more tries
|
|
34
|
-
const err = new pTimeout_1.TimeoutError(`"${fname}" timed out after ${timeout} ms
|
|
34
|
+
const err = new pTimeout_1.TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData);
|
|
35
35
|
if (fakeError) {
|
|
36
36
|
// keep original stack
|
|
37
37
|
err.stack = fakeError.stack.replace('Error: RetryError', 'TimeoutError');
|
|
@@ -75,6 +75,11 @@ async function pRetry(fn, opt = {}) {
|
|
|
75
75
|
fakeError.stack.replace('Error: RetryError', ''),
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
+
;
|
|
79
|
+
err.data = {
|
|
80
|
+
...err.data,
|
|
81
|
+
...opt.errorData,
|
|
82
|
+
};
|
|
78
83
|
reject(err);
|
|
79
84
|
}
|
|
80
85
|
else {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppError } from '../error/app.error';
|
|
2
|
+
import { ErrorData } from '../error/error.model';
|
|
2
3
|
import { AnyFunction } from '../types';
|
|
3
4
|
export declare class TimeoutError extends AppError {
|
|
4
5
|
}
|
|
@@ -25,6 +26,10 @@ export interface PTimeoutOptions {
|
|
|
25
26
|
* @experimental
|
|
26
27
|
*/
|
|
27
28
|
keepStackTrace?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Will be merged with `err.data` object.
|
|
31
|
+
*/
|
|
32
|
+
errorData?: ErrorData;
|
|
28
33
|
}
|
|
29
34
|
/**
|
|
30
35
|
* Decorates a Function with a timeout.
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -37,11 +37,15 @@ async function pTimeout(promise, opt) {
|
|
|
37
37
|
catch (err) {
|
|
38
38
|
if (fakeError)
|
|
39
39
|
err.stack = fakeError.stack; // keep original stack
|
|
40
|
+
err.data = {
|
|
41
|
+
...err.data,
|
|
42
|
+
...opt.errorData,
|
|
43
|
+
};
|
|
40
44
|
reject(err);
|
|
41
45
|
}
|
|
42
46
|
return;
|
|
43
47
|
}
|
|
44
|
-
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms
|
|
48
|
+
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`, opt.errorData);
|
|
45
49
|
if (fakeError)
|
|
46
50
|
err.stack = fakeError.stack; // keep original stack
|
|
47
51
|
reject(err);
|
|
@@ -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
|
};
|
|
@@ -68,17 +68,3 @@ export class MapMemoCache {
|
|
|
68
68
|
this.m.clear();
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
export class MapAsyncMemoCache {
|
|
72
|
-
constructor() {
|
|
73
|
-
this.m = new Map();
|
|
74
|
-
}
|
|
75
|
-
async get(k) {
|
|
76
|
-
return this.m.get(k);
|
|
77
|
-
}
|
|
78
|
-
async set(k, v) {
|
|
79
|
-
this.m.set(k, v);
|
|
80
|
-
}
|
|
81
|
-
async clear() {
|
|
82
|
-
this.m.clear();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
@@ -27,7 +27,7 @@ export async function pRetry(fn, opt = {}) {
|
|
|
27
27
|
return await new Promise((resolve, reject) => {
|
|
28
28
|
const rejectWithTimeout = () => {
|
|
29
29
|
timedOut = true; // to prevent more tries
|
|
30
|
-
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms
|
|
30
|
+
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData);
|
|
31
31
|
if (fakeError) {
|
|
32
32
|
// keep original stack
|
|
33
33
|
err.stack = fakeError.stack.replace('Error: RetryError', 'TimeoutError');
|
|
@@ -71,6 +71,8 @@ export async function pRetry(fn, opt = {}) {
|
|
|
71
71
|
fakeError.stack.replace('Error: RetryError', ''),
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
|
+
;
|
|
75
|
+
err.data = Object.assign(Object.assign({}, err.data), opt.errorData);
|
|
74
76
|
reject(err);
|
|
75
77
|
}
|
|
76
78
|
else {
|
|
@@ -32,11 +32,12 @@ export async function pTimeout(promise, opt) {
|
|
|
32
32
|
catch (err) {
|
|
33
33
|
if (fakeError)
|
|
34
34
|
err.stack = fakeError.stack; // keep original stack
|
|
35
|
+
err.data = Object.assign(Object.assign({}, err.data), opt.errorData);
|
|
35
36
|
reject(err);
|
|
36
37
|
}
|
|
37
38
|
return;
|
|
38
39
|
}
|
|
39
|
-
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms
|
|
40
|
+
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`, opt.errorData);
|
|
40
41
|
if (fakeError)
|
|
41
42
|
err.stack = fakeError.stack; // keep original stack
|
|
42
43
|
reject(err);
|
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
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _isPrimitive } from '../object/object.util'
|
|
2
|
+
import { Promisable } from '../typeFest'
|
|
2
3
|
|
|
3
4
|
export type MemoSerializer = (args: any[]) => any
|
|
4
5
|
|
|
@@ -28,14 +29,14 @@ export interface AsyncMemoCache<KEY = any, VALUE = any> {
|
|
|
28
29
|
* This also means that you CANNOT store `undefined` value in the Cache, as it'll be treated as a MISS.
|
|
29
30
|
* You CAN store `null` value instead, it will be treated as a HIT.
|
|
30
31
|
*/
|
|
31
|
-
get(k: KEY):
|
|
32
|
-
set(k: KEY, v: VALUE):
|
|
32
|
+
get(k: KEY): Promisable<VALUE | undefined>
|
|
33
|
+
set(k: KEY, v: VALUE): Promisable<void>
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* Clear is only called when `.dropCache()` is called.
|
|
36
37
|
* Otherwise the Cache is "persistent" (never cleared).
|
|
37
38
|
*/
|
|
38
|
-
clear():
|
|
39
|
+
clear(): Promisable<void>
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
// SingleValueMemoCache and ObjectMemoCache are example-only, not used in production code
|
|
@@ -84,7 +85,9 @@ export class ObjectMemoCache implements MemoCache {
|
|
|
84
85
|
}
|
|
85
86
|
*/
|
|
86
87
|
|
|
87
|
-
export class MapMemoCache<KEY = any, VALUE = any>
|
|
88
|
+
export class MapMemoCache<KEY = any, VALUE = any>
|
|
89
|
+
implements MemoCache<KEY, VALUE>, AsyncMemoCache<KEY, VALUE>
|
|
90
|
+
{
|
|
88
91
|
private m = new Map<KEY, VALUE>()
|
|
89
92
|
|
|
90
93
|
has(k: KEY): boolean {
|
|
@@ -103,19 +106,3 @@ export class MapMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALU
|
|
|
103
106
|
this.m.clear()
|
|
104
107
|
}
|
|
105
108
|
}
|
|
106
|
-
|
|
107
|
-
export class MapAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache<KEY, VALUE> {
|
|
108
|
-
private m = new Map<KEY, VALUE>()
|
|
109
|
-
|
|
110
|
-
async get(k: KEY): Promise<VALUE | undefined> {
|
|
111
|
-
return this.m.get(k)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async set(k: KEY, v: VALUE): Promise<void> {
|
|
115
|
-
this.m.set(k, v)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async clear(): Promise<void> {
|
|
119
|
-
this.m.clear()
|
|
120
|
-
}
|
|
121
|
-
}
|
package/src/error/assert.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorData,
|
|
1
|
+
import { ErrorData, _deepEquals, _stringifyAny } from '..'
|
|
2
2
|
import { AppError } from './app.error'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -19,7 +19,7 @@ import { AppError } from './app.error'
|
|
|
19
19
|
export function _assert(
|
|
20
20
|
condition: any, // will be evaluated as Boolean
|
|
21
21
|
message?: string,
|
|
22
|
-
errorData?:
|
|
22
|
+
errorData?: ErrorData,
|
|
23
23
|
): asserts condition {
|
|
24
24
|
if (!condition) {
|
|
25
25
|
throw new AssertionError(message || 'see stacktrace', {
|
|
@@ -39,7 +39,7 @@ export function _assertEquals<T>(
|
|
|
39
39
|
actual: any,
|
|
40
40
|
expected: T,
|
|
41
41
|
message?: string,
|
|
42
|
-
errorData?:
|
|
42
|
+
errorData?: ErrorData,
|
|
43
43
|
): asserts actual is T {
|
|
44
44
|
if (actual !== expected) {
|
|
45
45
|
const msg = [
|
|
@@ -67,7 +67,7 @@ export function _assertDeepEquals<T>(
|
|
|
67
67
|
actual: any,
|
|
68
68
|
expected: T,
|
|
69
69
|
message?: string,
|
|
70
|
-
errorData?:
|
|
70
|
+
errorData?: ErrorData,
|
|
71
71
|
): asserts actual is T {
|
|
72
72
|
if (!_deepEquals(actual, expected)) {
|
|
73
73
|
const msg = [
|
package/src/error/error.model.ts
CHANGED
package/src/promise/pRetry.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _since, _stringifyAny, AnyFunction, CommonLogger } from '..'
|
|
1
|
+
import { _since, _stringifyAny, AnyFunction, AppError, CommonLogger, ErrorData } from '..'
|
|
2
2
|
import { TimeoutError } from './pTimeout'
|
|
3
3
|
|
|
4
4
|
export interface PRetryOptions {
|
|
@@ -91,6 +91,11 @@ export interface PRetryOptions {
|
|
|
91
91
|
* @experimental
|
|
92
92
|
*/
|
|
93
93
|
keepStackTrace?: boolean
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Will be merged with `err.data` object.
|
|
97
|
+
*/
|
|
98
|
+
errorData?: ErrorData
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
/**
|
|
@@ -139,7 +144,7 @@ export async function pRetry<T>(
|
|
|
139
144
|
return await new Promise((resolve, reject) => {
|
|
140
145
|
const rejectWithTimeout = () => {
|
|
141
146
|
timedOut = true // to prevent more tries
|
|
142
|
-
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms
|
|
147
|
+
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData)
|
|
143
148
|
if (fakeError) {
|
|
144
149
|
// keep original stack
|
|
145
150
|
err.stack = fakeError.stack!.replace('Error: RetryError', 'TimeoutError')
|
|
@@ -199,6 +204,11 @@ export async function pRetry<T>(
|
|
|
199
204
|
})
|
|
200
205
|
}
|
|
201
206
|
|
|
207
|
+
;(err as AppError).data = {
|
|
208
|
+
...(err as AppError).data,
|
|
209
|
+
...opt.errorData,
|
|
210
|
+
}
|
|
211
|
+
|
|
202
212
|
reject(err)
|
|
203
213
|
} else {
|
|
204
214
|
// Retry after delay
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppError } from '../error/app.error'
|
|
2
|
+
import { ErrorData } from '../error/error.model'
|
|
2
3
|
import { AnyFunction } from '../types'
|
|
3
4
|
|
|
4
5
|
export class TimeoutError extends AppError {}
|
|
@@ -29,6 +30,11 @@ export interface PTimeoutOptions {
|
|
|
29
30
|
* @experimental
|
|
30
31
|
*/
|
|
31
32
|
keepStackTrace?: boolean
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Will be merged with `err.data` object.
|
|
36
|
+
*/
|
|
37
|
+
errorData?: ErrorData
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
/**
|
|
@@ -63,12 +69,19 @@ export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Pr
|
|
|
63
69
|
resolve(onTimeout())
|
|
64
70
|
} catch (err: any) {
|
|
65
71
|
if (fakeError) err.stack = fakeError.stack // keep original stack
|
|
72
|
+
err.data = {
|
|
73
|
+
...err.data,
|
|
74
|
+
...opt.errorData,
|
|
75
|
+
}
|
|
66
76
|
reject(err)
|
|
67
77
|
}
|
|
68
78
|
return
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
const err = new TimeoutError(
|
|
81
|
+
const err = new TimeoutError(
|
|
82
|
+
`"${name || 'pTimeout function'}" timed out after ${timeout} ms`,
|
|
83
|
+
opt.errorData,
|
|
84
|
+
)
|
|
72
85
|
if (fakeError) err.stack = fakeError.stack // keep original stack
|
|
73
86
|
reject(err)
|
|
74
87
|
}, timeout)
|