@daiso-tech/core 0.34.0 → 0.35.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/README.md +2 -4
- package/dist/async/_module-exports.d.ts +0 -1
- package/dist/async/_module-exports.js +0 -1
- package/dist/async/_module-exports.js.map +1 -1
- package/dist/async/async.errors.d.ts +19 -12
- package/dist/async/async.errors.js +27 -16
- package/dist/async/async.errors.js.map +1 -1
- package/dist/async/middlewares/_module.d.ts +3 -0
- package/dist/async/middlewares/_module.js +3 -0
- package/dist/async/middlewares/_module.js.map +1 -1
- package/dist/async/middlewares/_shared.d.ts +10 -0
- package/dist/async/middlewares/_shared.js +5 -0
- package/dist/async/middlewares/_shared.js.map +1 -0
- package/dist/async/middlewares/bulkhead/_module.d.ts +1 -0
- package/dist/async/middlewares/bulkhead/_module.js +2 -0
- package/dist/async/middlewares/bulkhead/_module.js.map +1 -0
- package/dist/async/middlewares/bulkhead/bulkhead.middleware.d.ts +80 -0
- package/dist/async/middlewares/bulkhead/bulkhead.middleware.js +46 -0
- package/dist/async/middlewares/bulkhead/bulkhead.middleware.js.map +1 -0
- package/dist/async/middlewares/dynamic/dynamic.middleware.d.ts +2 -2
- package/dist/async/middlewares/dynamic/dynamic.middleware.js +5 -5
- package/dist/async/middlewares/dynamic/dynamic.middleware.js.map +1 -1
- package/dist/async/middlewares/fallback/fallback.middleware.d.ts +10 -8
- package/dist/async/middlewares/fallback/fallback.middleware.js +1 -1
- package/dist/async/middlewares/fallback/fallback.middleware.js.map +1 -1
- package/dist/async/middlewares/hedging/_module.d.ts +2 -0
- package/dist/async/middlewares/hedging/_module.js +3 -0
- package/dist/async/middlewares/hedging/_module.js.map +1 -0
- package/dist/async/middlewares/hedging/_shared.d.ts +93 -0
- package/dist/async/middlewares/hedging/_shared.js +5 -0
- package/dist/async/middlewares/hedging/_shared.js.map +1 -0
- package/dist/async/middlewares/hedging/concurrent-hedging/_module.d.ts +1 -0
- package/dist/async/middlewares/hedging/concurrent-hedging/_module.js +2 -0
- package/dist/async/middlewares/hedging/concurrent-hedging/_module.js.map +1 -0
- package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.d.ts +51 -0
- package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.js +138 -0
- package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.js.map +1 -0
- package/dist/async/middlewares/hedging/sequential-hedging/_module.d.ts +1 -0
- package/dist/async/middlewares/hedging/sequential-hedging/_module.js +2 -0
- package/dist/async/middlewares/hedging/sequential-hedging/_module.js.map +1 -0
- package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.d.ts +51 -0
- package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.js +104 -0
- package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.js.map +1 -0
- package/dist/async/middlewares/observe/observe.middleware.d.ts +14 -16
- package/dist/async/middlewares/observe/observe.middleware.js +1 -1
- package/dist/async/middlewares/observe/observe.middleware.js.map +1 -1
- package/dist/async/middlewares/retry/retry.middleware.d.ts +41 -36
- package/dist/async/middlewares/retry/retry.middleware.js +31 -28
- package/dist/async/middlewares/retry/retry.middleware.js.map +1 -1
- package/dist/async/middlewares/timeout/timeout.middleware.d.ts +34 -39
- package/dist/async/middlewares/timeout/timeout.middleware.js +30 -47
- package/dist/async/middlewares/timeout/timeout.middleware.js.map +1 -1
- package/dist/async/utilities/_module.d.ts +2 -0
- package/dist/async/utilities/_module.js +2 -0
- package/dist/async/utilities/_module.js.map +1 -1
- package/dist/async/utilities/abort-and-fail/abort-and-fail.d.ts +3 -0
- package/dist/async/utilities/abort-and-fail/abort-and-fail.js +6 -7
- package/dist/async/utilities/abort-and-fail/abort-and-fail.js.map +1 -1
- package/dist/async/utilities/lazy-promise/lazy-promise.js +0 -1
- package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
- package/dist/async/utilities/promise-queue/_module.d.ts +1 -0
- package/dist/async/utilities/promise-queue/_module.js +2 -0
- package/dist/async/utilities/promise-queue/_module.js.map +1 -0
- package/dist/async/utilities/promise-queue/promise-queue.d.ts +32 -0
- package/dist/async/utilities/promise-queue/promise-queue.js +107 -0
- package/dist/async/utilities/promise-queue/promise-queue.js.map +1 -0
- package/dist/async/utilities/timeout-and-fail/_module.d.ts +1 -0
- package/dist/async/utilities/timeout-and-fail/_module.js +2 -0
- package/dist/async/utilities/timeout-and-fail/_module.js.map +1 -0
- package/dist/async/utilities/timeout-and-fail/timeout-and-fail.d.ts +7 -0
- package/dist/async/utilities/timeout-and-fail/timeout-and-fail.js +19 -0
- package/dist/async/utilities/timeout-and-fail/timeout-and-fail.js.map +1 -0
- package/dist/cache/contracts/cache.contract.d.ts +2 -2
- package/dist/cache/contracts/cache.events.d.ts +43 -47
- package/dist/cache/contracts/cache.events.js +5 -103
- package/dist/cache/contracts/cache.events.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.d.ts +15 -15
- package/dist/cache/implementations/derivables/cache/cache.js +94 -79
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +1 -1
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache.test-suite.js +138 -147
- package/dist/cache/implementations/test-utilities/cache.test-suite.js.map +1 -1
- package/dist/event-bus/contracts/_module-exports.d.ts +0 -1
- package/dist/event-bus/contracts/_module-exports.js +0 -1
- package/dist/event-bus/contracts/_module-exports.js.map +1 -1
- package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +6 -1
- package/dist/event-bus/contracts/event-bus-factory.contract.d.ts +2 -3
- package/dist/event-bus/contracts/event-bus-factory.contract.js.map +1 -1
- package/dist/event-bus/contracts/event-bus.contract.d.ts +12 -23
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +10 -10
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +30 -30
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +13 -7
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js +11 -5
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.d.ts +0 -2
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +13 -20
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.d.ts +0 -2
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +187 -207
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/lock-provider.contract.d.ts +2 -2
- package/dist/lock/contracts/lock.events.d.ts +35 -44
- package/dist/lock/contracts/lock.events.js +8 -90
- package/dist/lock/contracts/lock.events.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +9 -9
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +12 -12
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +2 -2
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +2 -2
- package/dist/lock/implementations/derivables/lock-provider/lock.js +69 -50
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +212 -212
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/serde/contracts/flexible-serde.contract.d.ts +0 -11
- package/dist/serde/implementations/derivables/serde.d.ts +1 -32
- package/dist/serde/implementations/derivables/serde.js +0 -45
- package/dist/serde/implementations/derivables/serde.js.map +1 -1
- package/dist/utilities/classes/hooks/async-hooks.d.ts +107 -20
- package/dist/utilities/classes/hooks/async-hooks.js +47 -18
- package/dist/utilities/classes/hooks/async-hooks.js.map +1 -1
- package/dist/utilities/classes/hooks/hooks.d.ts +39 -21
- package/dist/utilities/classes/hooks/hooks.js +19 -18
- package/dist/utilities/classes/hooks/hooks.js.map +1 -1
- package/dist/utilities/classes/key-prefixer/key-prefixer.js +2 -2
- package/dist/utilities/classes/key-prefixer/key-prefixer.js.map +1 -1
- package/dist/utilities/contracts/_module.d.ts +0 -1
- package/dist/utilities/contracts/_module.js +0 -1
- package/dist/utilities/contracts/_module.js.map +1 -1
- package/dist/utilities/functions/invokable.d.ts +9 -5
- package/dist/utilities/functions/invokable.js +10 -0
- package/dist/utilities/functions/invokable.js.map +1 -1
- package/package.json +22 -2
- package/dist/async/async.events.d.ts +0 -105
- package/dist/async/async.events.js +0 -82
- package/dist/async/async.events.js.map +0 -1
- package/dist/event-bus/contracts/_shared.d.ts +0 -12
- package/dist/event-bus/contracts/_shared.js +0 -15
- package/dist/event-bus/contracts/_shared.js.map +0 -1
- package/dist/test.d.ts +0 -1
- package/dist/test.js +0 -34
- package/dist/test.js.map +0 -1
- package/dist/utilities/contracts/sync-event-bus-listenable.d.ts +0 -13
- package/dist/utilities/contracts/sync-event-bus-listenable.js +0 -5
- package/dist/utilities/contracts/sync-event-bus-listenable.js.map +0 -1
|
@@ -3,35 +3,43 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { callInvokable, TimeSpan, } from "../../../utilities/_module-exports.js";
|
|
5
5
|
import { exponentialBackoffPolicy, } from "../../../async/backof-policies/_module.js";
|
|
6
|
-
import {
|
|
6
|
+
import { RetryAsyncError } from "../../../async/async.errors.js";
|
|
7
7
|
import { LazyPromise } from "../../../async/utilities/_module.js";
|
|
8
|
+
import {} from "../../../async/middlewares/_shared.js";
|
|
8
9
|
/**
|
|
9
10
|
* The `retry` middleware enables automatic retries for all errors or specific errors, with configurable backoff policies.
|
|
10
11
|
* An error will be thrown when all retry attempts fail.
|
|
11
12
|
*
|
|
12
13
|
* IMPORT_PATH: `"@daiso-tech/core/async"`
|
|
13
14
|
* @group Middleware
|
|
14
|
-
*
|
|
15
|
-
* @throws {AbortAsyncError} {@link AbortAsyncError}
|
|
15
|
+
* @throws {RetryAsyncError} {@link RetryAsyncError}
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
19
|
* import { retry } from "@daiso-tech/core/async";
|
|
20
|
-
* import { AsyncHooks } from "@daiso-tech/core/utilities";
|
|
20
|
+
* import { AsyncHooks, TimeSpan } from "@daiso-tech/core/utilities";
|
|
21
21
|
*
|
|
22
|
-
* await new AsyncHooks(
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
22
|
+
* const data = await new AsyncHooks(
|
|
23
|
+
* async (url: string, signal?: AbortSignal): Promise<unknown> => {
|
|
24
|
+
* const response = await fetch(url, { signal });
|
|
25
|
+
* return await response.json();
|
|
26
|
+
* },
|
|
27
|
+
* [retry()],
|
|
28
|
+
* {
|
|
29
|
+
* signalBinder: {
|
|
30
|
+
* getSignal: (args) => args[1],
|
|
31
|
+
* forwardSignal: (args, signal) => {
|
|
32
|
+
* args[1] = signal;
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
27
35
|
* }
|
|
28
|
-
*
|
|
29
|
-
*
|
|
36
|
+
* )
|
|
37
|
+
* .invoke("URL");
|
|
30
38
|
* ```
|
|
31
39
|
*/
|
|
32
40
|
export function retry(settings = {}) {
|
|
33
|
-
const { maxAttempts = 4, backoffPolicy = exponentialBackoffPolicy(), retryPolicy = () => true,
|
|
34
|
-
return async (args, next, context) => {
|
|
41
|
+
const { maxAttempts = 4, backoffPolicy = exponentialBackoffPolicy(), retryPolicy = () => true, onRetryDelay = () => { }, onExecutionAttempt = () => { }, } = settings;
|
|
42
|
+
return async (args, next, { context, signal }) => {
|
|
35
43
|
const errors = [];
|
|
36
44
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
37
45
|
try {
|
|
@@ -39,29 +47,24 @@ export function retry(settings = {}) {
|
|
|
39
47
|
return await next(...args);
|
|
40
48
|
}
|
|
41
49
|
catch (error) {
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
if (signal.aborted) {
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
const waitTime = TimeSpan.fromMilliseconds(callInvokable(backoffPolicy, attempt, error));
|
|
54
|
+
callInvokable(onRetryDelay, {
|
|
44
55
|
error,
|
|
45
|
-
waitTime
|
|
56
|
+
waitTime,
|
|
46
57
|
attempt,
|
|
47
58
|
args,
|
|
48
59
|
context,
|
|
49
60
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw error;
|
|
61
|
+
if (callInvokable(retryPolicy, error)) {
|
|
62
|
+
errors.push(error);
|
|
53
63
|
}
|
|
54
|
-
|
|
64
|
+
else {
|
|
55
65
|
throw error;
|
|
56
66
|
}
|
|
57
|
-
await LazyPromise.delay(
|
|
58
|
-
callInvokable(onRetryEnd, {
|
|
59
|
-
error,
|
|
60
|
-
waitTime: time,
|
|
61
|
-
attempt,
|
|
62
|
-
args,
|
|
63
|
-
context,
|
|
64
|
-
});
|
|
67
|
+
await LazyPromise.delay(waitTime, signal);
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
let errorMessage = `Promise was failed after retried ${String(maxAttempts)} times`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/retry/retry.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,aAAa,EACb,QAAQ,GAIX,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACH,wBAAwB,GAE3B,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D,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CACtC,aAAa,CAAC,aAAa,EAAE,OAAO,EAAE,KAAK,CAAC,CAC/C,CAAC;gBACF,aAAa,CAAC,YAAY,EAAE;oBACxB,KAAK;oBACL,QAAQ;oBACR,OAAO;oBACP,IAAI;oBACJ,OAAO;iBACV,CAAC,CAAC;gBAEH,IAAI,aAAa,CAAC,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC;oBACpC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACvB,CAAC;qBAAM,CAAC;oBACJ,MAAM,KAAK,CAAC;gBAChB,CAAC;gBACD,MAAM,WAAW,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YAC9C,CAAC;QACL,CAAC;QAED,IAAI,YAAY,GAAG,oCAAoC,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC;QACnF,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC5C,IAAI,SAAS,EAAE,CAAC;YACZ,gEAAgE;YAChE,YAAY,GAAG,GAAG,YAAY,+BAA+B,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC;QACtF,CAAC;QACD,MAAM,IAAI,eAAe,CAAC,YAAY,EAAE;YACpC,MAAM;YACN,WAAW;SACd,CAAC,CAAC;IACP,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Async
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
4
|
+
import { TimeSpan } from "../../../utilities/_module-exports.js";
|
|
5
5
|
import { type AsyncMiddlewareFn, type HookContext } from "../../../utilities/_module-exports.js";
|
|
6
6
|
import { type Invokable } from "../../../utilities/_module-exports.js";
|
|
7
7
|
/**
|
|
@@ -10,7 +10,7 @@ import { type Invokable } from "../../../utilities/_module-exports.js";
|
|
|
10
10
|
* @group Middleware
|
|
11
11
|
*/
|
|
12
12
|
export type OnTimeoutData<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = {
|
|
13
|
-
|
|
13
|
+
waitTime: TimeSpan;
|
|
14
14
|
args: TParameters;
|
|
15
15
|
context: TContext;
|
|
16
16
|
};
|
|
@@ -25,19 +25,29 @@ export type OnTimeout<TParameters extends unknown[] = unknown[], TContext extend
|
|
|
25
25
|
* IMPORT_PATH: `"@daiso-tech/core/async"`
|
|
26
26
|
* @group Middleware
|
|
27
27
|
*/
|
|
28
|
-
export type
|
|
28
|
+
export type TimeoutCallbacks<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = {
|
|
29
|
+
/**
|
|
30
|
+
* Callback function that will be called when the timeout occurs.
|
|
31
|
+
*/
|
|
32
|
+
onTimeout?: OnTimeout<TParameters, TContext>;
|
|
33
|
+
};
|
|
29
34
|
/**
|
|
30
35
|
*
|
|
31
36
|
* IMPORT_PATH: `"@daiso-tech/core/async"`
|
|
32
37
|
* @group Middleware
|
|
33
38
|
*/
|
|
34
|
-
export type TimeoutSettings<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = {
|
|
35
|
-
time: TimeSpan;
|
|
36
|
-
signalBinder?: AbortSignalBinder<TParameters>;
|
|
39
|
+
export type TimeoutSettings<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = TimeoutCallbacks<TParameters, TContext> & {
|
|
37
40
|
/**
|
|
38
|
-
*
|
|
41
|
+
* The maximum time to wait before automatically aborting the executing function.
|
|
42
|
+
*
|
|
43
|
+
* @default
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
46
|
+
*
|
|
47
|
+
* TimeSpan.fromSeconds(2)
|
|
48
|
+
* ```
|
|
39
49
|
*/
|
|
40
|
-
|
|
50
|
+
waitTime?: TimeSpan;
|
|
41
51
|
};
|
|
42
52
|
/**
|
|
43
53
|
* The `timeout` middleware automatically cancels functions after a specified time period, throwing an error when aborted.
|
|
@@ -45,7 +55,6 @@ export type TimeoutSettings<TParameters extends unknown[] = unknown[], TContext
|
|
|
45
55
|
*
|
|
46
56
|
* IMPORT_PATH: `"@daiso-tech/core/async"`
|
|
47
57
|
* @group Middleware
|
|
48
|
-
*
|
|
49
58
|
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
|
|
50
59
|
*
|
|
51
60
|
* @example
|
|
@@ -53,36 +62,22 @@ export type TimeoutSettings<TParameters extends unknown[] = unknown[], TContext
|
|
|
53
62
|
* import { timeout } from "@daiso-tech/core/async";
|
|
54
63
|
* import { AsyncHooks, TimeSpan } from "@daiso-tech/core/utilities";
|
|
55
64
|
*
|
|
56
|
-
* const
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* })
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
+
* const data = await new AsyncHooks(
|
|
66
|
+
* async (url: string, signal?: AbortSignal): Promise<unknown> => {
|
|
67
|
+
* const response = await fetch(url, { signal });
|
|
68
|
+
* return await response.json();
|
|
69
|
+
* },
|
|
70
|
+
* [timeout({ waitTime: TimeSpan.fromSeconds(2) })],
|
|
71
|
+
* {
|
|
72
|
+
* signalBinder: {
|
|
73
|
+
* getSignal: (args) => args[1],
|
|
74
|
+
* forwardSignal: (args, signal) => {
|
|
75
|
+
* args[1] = signal;
|
|
76
|
+
* }
|
|
77
|
+
* }
|
|
65
78
|
* }
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* time: TimeSpan.fromSeconds(2),
|
|
69
|
-
* // With the defined signalBinder the HTTP request will be arboted when timed out or when the inputed `AbortSignal` is called.
|
|
70
|
-
* signalBinder: ([url, fetchSignal], timeoutSignal) => {
|
|
71
|
-
* return [
|
|
72
|
-
* url,
|
|
73
|
-
* AbortSignal.any([
|
|
74
|
-
* fetchSignal,
|
|
75
|
-
* timeoutSignal
|
|
76
|
-
* ].filter(signal => signal !== undefined))
|
|
77
|
-
* ] as const;
|
|
78
|
-
* }
|
|
79
|
-
* }))
|
|
80
|
-
* .invoke("ENDPOINT", abortController.signal);
|
|
81
|
-
*
|
|
82
|
-
* abortController.abort();
|
|
83
|
-
*
|
|
84
|
-
* // An error will be thrown.
|
|
85
|
-
* await promise;
|
|
79
|
+
* )
|
|
80
|
+
* .invoke("URL");
|
|
86
81
|
* ```
|
|
87
82
|
*/
|
|
88
|
-
export declare function timeout<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings
|
|
83
|
+
export declare function timeout<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings?: NoInfer<TimeoutSettings<TParameters, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Async
|
|
3
3
|
*/
|
|
4
|
+
import { TimeSpan } from "../../../utilities/_module-exports.js";
|
|
4
5
|
import {} from "../../../utilities/_module-exports.js";
|
|
5
6
|
import { callInvokable } from "../../../utilities/_module-exports.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { TimeoutAsyncError } from "../../../async/async.errors.js";
|
|
8
|
+
import { timeoutAndFail } from "../../../async/utilities/_module.js";
|
|
8
9
|
/**
|
|
9
10
|
* The `timeout` middleware automatically cancels functions after a specified time period, throwing an error when aborted.
|
|
10
11
|
* Note the original function continues executing (even if the promise fails), you'll need to provide a settings.signalBinder to forward the `AbortSignal`.
|
|
11
12
|
*
|
|
12
13
|
* IMPORT_PATH: `"@daiso-tech/core/async"`
|
|
13
14
|
* @group Middleware
|
|
14
|
-
*
|
|
15
15
|
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
@@ -19,59 +19,42 @@ import { abortAndFail } from "../../../async/utilities/_module.js";
|
|
|
19
19
|
* import { timeout } from "@daiso-tech/core/async";
|
|
20
20
|
* import { AsyncHooks, TimeSpan } from "@daiso-tech/core/utilities";
|
|
21
21
|
*
|
|
22
|
-
* const
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* })
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* // With the defined signalBinder the HTTP request will be arboted when timed out or when the inputed `AbortSignal` is called.
|
|
36
|
-
* signalBinder: ([url, fetchSignal], timeoutSignal) => {
|
|
37
|
-
* return [
|
|
38
|
-
* url,
|
|
39
|
-
* AbortSignal.any([
|
|
40
|
-
* fetchSignal,
|
|
41
|
-
* timeoutSignal
|
|
42
|
-
* ].filter(signal => signal !== undefined))
|
|
43
|
-
* ] as const;
|
|
22
|
+
* const data = await new AsyncHooks(
|
|
23
|
+
* async (url: string, signal?: AbortSignal): Promise<unknown> => {
|
|
24
|
+
* const response = await fetch(url, { signal });
|
|
25
|
+
* return await response.json();
|
|
26
|
+
* },
|
|
27
|
+
* [timeout({ waitTime: TimeSpan.fromSeconds(2) })],
|
|
28
|
+
* {
|
|
29
|
+
* signalBinder: {
|
|
30
|
+
* getSignal: (args) => args[1],
|
|
31
|
+
* forwardSignal: (args, signal) => {
|
|
32
|
+
* args[1] = signal;
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
44
35
|
* }
|
|
45
|
-
*
|
|
46
|
-
* .invoke("
|
|
47
|
-
*
|
|
48
|
-
* abortController.abort();
|
|
49
|
-
*
|
|
50
|
-
* // An error will be thrown.
|
|
51
|
-
* await promise;
|
|
36
|
+
* )
|
|
37
|
+
* .invoke("URL");
|
|
52
38
|
* ```
|
|
53
39
|
*/
|
|
54
|
-
export function timeout(settings) {
|
|
55
|
-
const {
|
|
56
|
-
return async (args, next, context) => {
|
|
57
|
-
const timeoutController = new AbortController();
|
|
58
|
-
const timeoutId = setTimeout(() => {
|
|
59
|
-
timeoutController.abort(new TimeoutAsyncError("The promise exceded time"));
|
|
60
|
-
}, time.toMilliseconds());
|
|
40
|
+
export function timeout(settings = {}) {
|
|
41
|
+
const { waitTime = TimeSpan.fromSeconds(2), onTimeout = () => { } } = settings;
|
|
42
|
+
return async (args, next, { context, abort, signal }) => {
|
|
61
43
|
try {
|
|
62
|
-
return await
|
|
44
|
+
return await timeoutAndFail(next(...args), waitTime, (error) => {
|
|
45
|
+
abort(error);
|
|
46
|
+
}, signal);
|
|
63
47
|
}
|
|
64
48
|
catch (error) {
|
|
65
|
-
if (error instanceof
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
49
|
+
if (error instanceof TimeoutAsyncError) {
|
|
50
|
+
callInvokable(onTimeout, {
|
|
51
|
+
args,
|
|
52
|
+
context,
|
|
53
|
+
waitTime,
|
|
54
|
+
});
|
|
69
55
|
}
|
|
70
56
|
throw error;
|
|
71
57
|
}
|
|
72
|
-
finally {
|
|
73
|
-
clearTimeout(timeoutId);
|
|
74
|
-
}
|
|
75
58
|
};
|
|
76
59
|
}
|
|
77
60
|
//# sourceMappingURL=timeout.middleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/timeout/timeout.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"timeout.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/timeout/timeout.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAGN,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,aAAa,EAAkB,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AA+D9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,OAAO,CAKnB,WAA4D,EAAE;IAE9D,MAAM,EAAE,QAAQ,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,SAAS,GAAG,GAAG,EAAE,GAAE,CAAC,EAAE,GAC9D,QAAQ,CAAC;IACb,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE;QACpD,IAAI,CAAC;YACD,OAAO,MAAM,cAAc,CACvB,IAAI,CAAC,GAAG,IAAI,CAAC,EACb,QAAQ,EACR,CAAC,KAAc,EAAE,EAAE;gBACf,KAAK,CAAC,KAAK,CAAC,CAAC;YACjB,CAAC,EACD,MAAM,CACT,CAAC;QACN,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACtB,IAAI,KAAK,YAAY,iBAAiB,EAAE,CAAC;gBACrC,aAAa,CAAC,SAAS,EAAE;oBACrB,IAAI;oBACJ,OAAO;oBACP,QAAQ;iBACX,CAAC,CAAC;YACP,CAAC;YACD,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export * from "../../async/utilities/abort-and-fail/_module.js";
|
|
2
2
|
export * from "../../async/utilities/lazy-promise/_module.js";
|
|
3
|
+
export * from "../../async/utilities/promise-queue/_module.js";
|
|
4
|
+
export * from "../../async/utilities/timeout-and-fail/_module.js";
|
|
3
5
|
//# sourceMappingURL=_module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../src/async/utilities/_module.ts"],"names":[],"mappings":"AAAA,cAAc,6CAA6C,CAAC;AAC5D,cAAc,2CAA2C,CAAC"}
|
|
1
|
+
{"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../src/async/utilities/_module.ts"],"names":[],"mappings":"AAAA,cAAc,6CAA6C,CAAC;AAC5D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,4CAA4C,CAAC;AAC3D,cAAc,+CAA+C,CAAC"}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Async
|
|
3
3
|
*/
|
|
4
|
-
import { AbortAsyncError } from "../../../async/async.errors.js";
|
|
5
4
|
/**
|
|
6
5
|
* @internal
|
|
7
6
|
*/
|
|
8
|
-
function abortSignalToPromise(abortSignal
|
|
7
|
+
function abortSignalToPromise(abortSignal) {
|
|
9
8
|
let reject_ = null;
|
|
10
9
|
function abort() {
|
|
11
10
|
if (reject_ === null) {
|
|
12
11
|
return;
|
|
13
12
|
}
|
|
14
|
-
reject_(
|
|
13
|
+
reject_(abortSignal.reason);
|
|
15
14
|
}
|
|
16
15
|
if (abortSignal.aborted) {
|
|
17
16
|
return {
|
|
18
|
-
promise
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
18
|
+
promise: Promise.reject(abortSignal.reason),
|
|
19
19
|
abort,
|
|
20
20
|
};
|
|
21
21
|
}
|
|
@@ -37,11 +37,10 @@ function abortSignalToPromise(abortSignal, createError) {
|
|
|
37
37
|
* @internal
|
|
38
38
|
*/
|
|
39
39
|
export async function abortAndFail(promise, abortSignal) {
|
|
40
|
-
const createError = (reason) => new AbortAsyncError(`Promise was aborted with reason of "${String(reason)}"`, reason);
|
|
41
40
|
if (abortSignal.aborted) {
|
|
42
|
-
throw
|
|
41
|
+
throw abortSignal.reason;
|
|
43
42
|
}
|
|
44
|
-
const { promise: abortSignalPromise, abort } = abortSignalToPromise(abortSignal
|
|
43
|
+
const { promise: abortSignalPromise, abort } = abortSignalToPromise(abortSignal);
|
|
45
44
|
try {
|
|
46
45
|
return await Promise.race([promise, abortSignalPromise]);
|
|
47
46
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abort-and-fail.js","sourceRoot":"","sources":["../../../../src/async/utilities/abort-and-fail/abort-and-fail.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"abort-and-fail.js","sourceRoot":"","sources":["../../../../src/async/utilities/abort-and-fail/abort-and-fail.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,SAAS,oBAAoB,CACzB,WAAwB;IAKxB,IAAI,OAAO,GAAsC,IAAI,CAAC;IACtD,SAAS,KAAK;QACV,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO;QACX,CAAC;QACD,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAED,IAAI,WAAW,CAAC,OAAO,EAAE,CAAC;QACtB,OAAO;YACH,2EAA2E;YAC3E,OAAO,EAAE,OAAO,CAAC,MAAM,CAAS,WAAW,CAAC,MAAM,CAAC;YACnD,KAAK;SACR,CAAC;IACN,CAAC;IAED,OAAO;QACH,OAAO,EAAE,IAAI,OAAO,CAAC,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;YACtC,OAAO,GAAG,MAAM,CAAC;YACjB,IAAI,WAAW,CAAC,OAAO,EAAE,CAAC;gBACtB,KAAK,EAAE,CAAC;gBACR,OAAO;YACX,CAAC;YACD,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,KAAK,EAAE;gBACzC,IAAI,EAAE,IAAI;aACb,CAAC,CAAC;QACP,CAAC,CAAC;QACF,KAAK;KACR,CAAC;AACN,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAC9B,OAA4B,EAC5B,WAAwB;IAExB,IAAI,WAAW,CAAC,OAAO,EAAE,CAAC;QACtB,MAAM,WAAW,CAAC,MAAM,CAAC;IAC7B,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,GACxC,oBAAoB,CAAS,WAAW,CAAC,CAAC;IAC9C,IAAI,CAAC;QACD,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAC7D,CAAC;YAAS,CAAC;QACP,WAAW,CAAC,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;AACL,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lazy-promise.js","sourceRoot":"","sources":["../../../../src/async/utilities/lazy-promise/lazy-promise.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACH,UAAU,EAQV,aAAa,EACb,oBAAoB,GACvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,6CAA6C,CAAC;AAiC3E;;;;;;GAMG;AACH,MAAM,OAAO,WAAW;IACpB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,MAAM,CACT,EAAyC;QAEzC,OAAO,CAAC,GAAG,UAAU,EAAE,EAAE,CACrB,IAAI,WAAW,CAAU,GAAG,EAAE,CAAC,aAAa,CAAC,EAAE,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC;IACzE,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,KAAK,CACR,IAAc,EACd,cAA2B,IAAI,eAAe,EAAE,CAAC,MAAM;QAEvD,OAAO,IAAI,WAAW,CAAC,KAAK,IAAI,EAAE;YAC9B,IAAI,SAAS,GAAG,IAA+C,CAAC;YAChE,IAAI,CAAC;gBACD,MAAM,YAAY,CACd,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;oBAC1B,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;wBACxB,OAAO,EAAE,CAAC;oBACd,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,CAAC,CAAC;gBAC9B,CAAC,CAAC,EACF,WAAW,CACd,CAAC;YACN,CAAC;oBAAS,CAAC;gBACP,
|
|
1
|
+
{"version":3,"file":"lazy-promise.js","sourceRoot":"","sources":["../../../../src/async/utilities/lazy-promise/lazy-promise.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACH,UAAU,EAQV,aAAa,EACb,oBAAoB,GAC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AAC,OAAO,KAAK,IAAI,EAAE,CAAC;YACxB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;QAC3C,CAAC;QACD,qFAAqF;QACrF,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,KAAK;QACD,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACxB,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../../async/utilities/promise-queue/promise-queue.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../src/async/utilities/promise-queue/_module.ts"],"names":[],"mappings":"AAAA,cAAc,kDAAkD,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import type { TimeSpan } from "../../../utilities/_module-exports.js";
|
|
5
|
+
import { type InvokableFn, type Promisable } from "../../../utilities/_module-exports.js";
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export type PromiseQueueSettings = {
|
|
10
|
+
maxConcurrency: number;
|
|
11
|
+
maxCapacity: number | null;
|
|
12
|
+
interval: TimeSpan;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare class PromiseQueue {
|
|
18
|
+
private readonly settings;
|
|
19
|
+
private readonly queue;
|
|
20
|
+
private readonly listeners;
|
|
21
|
+
constructor(settings: PromiseQueueSettings);
|
|
22
|
+
private start;
|
|
23
|
+
private getItems;
|
|
24
|
+
private process;
|
|
25
|
+
private enqueue;
|
|
26
|
+
private listenOnce;
|
|
27
|
+
private asPromise;
|
|
28
|
+
/**
|
|
29
|
+
* @throws {CapacityFullAsyncError} {@link CapacityFullAsyncError}
|
|
30
|
+
*/
|
|
31
|
+
add<TValue>(func: InvokableFn<[signal: AbortSignal], Promisable<TValue>>, signal: AbortSignal): Promise<TValue>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import {} from "../../../utilities/_module-exports.js";
|
|
5
|
+
import { LazyPromise } from "../../../async/utilities/_module.js";
|
|
6
|
+
import { v4 } from "uuid";
|
|
7
|
+
import { CapacityFullAsyncError } from "../../../async/async.errors.js";
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
class Queue {
|
|
12
|
+
array = [];
|
|
13
|
+
size() {
|
|
14
|
+
return this.array.length;
|
|
15
|
+
}
|
|
16
|
+
enqueue(value) {
|
|
17
|
+
this.array.push(value);
|
|
18
|
+
}
|
|
19
|
+
dequeue() {
|
|
20
|
+
return this.array.shift() ?? null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export class PromiseQueue {
|
|
27
|
+
settings;
|
|
28
|
+
queue = new Queue();
|
|
29
|
+
listeners = new Map();
|
|
30
|
+
constructor(settings) {
|
|
31
|
+
this.settings = settings;
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
33
|
+
this.start();
|
|
34
|
+
}
|
|
35
|
+
async start() {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
|
|
37
|
+
while (true) {
|
|
38
|
+
await Promise.all(this.getItems().map((item) => this.process(item)));
|
|
39
|
+
await LazyPromise.delay(this.settings.interval);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getItems() {
|
|
43
|
+
const values = [];
|
|
44
|
+
for (let i = 0; i < this.settings.maxConcurrency; i++) {
|
|
45
|
+
const item = this.queue.dequeue();
|
|
46
|
+
if (item === null) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
values.push(item);
|
|
50
|
+
}
|
|
51
|
+
return values;
|
|
52
|
+
}
|
|
53
|
+
async process(item) {
|
|
54
|
+
const listener = this.listeners.get(item.id);
|
|
55
|
+
try {
|
|
56
|
+
if (item.signal.aborted) {
|
|
57
|
+
listener?.([null, item.signal.reason]);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const value = await item.func(item.signal);
|
|
61
|
+
listener?.([value, null]);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
listener?.([null, error]);
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
enqueue(func, signal) {
|
|
69
|
+
const id = v4();
|
|
70
|
+
if (this.settings.maxCapacity === null) {
|
|
71
|
+
this.queue.enqueue({
|
|
72
|
+
id,
|
|
73
|
+
func,
|
|
74
|
+
signal,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else if (this.queue.size() <= this.settings.maxCapacity) {
|
|
78
|
+
this.queue.enqueue({ id, func, signal });
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw new CapacityFullAsyncError(`Max capacity reached, ${String(this.settings.maxCapacity)} items allowed.`);
|
|
82
|
+
}
|
|
83
|
+
return id;
|
|
84
|
+
}
|
|
85
|
+
listenOnce(id, listener) {
|
|
86
|
+
this.listeners.set(id, listener);
|
|
87
|
+
}
|
|
88
|
+
asPromise(id) {
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
this.listenOnce(id, ([value, error]) => {
|
|
91
|
+
if (value === null) {
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
93
|
+
reject(error);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
resolve(value);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @throws {CapacityFullAsyncError} {@link CapacityFullAsyncError}
|
|
102
|
+
*/
|
|
103
|
+
add(func, signal) {
|
|
104
|
+
return this.asPromise(this.enqueue(func, signal));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=promise-queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise-queue.js","sourceRoot":"","sources":["../../../../src/async/utilities/promise-queue/promise-queue.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAIN,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAC1B,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AAEjE;;GAEG;AACH,MAAM,KAAK;IACU,KAAK,GAAa,EAAE,CAAC;IAEtC,IAAI;QACA,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;IAC7B,CAAC;IAED,OAAO,CAAC,KAAa;QACjB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,IAAI,CAAC;IACtC,CAAC;CACJ;AA2BD;;GAEG;AACH,MAAM,OAAO,YAAY;IAKQ;IAJZ,KAAK,GAAG,IAAI,KAAK,EAA6B,CAAC;IAE/C,SAAS,GAAG,IAAI,GAAG,EAAqC,CAAC;IAE1E,YAA6B,QAA8B;QAA9B,aAAQ,GAAR,QAAQ,CAAsB;QACvD,mEAAmE;QACnE,IAAI,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC;IAEO,KAAK,CAAC,KAAK;QACf,8FAA8F;QAC9F,OAAO,IAAI,EAAE,CAAC;YACV,MAAM,OAAO,CAAC,GAAG,CACb,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,C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O,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IACtD,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../../async/utilities/timeout-and-fail/timeout-and-fail.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../src/async/utilities/timeout-and-fail/_module.ts"],"names":[],"mappings":"AAAA,cAAc,wDAAwD,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { TimeSpan } from "../../../utilities/_module-exports.js";
|
|
2
|
+
/**
|
|
3
|
+
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export declare function timeoutAndFail<TValue>(promise: PromiseLike<TValue>, time: TimeSpan, abort: (rejection: unknown) => void, signal: AbortSignal): Promise<TValue>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TimeoutAsyncError } from "../../../async/async.errors.js";
|
|
2
|
+
import { abortAndFail } from "../../../async/utilities/abort-and-fail/_module.js";
|
|
3
|
+
/**
|
|
4
|
+
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export async function timeoutAndFail(promise, time, abort, signal) {
|
|
9
|
+
const timeoutId = setTimeout(() => {
|
|
10
|
+
abort(new TimeoutAsyncError("The promise exceded time"));
|
|
11
|
+
}, time.toMilliseconds());
|
|
12
|
+
try {
|
|
13
|
+
return await abortAndFail(promise, signal);
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
clearTimeout(timeoutId);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=timeout-and-fail.js.map
|