@naturalcycles/js-lib 14.84.1 → 14.86.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/dist/decorators/asyncMemo.decorator.d.ts +35 -9
- package/dist/decorators/asyncMemo.decorator.js +39 -34
- package/dist/decorators/decorator.util.d.ts +1 -1
- package/dist/decorators/decorator.util.js +2 -2
- package/dist/decorators/logMethod.decorator.d.ts +6 -4
- package/dist/decorators/logMethod.decorator.js +3 -3
- package/dist/decorators/memo.decorator.d.ts +29 -29
- package/dist/decorators/memo.decorator.js +42 -53
- package/dist/decorators/memo.util.d.ts +6 -6
- package/dist/decorators/memoFn.d.ts +5 -0
- package/dist/decorators/memoFn.js +32 -37
- package/dist/decorators/memoFnAsync.d.ts +10 -0
- package/dist/decorators/memoFnAsync.js +69 -0
- package/dist/decorators/memoSimple.decorator.d.ts +1 -1
- package/dist/decorators/memoSimple.decorator.js +3 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -0
- package/dist/types.d.ts +4 -0
- package/dist-esm/decorators/asyncMemo.decorator.js +40 -47
- package/dist-esm/decorators/decorator.util.js +2 -2
- package/dist-esm/decorators/logMethod.decorator.js +3 -3
- package/dist-esm/decorators/memo.decorator.js +41 -53
- package/dist-esm/decorators/memoFn.js +32 -37
- package/dist-esm/decorators/memoFnAsync.js +65 -0
- package/dist-esm/decorators/memoSimple.decorator.js +3 -3
- package/dist-esm/index.js +1 -0
- package/package.json +1 -1
- package/src/decorators/asyncMemo.decorator.ts +88 -61
- package/src/decorators/decorator.util.ts +2 -2
- package/src/decorators/logMethod.decorator.ts +16 -7
- package/src/decorators/memo.decorator.ts +66 -102
- package/src/decorators/memo.util.ts +7 -7
- package/src/decorators/memoFn.ts +33 -51
- package/src/decorators/memoFnAsync.ts +91 -0
- package/src/decorators/memoSimple.decorator.ts +4 -4
- package/src/index.ts +3 -0
- package/src/types.ts +5 -0
package/src/decorators/memoFn.ts
CHANGED
|
@@ -7,6 +7,11 @@ export interface MemoizedFunction {
|
|
|
7
7
|
cache: MemoCache
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Only supports Sync functions.
|
|
12
|
+
* To support Async functions - use _memoFnAsync.
|
|
13
|
+
* Technically, you can use it with Async functions, but it'll return the Promise without awaiting it.
|
|
14
|
+
*/
|
|
10
15
|
export function _memoFn<T extends (...args: any[]) => any>(
|
|
11
16
|
fn: T,
|
|
12
17
|
opt: MemoOptions = {},
|
|
@@ -14,86 +19,63 @@ export function _memoFn<T extends (...args: any[]) => any>(
|
|
|
14
19
|
const {
|
|
15
20
|
logHit = false,
|
|
16
21
|
logMiss = false,
|
|
17
|
-
|
|
22
|
+
logArgs = true,
|
|
18
23
|
logger = console,
|
|
19
|
-
|
|
20
|
-
noCacheResolved = false,
|
|
24
|
+
cacheErrors = true,
|
|
21
25
|
cacheFactory = () => new MapMemoCache(),
|
|
22
26
|
cacheKeyFn = jsonMemoSerializer,
|
|
23
27
|
} = opt
|
|
24
28
|
|
|
25
29
|
const cache = cacheFactory()
|
|
26
|
-
const awaitPromise = Boolean(noCacheRejected || noCacheResolved)
|
|
27
30
|
const fnName = fn.name
|
|
28
31
|
|
|
29
32
|
const memoizedFn = function (this: any, ...args: any[]): T {
|
|
30
33
|
const ctx = this
|
|
31
34
|
const cacheKey = cacheKeyFn(args)
|
|
35
|
+
let value: any
|
|
32
36
|
|
|
33
37
|
if (cache.has(cacheKey)) {
|
|
34
38
|
if (logHit) {
|
|
35
|
-
logger.log(`${fnName}(${_getArgsSignature(args,
|
|
39
|
+
logger.log(`${fnName}(${_getArgsSignature(args, logArgs)}) memoFn hit`)
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
value = cache.get(cacheKey)
|
|
39
43
|
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
} else {
|
|
43
|
-
return res
|
|
44
|
+
if (value instanceof Error) {
|
|
45
|
+
throw value
|
|
44
46
|
}
|
|
47
|
+
|
|
48
|
+
return value
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
const started = Date.now()
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (awaitPromise) {
|
|
52
|
-
return (res as Promise<any>)
|
|
53
|
-
.then(res => {
|
|
54
|
-
// console.log('RESOLVED', res)
|
|
55
|
-
if (logMiss) {
|
|
56
|
-
logger.log(
|
|
57
|
-
`${fnName}(${_getArgsSignature(args, noLogArgs)}) memoFn miss resolved (${_since(
|
|
58
|
-
started,
|
|
59
|
-
)})`,
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (!noCacheResolved) {
|
|
64
|
-
cache.set(cacheKey, res)
|
|
65
|
-
}
|
|
53
|
+
try {
|
|
54
|
+
value = fn.apply(ctx, args)
|
|
66
55
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
logger.log(
|
|
73
|
-
`${fnName}(${_getArgsSignature(args, noLogArgs)}) memoFn miss rejected (${_since(
|
|
74
|
-
started,
|
|
75
|
-
)})`,
|
|
76
|
-
)
|
|
77
|
-
}
|
|
56
|
+
try {
|
|
57
|
+
cache.set(cacheKey, value)
|
|
58
|
+
} catch (err) {
|
|
59
|
+
logger.error(err)
|
|
60
|
+
}
|
|
78
61
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
62
|
+
return value
|
|
63
|
+
} catch (err) {
|
|
64
|
+
if (cacheErrors) {
|
|
65
|
+
try {
|
|
66
|
+
cache.set(cacheKey, err)
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger.error(err)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
85
71
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
} else {
|
|
72
|
+
throw err
|
|
73
|
+
} finally {
|
|
89
74
|
if (logMiss) {
|
|
90
75
|
logger.log(
|
|
91
|
-
`${fnName}(${_getArgsSignature(args,
|
|
76
|
+
`${fnName}(${_getArgsSignature(args, logArgs)}) memoFn miss (${_since(started)})`,
|
|
92
77
|
)
|
|
93
78
|
}
|
|
94
|
-
|
|
95
|
-
cache.set(cacheKey, res)
|
|
96
|
-
return res
|
|
97
79
|
}
|
|
98
80
|
}
|
|
99
81
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { _since } from '../time/time.util'
|
|
2
|
+
import { AsyncMemoOptions } from './asyncMemo.decorator'
|
|
3
|
+
import { _getArgsSignature } from './decorator.util'
|
|
4
|
+
import { AsyncMemoCache, jsonMemoSerializer, MapMemoCache } from './memo.util'
|
|
5
|
+
|
|
6
|
+
export interface MemoizedAsyncFunction {
|
|
7
|
+
cache: AsyncMemoCache
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Only supports Sync functions.
|
|
12
|
+
* To support Async functions - use _memoFnAsync
|
|
13
|
+
*/
|
|
14
|
+
export function _memoFnAsync<T extends (...args: any[]) => Promise<any>>(
|
|
15
|
+
fn: T,
|
|
16
|
+
opt: AsyncMemoOptions = {},
|
|
17
|
+
): T & MemoizedAsyncFunction {
|
|
18
|
+
const {
|
|
19
|
+
logHit = false,
|
|
20
|
+
logMiss = false,
|
|
21
|
+
logArgs = true,
|
|
22
|
+
logger = console,
|
|
23
|
+
cacheRejections = true,
|
|
24
|
+
cacheFactory = () => new MapMemoCache(),
|
|
25
|
+
cacheKeyFn = jsonMemoSerializer,
|
|
26
|
+
} = opt
|
|
27
|
+
|
|
28
|
+
const cache = cacheFactory()
|
|
29
|
+
const fnName = fn.name
|
|
30
|
+
|
|
31
|
+
const memoizedFn = async function (this: any, ...args: any[]): Promise<any> {
|
|
32
|
+
const ctx = this
|
|
33
|
+
const cacheKey = cacheKeyFn(args)
|
|
34
|
+
let value: any
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
value = await cache.get(cacheKey)
|
|
38
|
+
} catch (err) {
|
|
39
|
+
logger.error(err)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (value !== undefined) {
|
|
43
|
+
if (logHit) {
|
|
44
|
+
logger.log(`${fnName}(${_getArgsSignature(args, logArgs)}) memoFnAsync hit`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (value instanceof Error) {
|
|
48
|
+
throw value
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return value
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const started = Date.now()
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
value = await fn.apply(ctx, args)
|
|
58
|
+
|
|
59
|
+
void (async () => {
|
|
60
|
+
try {
|
|
61
|
+
await cache.set(cacheKey, value)
|
|
62
|
+
} catch (err) {
|
|
63
|
+
logger.error(err)
|
|
64
|
+
}
|
|
65
|
+
})()
|
|
66
|
+
|
|
67
|
+
return value
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (cacheRejections) {
|
|
70
|
+
void (async () => {
|
|
71
|
+
try {
|
|
72
|
+
await cache.set(cacheKey, err)
|
|
73
|
+
} catch (err) {
|
|
74
|
+
logger.error(err)
|
|
75
|
+
}
|
|
76
|
+
})()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
throw err
|
|
80
|
+
} finally {
|
|
81
|
+
if (logMiss) {
|
|
82
|
+
logger.log(
|
|
83
|
+
`${fnName}(${_getArgsSignature(args, logArgs)}) memoFnAsync miss (${_since(started)})`,
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Object.assign(memoizedFn, { cache })
|
|
90
|
+
return memoizedFn as T & MemoizedAsyncFunction
|
|
91
|
+
}
|
|
@@ -18,7 +18,7 @@ import { jsonMemoSerializer, MapMemoCache, MemoCache } from './memo.util'
|
|
|
18
18
|
export interface MemoOpts {
|
|
19
19
|
logHit?: boolean
|
|
20
20
|
logMiss?: boolean
|
|
21
|
-
|
|
21
|
+
logArgs?: boolean
|
|
22
22
|
logger?: CommonLogger
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -57,7 +57,7 @@ export const memoSimple =
|
|
|
57
57
|
*/
|
|
58
58
|
const cache: MemoCache = new MapMemoCache()
|
|
59
59
|
|
|
60
|
-
const { logHit, logMiss,
|
|
60
|
+
const { logHit, logMiss, logArgs = true, logger = console } = opt
|
|
61
61
|
const keyStr = String(key)
|
|
62
62
|
const methodSignature = _getTargetMethodSignature(target, keyStr)
|
|
63
63
|
|
|
@@ -67,7 +67,7 @@ export const memoSimple =
|
|
|
67
67
|
|
|
68
68
|
if (cache.has(cacheKey)) {
|
|
69
69
|
if (logHit) {
|
|
70
|
-
logger.log(`${methodSignature}(${_getArgsSignature(args,
|
|
70
|
+
logger.log(`${methodSignature}(${_getArgsSignature(args, logArgs)}) @memo hit`)
|
|
71
71
|
}
|
|
72
72
|
return cache.get(cacheKey)
|
|
73
73
|
}
|
|
@@ -78,7 +78,7 @@ export const memoSimple =
|
|
|
78
78
|
|
|
79
79
|
if (logMiss) {
|
|
80
80
|
logger.log(
|
|
81
|
-
`${methodSignature}(${_getArgsSignature(args,
|
|
81
|
+
`${methodSignature}(${_getArgsSignature(args, logArgs)}) @memo miss (${
|
|
82
82
|
Date.now() - d
|
|
83
83
|
} ms)`,
|
|
84
84
|
)
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ export * from './decorators/memo.decorator'
|
|
|
15
15
|
export * from './decorators/asyncMemo.decorator'
|
|
16
16
|
import { MemoCache, AsyncMemoCache } from './decorators/memo.util'
|
|
17
17
|
export * from './decorators/memoFn'
|
|
18
|
+
export * from './decorators/memoFnAsync'
|
|
18
19
|
export * from './decorators/retry.decorator'
|
|
19
20
|
export * from './decorators/timeout.decorator'
|
|
20
21
|
export * from './error/app.error'
|
|
@@ -118,6 +119,7 @@ import {
|
|
|
118
119
|
SavedDBEntity,
|
|
119
120
|
StringMap,
|
|
120
121
|
UnixTimestamp,
|
|
122
|
+
Integer,
|
|
121
123
|
ValueOf,
|
|
122
124
|
ValuesOf,
|
|
123
125
|
AbortableMapper,
|
|
@@ -203,6 +205,7 @@ export type {
|
|
|
203
205
|
ConditionalExcept,
|
|
204
206
|
Class,
|
|
205
207
|
UnixTimestamp,
|
|
208
|
+
Integer,
|
|
206
209
|
BaseDBEntity,
|
|
207
210
|
SavedDBEntity,
|
|
208
211
|
Saved,
|
package/src/types.ts
CHANGED
|
@@ -166,6 +166,11 @@ export type IsoDateTime = string
|
|
|
166
166
|
*/
|
|
167
167
|
export type UnixTimestamp = number
|
|
168
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Same as `number`, but with semantic meaning that it's an Integer.
|
|
171
|
+
*/
|
|
172
|
+
export type Integer = number
|
|
173
|
+
|
|
169
174
|
/**
|
|
170
175
|
* Base interface for any Entity that was saved to DB.
|
|
171
176
|
*/
|