@naturalcycles/js-lib 14.247.0 → 14.248.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/define.d.ts +2 -2
- package/dist/promise/pRetry.js +2 -2
- package/dist/types.d.ts +12 -0
- package/dist-esm/promise/pRetry.js +2 -2
- package/package.json +8 -4
- package/src/define.ts +2 -2
- package/src/promise/pRetry.ts +2 -2
- package/src/types.ts +12 -0
package/dist/define.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyFunction, AnyObject } from './types';
|
|
1
|
+
import type { AnyFunction, AnyObject, Lazy } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* const value = lazyValue(() => expensiveComputation())
|
|
4
4
|
*
|
|
@@ -8,7 +8,7 @@ import type { AnyFunction, AnyObject } from './types';
|
|
|
8
8
|
*
|
|
9
9
|
* Based on: https://github.com/sindresorhus/lazy-value
|
|
10
10
|
*/
|
|
11
|
-
export declare function _lazyValue<T
|
|
11
|
+
export declare function _lazyValue<T>(fn: () => T): Lazy<T>;
|
|
12
12
|
/**
|
|
13
13
|
* interface Obj {
|
|
14
14
|
* v: number
|
package/dist/promise/pRetry.js
CHANGED
|
@@ -15,7 +15,7 @@ function pRetryFn(fn, opt = {}) {
|
|
|
15
15
|
async function pRetry(fn, opt = {}) {
|
|
16
16
|
const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate, logger = console, name, timeout, } = opt;
|
|
17
17
|
const fakeError = timeout ? new Error('TimeoutError') : undefined;
|
|
18
|
-
let { logFirstAttempt = false, logRetries = true, logFailures =
|
|
18
|
+
let { logFirstAttempt = false, logRetries = true, logFailures = true, logSuccess = false } = opt;
|
|
19
19
|
if (opt.logAll) {
|
|
20
20
|
logSuccess = logFirstAttempt = logRetries = logFailures = true;
|
|
21
21
|
}
|
|
@@ -51,7 +51,7 @@ async function pRetry(fn, opt = {}) {
|
|
|
51
51
|
}
|
|
52
52
|
catch (err) {
|
|
53
53
|
if (logFailures) {
|
|
54
|
-
logger.
|
|
54
|
+
logger.error(`${fname} attempt #${attempt} error in ${(0, __1._since)(started)}:`, err);
|
|
55
55
|
}
|
|
56
56
|
if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
|
|
57
57
|
// Give up
|
package/dist/types.d.ts
CHANGED
|
@@ -71,6 +71,18 @@ export type AnyAsyncFunction<T = any> = (...args: any[]) => Promise<T>;
|
|
|
71
71
|
export type AsyncFunction<T = any> = () => Promise<T>;
|
|
72
72
|
export type AnyPromisableFunction<T = any> = (...args: any[]) => Promisable<T>;
|
|
73
73
|
export type PromisableFunction<T = any> = () => Promisable<T>;
|
|
74
|
+
/**
|
|
75
|
+
* A function that lazily calculates something.
|
|
76
|
+
*/
|
|
77
|
+
export type Lazy<T> = () => T;
|
|
78
|
+
/**
|
|
79
|
+
* A function that lazily calculates something async (returns a Promise).
|
|
80
|
+
*/
|
|
81
|
+
export type LazyPromise<T> = () => Promise<T>;
|
|
82
|
+
/**
|
|
83
|
+
* A function that lazily calculates something async, that can return null.
|
|
84
|
+
*/
|
|
85
|
+
export type LazyNullablePromise<T> = () => Promise<T | null>;
|
|
74
86
|
/**
|
|
75
87
|
* Symbol to indicate END of Sequence.
|
|
76
88
|
*/
|
|
@@ -11,7 +11,7 @@ export function pRetryFn(fn, opt = {}) {
|
|
|
11
11
|
export async function pRetry(fn, opt = {}) {
|
|
12
12
|
const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate, logger = console, name, timeout, } = opt;
|
|
13
13
|
const fakeError = timeout ? new Error('TimeoutError') : undefined;
|
|
14
|
-
let { logFirstAttempt = false, logRetries = true, logFailures =
|
|
14
|
+
let { logFirstAttempt = false, logRetries = true, logFailures = true, logSuccess = false } = opt;
|
|
15
15
|
if (opt.logAll) {
|
|
16
16
|
logSuccess = logFirstAttempt = logRetries = logFailures = true;
|
|
17
17
|
}
|
|
@@ -47,7 +47,7 @@ export async function pRetry(fn, opt = {}) {
|
|
|
47
47
|
}
|
|
48
48
|
catch (err) {
|
|
49
49
|
if (logFailures) {
|
|
50
|
-
logger.
|
|
50
|
+
logger.error(`${fname} attempt #${attempt} error in ${_since(started)}:`, err);
|
|
51
51
|
}
|
|
52
52
|
if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
|
|
53
53
|
// Give up
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.248.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky",
|
|
6
|
-
"build
|
|
6
|
+
"build": "dev-lib build-esm-cjs",
|
|
7
|
+
"test": "dev-lib test",
|
|
8
|
+
"lint": "dev-lib lint",
|
|
9
|
+
"bt": "dev-lib bt",
|
|
10
|
+
"lbt": "dev-lib lbt",
|
|
7
11
|
"test-tz1": "TZ=Europe/Stockholm yarn test local",
|
|
8
12
|
"test-tz2": "TZ=JST-9 yarn test local",
|
|
9
13
|
"test-ny": "TZ=GMT-0500 yarn test localTime",
|
|
@@ -17,11 +21,11 @@
|
|
|
17
21
|
},
|
|
18
22
|
"devDependencies": {
|
|
19
23
|
"@naturalcycles/bench-lib": "^3.0.0",
|
|
20
|
-
"@naturalcycles/dev-lib": "^
|
|
24
|
+
"@naturalcycles/dev-lib": "^15.0.3",
|
|
21
25
|
"@naturalcycles/nodejs-lib": "^13.0.1",
|
|
22
26
|
"@naturalcycles/time-lib": "^3.5.1",
|
|
23
27
|
"@types/crypto-js": "^4.1.1",
|
|
24
|
-
"@types/node": "^
|
|
28
|
+
"@types/node": "^22.0.0",
|
|
25
29
|
"@types/semver": "^7.5.8",
|
|
26
30
|
"crypto-js": "^4.1.1",
|
|
27
31
|
"jest": "^29.0.0",
|
package/src/define.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _mapObject, _mapValues } from './object/object.util'
|
|
2
|
-
import type { AnyFunction, AnyObject } from './types'
|
|
2
|
+
import type { AnyFunction, AnyObject, Lazy } from './types'
|
|
3
3
|
import { SKIP } from './types'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -11,7 +11,7 @@ import { SKIP } from './types'
|
|
|
11
11
|
*
|
|
12
12
|
* Based on: https://github.com/sindresorhus/lazy-value
|
|
13
13
|
*/
|
|
14
|
-
export function _lazyValue<T
|
|
14
|
+
export function _lazyValue<T>(fn: () => T): Lazy<T> {
|
|
15
15
|
let isCalled = false
|
|
16
16
|
let result: any
|
|
17
17
|
|
package/src/promise/pRetry.ts
CHANGED
|
@@ -114,7 +114,7 @@ export async function pRetry<T>(
|
|
|
114
114
|
} = opt
|
|
115
115
|
|
|
116
116
|
const fakeError = timeout ? new Error('TimeoutError') : undefined
|
|
117
|
-
let { logFirstAttempt = false, logRetries = true, logFailures =
|
|
117
|
+
let { logFirstAttempt = false, logRetries = true, logFailures = true, logSuccess = false } = opt
|
|
118
118
|
|
|
119
119
|
if (opt.logAll) {
|
|
120
120
|
logSuccess = logFirstAttempt = logRetries = logFailures = true
|
|
@@ -157,7 +157,7 @@ export async function pRetry<T>(
|
|
|
157
157
|
return result
|
|
158
158
|
} catch (err) {
|
|
159
159
|
if (logFailures) {
|
|
160
|
-
logger.
|
|
160
|
+
logger.error(`${fname} attempt #${attempt} error in ${_since(started)}:`, err)
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
if (attempt >= maxAttempts || (predicate && !predicate(err as Error, attempt, maxAttempts))) {
|
package/src/types.ts
CHANGED
|
@@ -90,6 +90,18 @@ export type AnyAsyncFunction<T = any> = (...args: any[]) => Promise<T>
|
|
|
90
90
|
export type AsyncFunction<T = any> = () => Promise<T>
|
|
91
91
|
export type AnyPromisableFunction<T = any> = (...args: any[]) => Promisable<T>
|
|
92
92
|
export type PromisableFunction<T = any> = () => Promisable<T>
|
|
93
|
+
/**
|
|
94
|
+
* A function that lazily calculates something.
|
|
95
|
+
*/
|
|
96
|
+
export type Lazy<T> = () => T
|
|
97
|
+
/**
|
|
98
|
+
* A function that lazily calculates something async (returns a Promise).
|
|
99
|
+
*/
|
|
100
|
+
export type LazyPromise<T> = () => Promise<T>
|
|
101
|
+
/**
|
|
102
|
+
* A function that lazily calculates something async, that can return null.
|
|
103
|
+
*/
|
|
104
|
+
export type LazyNullablePromise<T> = () => Promise<T | null>
|
|
93
105
|
|
|
94
106
|
/**
|
|
95
107
|
* Symbol to indicate END of Sequence.
|