@naturalcycles/js-lib 14.227.0 → 14.228.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/timeout.decorator.js +2 -0
- package/dist/promise/pTimeout.d.ts +2 -0
- package/dist/promise/pTimeout.js +7 -0
- package/dist-esm/decorators/timeout.decorator.js +2 -0
- package/dist-esm/promise/pTimeout.js +7 -0
- package/package.json +1 -1
- package/src/decorators/timeout.decorator.ts +2 -0
- package/src/promise/pTimeout.ts +11 -0
|
@@ -8,6 +8,8 @@ const decorator_util_1 = require("./decorator.util");
|
|
|
8
8
|
function _Timeout(opt) {
|
|
9
9
|
return (target, key, descriptor) => {
|
|
10
10
|
(0, assert_1._assert)(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
|
|
11
|
+
if (!opt.timeout)
|
|
12
|
+
return descriptor;
|
|
11
13
|
const originalFn = descriptor.value;
|
|
12
14
|
const keyStr = String(key);
|
|
13
15
|
descriptor.value = async function (...args) {
|
|
@@ -4,6 +4,8 @@ import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
|
|
|
4
4
|
export interface PTimeoutOptions {
|
|
5
5
|
/**
|
|
6
6
|
* Timeout in milliseconds.
|
|
7
|
+
*
|
|
8
|
+
* If 0 is passed - the function would be executed right away, with no timeout.
|
|
7
9
|
*/
|
|
8
10
|
timeout: NumberOfMilliseconds;
|
|
9
11
|
/**
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -11,6 +11,9 @@ const error_util_1 = require("../error/error.util");
|
|
|
11
11
|
*/
|
|
12
12
|
function pTimeoutFn(fn, opt) {
|
|
13
13
|
opt.name ||= fn.name;
|
|
14
|
+
if (!opt.timeout) {
|
|
15
|
+
return fn;
|
|
16
|
+
}
|
|
14
17
|
return async function pTimeoutInternalFn(...args) {
|
|
15
18
|
return await pTimeout(() => fn.apply(this, args), opt);
|
|
16
19
|
};
|
|
@@ -23,6 +26,10 @@ exports.pTimeoutFn = pTimeoutFn;
|
|
|
23
26
|
* If the Function rejects - passes this rejection further.
|
|
24
27
|
*/
|
|
25
28
|
async function pTimeout(fn, opt) {
|
|
29
|
+
if (!opt.timeout) {
|
|
30
|
+
// short-circuit to direct execution if 0 timeout is passed
|
|
31
|
+
return await fn();
|
|
32
|
+
}
|
|
26
33
|
const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
|
|
27
34
|
const fakeError = opt.fakeError || new Error('TimeoutError');
|
|
28
35
|
// eslint-disable-next-line no-async-promise-executor
|
|
@@ -5,6 +5,8 @@ import { _getMethodSignature } from './decorator.util';
|
|
|
5
5
|
export function _Timeout(opt) {
|
|
6
6
|
return (target, key, descriptor) => {
|
|
7
7
|
_assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
|
|
8
|
+
if (!opt.timeout)
|
|
9
|
+
return descriptor;
|
|
8
10
|
const originalFn = descriptor.value;
|
|
9
11
|
const keyStr = String(key);
|
|
10
12
|
descriptor.value = async function (...args) {
|
|
@@ -8,6 +8,9 @@ import { _errorDataAppend, TimeoutError } from '../error/error.util';
|
|
|
8
8
|
*/
|
|
9
9
|
export function pTimeoutFn(fn, opt) {
|
|
10
10
|
opt.name || (opt.name = fn.name);
|
|
11
|
+
if (!opt.timeout) {
|
|
12
|
+
return fn;
|
|
13
|
+
}
|
|
11
14
|
return async function pTimeoutInternalFn(...args) {
|
|
12
15
|
return await pTimeout(() => fn.apply(this, args), opt);
|
|
13
16
|
};
|
|
@@ -19,6 +22,10 @@ export function pTimeoutFn(fn, opt) {
|
|
|
19
22
|
* If the Function rejects - passes this rejection further.
|
|
20
23
|
*/
|
|
21
24
|
export async function pTimeout(fn, opt) {
|
|
25
|
+
if (!opt.timeout) {
|
|
26
|
+
// short-circuit to direct execution if 0 timeout is passed
|
|
27
|
+
return await fn();
|
|
28
|
+
}
|
|
22
29
|
const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
|
|
23
30
|
const fakeError = opt.fakeError || new Error('TimeoutError');
|
|
24
31
|
// eslint-disable-next-line no-async-promise-executor
|
package/package.json
CHANGED
|
@@ -8,6 +8,8 @@ export function _Timeout(opt: PTimeoutOptions): MethodDecorator {
|
|
|
8
8
|
return (target, key, descriptor) => {
|
|
9
9
|
_assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods')
|
|
10
10
|
|
|
11
|
+
if (!opt.timeout) return descriptor
|
|
12
|
+
|
|
11
13
|
const originalFn = descriptor.value
|
|
12
14
|
const keyStr = String(key)
|
|
13
15
|
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types'
|
|
|
5
5
|
export interface PTimeoutOptions {
|
|
6
6
|
/**
|
|
7
7
|
* Timeout in milliseconds.
|
|
8
|
+
*
|
|
9
|
+
* If 0 is passed - the function would be executed right away, with no timeout.
|
|
8
10
|
*/
|
|
9
11
|
timeout: NumberOfMilliseconds
|
|
10
12
|
|
|
@@ -46,6 +48,10 @@ export interface PTimeoutOptions {
|
|
|
46
48
|
export function pTimeoutFn<T extends AnyAsyncFunction>(fn: T, opt: PTimeoutOptions): T {
|
|
47
49
|
opt.name ||= fn.name
|
|
48
50
|
|
|
51
|
+
if (!opt.timeout) {
|
|
52
|
+
return fn
|
|
53
|
+
}
|
|
54
|
+
|
|
49
55
|
return async function pTimeoutInternalFn(this: any, ...args: any[]) {
|
|
50
56
|
return await pTimeout(() => fn.apply(this, args), opt)
|
|
51
57
|
} as T
|
|
@@ -58,6 +64,11 @@ export function pTimeoutFn<T extends AnyAsyncFunction>(fn: T, opt: PTimeoutOptio
|
|
|
58
64
|
* If the Function rejects - passes this rejection further.
|
|
59
65
|
*/
|
|
60
66
|
export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions): Promise<T> {
|
|
67
|
+
if (!opt.timeout) {
|
|
68
|
+
// short-circuit to direct execution if 0 timeout is passed
|
|
69
|
+
return await fn()
|
|
70
|
+
}
|
|
71
|
+
|
|
61
72
|
const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt
|
|
62
73
|
const fakeError = opt.fakeError || new Error('TimeoutError')
|
|
63
74
|
|