xjs-common 11.0.1 → 11.0.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/README.md +3 -1
- package/build/const/types.d.ts +1 -1
- package/build/func/u.d.ts +10 -4
- package/build/func/u.js +11 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[![npm][npm-badge]][npm-url] [![CI][ci-badge]][ci-url]
|
|
1
|
+
[![npm][npm-badge]][npm-url] [![CI][ci-badge]][ci-url] [![publish][publish-badge]][publish-url]
|
|
2
2
|
|
|
3
3
|
# Overview
|
|
4
4
|
Library modules for typescript that bundled general-purpose implementations.
|
|
@@ -192,3 +192,5 @@ XJS throws error with `code` property which has one of the following numbers.
|
|
|
192
192
|
[npm-badge]: https://badgen.net/npm/v/xjs-common
|
|
193
193
|
[ci-url]: https://github.com/begyyal/xjs_commons/actions/workflows/test.yml
|
|
194
194
|
[ci-badge]: https://github.com/begyyal/xjs_commons/actions/workflows/test.yml/badge.svg
|
|
195
|
+
[publish-url]: https://github.com/begyyal/xjs_commons/actions/workflows/publish.yml
|
|
196
|
+
[publish-badge]: https://github.com/begyyal/xjs_commons/actions/workflows/publish.yml/badge.svg
|
package/build/const/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type IndexSignature = string | number | symbol;
|
|
2
|
-
export type NormalRecord = Record<IndexSignature,
|
|
2
|
+
export type NormalRecord<T = any> = Record<IndexSignature, T>;
|
|
3
3
|
export type MaybeArray<T> = T | T[];
|
|
4
4
|
export type Loggable = {
|
|
5
5
|
log: (msg: any) => void;
|
package/build/func/u.d.ts
CHANGED
|
@@ -10,15 +10,21 @@ export interface RetryOption<T = void | Promise<void>> {
|
|
|
10
10
|
errorCriterion?: (e: any) => boolean;
|
|
11
11
|
intervalPredicate?: () => T;
|
|
12
12
|
}
|
|
13
|
+
export interface SyncRetryOption extends RetryOption<void> {
|
|
14
|
+
}
|
|
15
|
+
export interface AsyncRetryOption extends RetryOption {
|
|
16
|
+
intervalSec?: number;
|
|
17
|
+
}
|
|
13
18
|
/**
|
|
14
19
|
* runs callback with customizable retry.
|
|
15
20
|
* @param cb callback to be retried.
|
|
16
21
|
* @param op.count number of retries. default is 1.
|
|
17
22
|
* @param op.logger logger used for exceptions while retrying the process. default is `console` object.
|
|
18
23
|
* @param op.errorCriterion distinguish whether retry is required form exceptions. default is none. (i.e. allways required.)
|
|
24
|
+
* @param op.intervalSec secounds to wait between callbacks. this wait occurs after `intervalPredicate`.
|
|
19
25
|
* @param op.intervalPredicate predicate that runs between callbacks when retrying.
|
|
20
26
|
*/
|
|
21
|
-
export declare function retry<T>(cb: () => T, op?:
|
|
22
|
-
export declare function retry<T>(cb: () => T, op?:
|
|
23
|
-
export declare function retry<T>(cb: () => Promise<T>, op?:
|
|
24
|
-
export declare function retry<T>(cb: () => Promise<T>, op?:
|
|
27
|
+
export declare function retry<T>(cb: () => T, op?: SyncRetryOption): T;
|
|
28
|
+
export declare function retry<T>(cb: () => T, op?: AsyncRetryOption): Promise<T>;
|
|
29
|
+
export declare function retry<T>(cb: () => Promise<T>, op?: SyncRetryOption): Promise<T>;
|
|
30
|
+
export declare function retry<T>(cb: () => Promise<T>, op?: AsyncRetryOption): Promise<T>;
|
package/build/func/u.js
CHANGED
|
@@ -27,6 +27,8 @@ export function bitor(...bit) {
|
|
|
27
27
|
return bit.reduce((a, b) => a | b);
|
|
28
28
|
}
|
|
29
29
|
;
|
|
30
|
+
;
|
|
31
|
+
;
|
|
30
32
|
export function retry(cb, op) {
|
|
31
33
|
const l = op?.logger ?? console;
|
|
32
34
|
const initialCount = op?.count ?? 1;
|
|
@@ -59,9 +61,15 @@ export function retry(cb, op) {
|
|
|
59
61
|
else
|
|
60
62
|
return ret;
|
|
61
63
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
const chain = (c) => ret instanceof Promise ? ret.then(() => c()) : c();
|
|
65
|
+
if (c < initialCount) {
|
|
66
|
+
if (op?.intervalPredicate)
|
|
67
|
+
ret = op?.intervalPredicate();
|
|
68
|
+
const intervalSec = op?.intervalSec;
|
|
69
|
+
if (intervalSec)
|
|
70
|
+
ret = chain(() => delay(intervalSec));
|
|
71
|
+
}
|
|
72
|
+
return chain(innerPrcs);
|
|
65
73
|
};
|
|
66
74
|
return prcs(initialCount);
|
|
67
75
|
}
|