@twin.org/core 0.9.3-next.6 → 0.9.3-next.7
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.
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
// Copyright 2026 IOTA Stiftung.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
-
import { GeneralError } from "../errors/generalError.js";
|
|
4
3
|
import { Is } from "../utils/is.js";
|
|
5
4
|
/**
|
|
6
5
|
* Helper for bounding operations which can fail to settle.
|
|
7
6
|
*/
|
|
8
7
|
export class TimeoutHelper {
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
9
|
+
* Stop waiting for an operation which has not settled within the given time.
|
|
11
10
|
* The operation itself cannot be cancelled, so a timed out operation is abandoned, which is
|
|
12
|
-
* the only option available when the
|
|
13
|
-
*
|
|
11
|
+
* the only option available when the code being called can leave the promise it returned
|
|
12
|
+
* pending forever.
|
|
14
13
|
* @param operation The operation to bound.
|
|
15
14
|
* @param timeoutMs The maximum time to wait in milliseconds, 0 or less waits indefinitely.
|
|
16
|
-
* @param
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
19
|
-
* @returns The result of the operation.
|
|
20
|
-
* @throws GeneralError if the operation has not settled within timeoutMs.
|
|
15
|
+
* @param onTimeout Called when the wait expires, throw from it to fail the operation, or
|
|
16
|
+
* return a value to complete it with that value instead.
|
|
17
|
+
* @returns The result of the operation, or the value returned by onTimeout.
|
|
21
18
|
*/
|
|
22
|
-
static async withTimeout(operation, timeoutMs,
|
|
19
|
+
static async withTimeout(operation, timeoutMs, onTimeout) {
|
|
23
20
|
if (timeoutMs <= 0) {
|
|
24
21
|
return operation;
|
|
25
22
|
}
|
|
@@ -29,7 +26,12 @@ export class TimeoutHelper {
|
|
|
29
26
|
operation,
|
|
30
27
|
new Promise((resolve, reject) => {
|
|
31
28
|
timer = setTimeout(() => {
|
|
32
|
-
|
|
29
|
+
try {
|
|
30
|
+
resolve(onTimeout());
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
reject(error);
|
|
34
|
+
}
|
|
33
35
|
}, timeoutMs);
|
|
34
36
|
})
|
|
35
37
|
]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeoutHelper.js","sourceRoot":"","sources":["../../../src/helpers/timeoutHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"timeoutHelper.js","sourceRoot":"","sources":["../../../src/helpers/timeoutHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,aAAa;IACzB;;;;;;;;;;OAUG;IACI,MAAM,CAAC,KAAK,CAAC,WAAW,CAC9B,SAAqB,EACrB,SAAiB,EACjB,SAAkB;QAElB,IAAI,SAAS,IAAI,CAAC,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,IAAI,KAAgD,CAAC;QAErD,IAAI,CAAC;YACJ,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC;gBACzB,SAAS;gBACT,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;oBAClC,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;wBACvB,IAAI,CAAC;4BACJ,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;wBACtB,CAAC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BAChB,MAAM,CAAC,KAAK,CAAC,CAAC;wBACf,CAAC;oBACF,CAAC,EAAE,SAAS,CAAC,CAAC;gBACf,CAAC,CAAC;aACF,CAAC,CAAC;QACJ,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC1B,YAAY,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;QACF,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Helper for bounding operations which can fail to settle.\n */\nexport class TimeoutHelper {\n\t/**\n\t * Stop waiting for an operation which has not settled within the given time.\n\t * The operation itself cannot be cancelled, so a timed out operation is abandoned, which is\n\t * the only option available when the code being called can leave the promise it returned\n\t * pending forever.\n\t * @param operation The operation to bound.\n\t * @param timeoutMs The maximum time to wait in milliseconds, 0 or less waits indefinitely.\n\t * @param onTimeout Called when the wait expires, throw from it to fail the operation, or\n\t * return a value to complete it with that value instead.\n\t * @returns The result of the operation, or the value returned by onTimeout.\n\t */\n\tpublic static async withTimeout<T>(\n\t\toperation: Promise<T>,\n\t\ttimeoutMs: number,\n\t\tonTimeout: () => T\n\t): Promise<T> {\n\t\tif (timeoutMs <= 0) {\n\t\t\treturn operation;\n\t\t}\n\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\n\t\ttry {\n\t\t\treturn await Promise.race([\n\t\t\t\toperation,\n\t\t\t\tnew Promise<T>((resolve, reject) => {\n\t\t\t\t\ttimer = setTimeout(() => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tresolve(onTimeout());\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\treject(error);\n\t\t\t\t\t\t}\n\t\t\t\t\t}, timeoutMs);\n\t\t\t\t})\n\t\t\t]);\n\t\t} finally {\n\t\t\tif (!Is.undefined(timer)) {\n\t\t\t\tclearTimeout(timer);\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
|
@@ -3,19 +3,15 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export declare class TimeoutHelper {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Stop waiting for an operation which has not settled within the given time.
|
|
7
7
|
* The operation itself cannot be cancelled, so a timed out operation is abandoned, which is
|
|
8
|
-
* the only option available when the
|
|
9
|
-
*
|
|
8
|
+
* the only option available when the code being called can leave the promise it returned
|
|
9
|
+
* pending forever.
|
|
10
10
|
* @param operation The operation to bound.
|
|
11
11
|
* @param timeoutMs The maximum time to wait in milliseconds, 0 or less waits indefinitely.
|
|
12
|
-
* @param
|
|
13
|
-
*
|
|
14
|
-
* @
|
|
15
|
-
* @returns The result of the operation.
|
|
16
|
-
* @throws GeneralError if the operation has not settled within timeoutMs.
|
|
12
|
+
* @param onTimeout Called when the wait expires, throw from it to fail the operation, or
|
|
13
|
+
* return a value to complete it with that value instead.
|
|
14
|
+
* @returns The result of the operation, or the value returned by onTimeout.
|
|
17
15
|
*/
|
|
18
|
-
static withTimeout<T>(operation: Promise<T>, timeoutMs: number,
|
|
19
|
-
[id: string]: unknown;
|
|
20
|
-
}): Promise<T>;
|
|
16
|
+
static withTimeout<T>(operation: Promise<T>, timeoutMs: number, onTimeout: () => T): Promise<T>;
|
|
21
17
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.9.3-next.7](https://github.com/iotaledger/twin-framework/compare/core-v0.9.3-next.6...core-v0.9.3-next.7) (2026-09-07)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* timeout helper callback ([cb7a8d1](https://github.com/iotaledger/twin-framework/commit/cb7a8d1d3fb94a7e2148e704b23ece3c315ebb23))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/nameof bumped from 0.9.3-next.6 to 0.9.3-next.7
|
|
16
|
+
* devDependencies
|
|
17
|
+
* @twin.org/nameof-transformer bumped from 0.9.3-next.6 to 0.9.3-next.7
|
|
18
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.9.3-next.6 to 0.9.3-next.7
|
|
19
|
+
|
|
3
20
|
## [0.9.3-next.6](https://github.com/iotaledger/twin-framework/compare/core-v0.9.3-next.5...core-v0.9.3-next.6) (2026-09-07)
|
|
4
21
|
|
|
5
22
|
|
|
@@ -16,12 +16,12 @@ Helper for bounding operations which can fail to settle.
|
|
|
16
16
|
|
|
17
17
|
### withTimeout() {#withtimeout}
|
|
18
18
|
|
|
19
|
-
> `static` **withTimeout**\<`T`\>(`operation`, `timeoutMs`, `
|
|
19
|
+
> `static` **withTimeout**\<`T`\>(`operation`, `timeoutMs`, `onTimeout`): `Promise`\<`T`\>
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Stop waiting for an operation which has not settled within the given time.
|
|
22
22
|
The operation itself cannot be cancelled, so a timed out operation is abandoned, which is
|
|
23
|
-
the only option available when the
|
|
24
|
-
|
|
23
|
+
the only option available when the code being called can leave the promise it returned
|
|
24
|
+
pending forever.
|
|
25
25
|
|
|
26
26
|
#### Type Parameters
|
|
27
27
|
|
|
@@ -43,28 +43,15 @@ The operation to bound.
|
|
|
43
43
|
|
|
44
44
|
The maximum time to wait in milliseconds, 0 or less waits indefinitely.
|
|
45
45
|
|
|
46
|
-
#####
|
|
46
|
+
##### onTimeout
|
|
47
47
|
|
|
48
|
-
`
|
|
48
|
+
() => `T`
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
##### message
|
|
53
|
-
|
|
54
|
-
`string`
|
|
55
|
-
|
|
56
|
-
The message key to use for the timeout error.
|
|
57
|
-
|
|
58
|
-
##### properties?
|
|
59
|
-
|
|
60
|
-
Additional properties to include in the timeout error.
|
|
50
|
+
Called when the wait expires, throw from it to fail the operation, or
|
|
51
|
+
return a value to complete it with that value instead.
|
|
61
52
|
|
|
62
53
|
#### Returns
|
|
63
54
|
|
|
64
55
|
`Promise`\<`T`\>
|
|
65
56
|
|
|
66
|
-
The result of the operation.
|
|
67
|
-
|
|
68
|
-
#### Throws
|
|
69
|
-
|
|
70
|
-
GeneralError if the operation has not settled within timeoutMs.
|
|
57
|
+
The result of the operation, or the value returned by onTimeout.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/core",
|
|
3
|
-
"version": "0.9.3-next.
|
|
3
|
+
"version": "0.9.3-next.7",
|
|
4
4
|
"description": "Helper methods/classes for data type checking/validation/guarding/error handling",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"node": ">=24.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/nameof": "0.9.3-next.
|
|
17
|
+
"@twin.org/nameof": "0.9.3-next.7",
|
|
18
18
|
"intl-messageformat": "11.2.13",
|
|
19
19
|
"rfc6902": "5.3.0"
|
|
20
20
|
},
|