@kikiutils/shared 13.2.0 → 13.3.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/event-awaiter.d.ts +12 -8
- package/dist/event-awaiter.d.ts.map +1 -1
- package/dist/event-awaiter.js +23 -10
- package/dist/event-awaiter.js.map +1 -1
- package/package.json +1 -1
- package/src/event-awaiter.ts +30 -10
package/dist/event-awaiter.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* This class allows multiple consumers to `wait` for a specific key
|
|
6
6
|
* and be resolved when `trigger` is called for that key, or automatically
|
|
7
|
-
* resolved with `undefined` if a timeout occurs.
|
|
7
|
+
* resolved with `undefined` if a timeout occurs or an AbortSignal is triggered.
|
|
8
8
|
*
|
|
9
9
|
* Typical use cases include long-polling, request coordination,
|
|
10
10
|
* or implementing event-driven primitives in applications or services.
|
|
@@ -17,7 +17,7 @@ declare class EventAwaiter<T> {
|
|
|
17
17
|
* Triggers all pending promises waiting for the given key.
|
|
18
18
|
* Each promise will be resolved with the provided value.
|
|
19
19
|
*
|
|
20
|
-
* @param {string}
|
|
20
|
+
* @param {string} key - Identifier for the awaited event
|
|
21
21
|
* @param {T | undefined} value - The value to resolve the awaiting promises with.
|
|
22
22
|
* May be `undefined` to indicate no result or a timeout-like behavior.
|
|
23
23
|
*/
|
|
@@ -28,6 +28,7 @@ declare class EventAwaiter<T> {
|
|
|
28
28
|
* The returned promise will resolve when:
|
|
29
29
|
* - `trigger(key)` is called, in which case it resolves with the provided value.
|
|
30
30
|
* - The optional timeout is reached, in which case it resolves with `undefined`.
|
|
31
|
+
* - The optional `AbortSignal` is aborted, in which case it resolves with `undefined`.
|
|
31
32
|
*
|
|
32
33
|
* Behavior when multiple waiters exist for the same key:
|
|
33
34
|
* - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
|
|
@@ -38,12 +39,13 @@ declare class EventAwaiter<T> {
|
|
|
38
39
|
* @param {string} key - Identifier for the awaited event
|
|
39
40
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
40
41
|
* If reached, the promise resolves with `undefined`.
|
|
41
|
-
* @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters
|
|
42
|
+
* @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters
|
|
43
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
42
44
|
*
|
|
43
|
-
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value
|
|
44
|
-
* or `undefined` if timeout occurs
|
|
45
|
+
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value,
|
|
46
|
+
* or `undefined` if timeout or abort occurs
|
|
45
47
|
*/
|
|
46
|
-
wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict'): Promise<T | undefined>;
|
|
48
|
+
wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict', signal?: AbortSignal): Promise<T | undefined>;
|
|
47
49
|
/**
|
|
48
50
|
* Waits for an event in strict mode.
|
|
49
51
|
* Only one waiter is allowed per key. If another waiter already exists,
|
|
@@ -52,8 +54,9 @@ declare class EventAwaiter<T> {
|
|
|
52
54
|
* @param {string} key - Identifier for the awaited event
|
|
53
55
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
54
56
|
* If reached, the promise resolves with `undefined`.
|
|
57
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
55
58
|
*/
|
|
56
|
-
waitExclusive(key: string, timeoutMs?: number): Promise<T | undefined>;
|
|
59
|
+
waitExclusive(key: string, timeoutMs?: number, signal?: AbortSignal): Promise<T | undefined>;
|
|
57
60
|
/**
|
|
58
61
|
* Waits for an event in override mode.
|
|
59
62
|
* If another waiter already exists for the given key, it will be canceled
|
|
@@ -62,8 +65,9 @@ declare class EventAwaiter<T> {
|
|
|
62
65
|
* @param {string} key - Identifier for the awaited event
|
|
63
66
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
64
67
|
* If reached, the promise resolves with `undefined`.
|
|
68
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
65
69
|
*/
|
|
66
|
-
waitLatest(key: string, timeoutMs?: number): Promise<T | undefined>;
|
|
70
|
+
waitLatest(key: string, timeoutMs?: number, signal?: AbortSignal): Promise<T | undefined>;
|
|
67
71
|
}
|
|
68
72
|
//#endregion
|
|
69
73
|
export { EventAwaiter };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-awaiter.d.ts","names":[],"sources":["../src/event-awaiter.ts"],"sourcesContent":[],"mappings":";;AAYA;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"event-awaiter.d.ts","names":[],"sources":["../src/event-awaiter.ts"],"sourcesContent":[],"mappings":";;AAYA;;;;;;;;;;;AAmHoE,cAnHvD,YAmHuD,CAAA,CAAA,CAAA,CAAA;EAAA,CAAA,OAAA;;;;;;;;;8BAxGpC;;;;;;;;;;;;;;;;;;;;;;;;+EA+BiD,cAAW,QAAA;;;;;;;;;;;0DA2DhC,cAAW,QAAA;;;;;;;;;;;uDAcd,cAAW,QAAA"}
|
package/dist/event-awaiter.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* This class allows multiple consumers to `wait` for a specific key
|
|
6
6
|
* and be resolved when `trigger` is called for that key, or automatically
|
|
7
|
-
* resolved with `undefined` if a timeout occurs.
|
|
7
|
+
* resolved with `undefined` if a timeout occurs or an AbortSignal is triggered.
|
|
8
8
|
*
|
|
9
9
|
* Typical use cases include long-polling, request coordination,
|
|
10
10
|
* or implementing event-driven primitives in applications or services.
|
|
@@ -17,7 +17,7 @@ var EventAwaiter = class {
|
|
|
17
17
|
* Triggers all pending promises waiting for the given key.
|
|
18
18
|
* Each promise will be resolved with the provided value.
|
|
19
19
|
*
|
|
20
|
-
* @param {string}
|
|
20
|
+
* @param {string} key - Identifier for the awaited event
|
|
21
21
|
* @param {T | undefined} value - The value to resolve the awaiting promises with.
|
|
22
22
|
* May be `undefined` to indicate no result or a timeout-like behavior.
|
|
23
23
|
*/
|
|
@@ -34,6 +34,7 @@ var EventAwaiter = class {
|
|
|
34
34
|
* The returned promise will resolve when:
|
|
35
35
|
* - `trigger(key)` is called, in which case it resolves with the provided value.
|
|
36
36
|
* - The optional timeout is reached, in which case it resolves with `undefined`.
|
|
37
|
+
* - The optional `AbortSignal` is aborted, in which case it resolves with `undefined`.
|
|
37
38
|
*
|
|
38
39
|
* Behavior when multiple waiters exist for the same key:
|
|
39
40
|
* - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
|
|
@@ -44,12 +45,13 @@ var EventAwaiter = class {
|
|
|
44
45
|
* @param {string} key - Identifier for the awaited event
|
|
45
46
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
46
47
|
* If reached, the promise resolves with `undefined`.
|
|
47
|
-
* @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters
|
|
48
|
+
* @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters
|
|
49
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
48
50
|
*
|
|
49
|
-
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value
|
|
50
|
-
* or `undefined` if timeout occurs
|
|
51
|
+
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value,
|
|
52
|
+
* or `undefined` if timeout or abort occurs
|
|
51
53
|
*/
|
|
52
|
-
wait(key, timeoutMs, mode) {
|
|
54
|
+
wait(key, timeoutMs, mode, signal) {
|
|
53
55
|
return new Promise((resolve) => {
|
|
54
56
|
const resolvers = this.#promiseResolvers.get(key) || [];
|
|
55
57
|
if (resolvers.length) switch (mode) {
|
|
@@ -70,6 +72,15 @@ var EventAwaiter = class {
|
|
|
70
72
|
else this.#promiseResolvers.delete(key);
|
|
71
73
|
}
|
|
72
74
|
}, timeoutMs);
|
|
75
|
+
if (signal) signal.addEventListener("abort", () => {
|
|
76
|
+
const resolvers$1 = this.#promiseResolvers.get(key);
|
|
77
|
+
if (resolvers$1?.includes(resolve)) {
|
|
78
|
+
resolve(void 0);
|
|
79
|
+
const newResolvers = resolvers$1.filter((r) => r !== resolve);
|
|
80
|
+
if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);
|
|
81
|
+
else this.#promiseResolvers.delete(key);
|
|
82
|
+
}
|
|
83
|
+
}, { once: true });
|
|
73
84
|
});
|
|
74
85
|
}
|
|
75
86
|
/**
|
|
@@ -80,9 +91,10 @@ var EventAwaiter = class {
|
|
|
80
91
|
* @param {string} key - Identifier for the awaited event
|
|
81
92
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
82
93
|
* If reached, the promise resolves with `undefined`.
|
|
94
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
83
95
|
*/
|
|
84
|
-
waitExclusive(key, timeoutMs) {
|
|
85
|
-
return this.wait(key, timeoutMs, "strict");
|
|
96
|
+
waitExclusive(key, timeoutMs, signal) {
|
|
97
|
+
return this.wait(key, timeoutMs, "strict", signal);
|
|
86
98
|
}
|
|
87
99
|
/**
|
|
88
100
|
* Waits for an event in override mode.
|
|
@@ -92,9 +104,10 @@ var EventAwaiter = class {
|
|
|
92
104
|
* @param {string} key - Identifier for the awaited event
|
|
93
105
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
94
106
|
* If reached, the promise resolves with `undefined`.
|
|
107
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
95
108
|
*/
|
|
96
|
-
waitLatest(key, timeoutMs) {
|
|
97
|
-
return this.wait(key, timeoutMs, "override");
|
|
109
|
+
waitLatest(key, timeoutMs, signal) {
|
|
110
|
+
return this.wait(key, timeoutMs, "override", signal);
|
|
98
111
|
}
|
|
99
112
|
};
|
|
100
113
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-awaiter.js","names":["#promiseResolvers","resolvers"],"sources":["../src/event-awaiter.ts"],"sourcesContent":["/**\n * EventAwaiter provides a mechanism to wait for events (by key) asynchronously.\n *\n * This class allows multiple consumers to `wait` for a specific key\n * and be resolved when `trigger` is called for that key, or automatically\n * resolved with `undefined` if a timeout occurs.\n *\n * Typical use cases include long-polling, request coordination,\n * or implementing event-driven primitives in applications or services.\n *\n * @template T - The type of value that will be resolved when the event is triggered\n */\nexport class EventAwaiter<T> {\n #promiseResolvers = new Map<string, ((value: PromiseLike<T | undefined> | T | undefined) => void)[]>();\n\n /**\n * Triggers all pending promises waiting for the given key.\n * Each promise will be resolved with the provided value.\n *\n * @param {string}
|
|
1
|
+
{"version":3,"file":"event-awaiter.js","names":["#promiseResolvers","resolvers"],"sources":["../src/event-awaiter.ts"],"sourcesContent":["/**\n * EventAwaiter provides a mechanism to wait for events (by key) asynchronously.\n *\n * This class allows multiple consumers to `wait` for a specific key\n * and be resolved when `trigger` is called for that key, or automatically\n * resolved with `undefined` if a timeout occurs or an AbortSignal is triggered.\n *\n * Typical use cases include long-polling, request coordination,\n * or implementing event-driven primitives in applications or services.\n *\n * @template T - The type of value that will be resolved when the event is triggered\n */\nexport class EventAwaiter<T> {\n #promiseResolvers = new Map<string, ((value: PromiseLike<T | undefined> | T | undefined) => void)[]>();\n\n /**\n * Triggers all pending promises waiting for the given key.\n * Each promise will be resolved with the provided value.\n *\n * @param {string} key - Identifier for the awaited event\n * @param {T | undefined} value - The value to resolve the awaiting promises with.\n * May be `undefined` to indicate no result or a timeout-like behavior.\n */\n trigger(key: string, value: T | undefined) {\n const resolvers = this.#promiseResolvers.get(key);\n if (resolvers) {\n this.#promiseResolvers.delete(key);\n resolvers?.forEach((resolve) => resolve(value));\n }\n }\n\n /**\n * Waits for an event associated with the given key.\n *\n * The returned promise will resolve when:\n * - `trigger(key)` is called, in which case it resolves with the provided value.\n * - The optional timeout is reached, in which case it resolves with `undefined`.\n * - The optional `AbortSignal` is aborted, in which case it resolves with `undefined`.\n *\n * Behavior when multiple waiters exist for the same key:\n * - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.\n * - `strict` mode: throws an error if a waiter already exists for the given key.\n * - `override` mode: cancels all existing waiters (resolving them with `undefined`)\n * and only keeps the latest one.\n *\n * @param {string} key - Identifier for the awaited event\n * @param {number} [timeoutMs] - Optional timeout (in milliseconds).\n * If reached, the promise resolves with `undefined`.\n * @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters\n * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`\n *\n * @returns {Promise<T | undefined>} A promise that resolves with the triggered value,\n * or `undefined` if timeout or abort occurs\n */\n wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict', signal?: AbortSignal) {\n return new Promise<T | undefined>((resolve) => {\n const resolvers = this.#promiseResolvers.get(key) || [];\n if (resolvers.length) {\n switch (mode) {\n case 'override':\n resolvers.forEach((r) => r(undefined));\n resolvers.length = 0;\n break;\n case 'strict':\n throw new Error(`Duplicate wait detected for key: ${key}`);\n }\n }\n\n resolvers.push(resolve);\n this.#promiseResolvers.set(key, resolvers);\n if (timeoutMs) {\n setTimeout(\n () => {\n const resolvers = this.#promiseResolvers.get(key);\n if (resolvers?.includes(resolve)) {\n resolve(undefined);\n const newResolvers = resolvers.filter((r) => r !== resolve);\n if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);\n else this.#promiseResolvers.delete(key);\n }\n },\n timeoutMs,\n );\n }\n\n if (signal) {\n signal.addEventListener(\n 'abort',\n () => {\n const resolvers = this.#promiseResolvers.get(key);\n if (resolvers?.includes(resolve)) {\n resolve(undefined);\n const newResolvers = resolvers.filter((r) => r !== resolve);\n if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);\n else this.#promiseResolvers.delete(key);\n }\n },\n { once: true },\n );\n }\n });\n }\n\n /**\n * Waits for an event in strict mode.\n * Only one waiter is allowed per key. If another waiter already exists,\n * this method will throw an error.\n *\n * @param {string} key - Identifier for the awaited event\n * @param {number} [timeoutMs] - Optional timeout (in milliseconds).\n * If reached, the promise resolves with `undefined`.\n * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`\n */\n waitExclusive(key: string, timeoutMs?: number, signal?: AbortSignal) {\n return this.wait(key, timeoutMs, 'strict', signal);\n }\n\n /**\n * Waits for an event in override mode.\n * If another waiter already exists for the given key, it will be canceled\n * (resolved with `undefined`) and replaced by the new waiter.\n *\n * @param {string} key - Identifier for the awaited event\n * @param {number} [timeoutMs] - Optional timeout (in milliseconds).\n * If reached, the promise resolves with `undefined`.\n * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`\n */\n waitLatest(key: string, timeoutMs?: number, signal?: AbortSignal) {\n return this.wait(key, timeoutMs, 'override', signal);\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,IAAa,eAAb,MAA6B;CACzB,oCAAoB,IAAI,KAA8E;;;;;;;;;CAUtG,QAAQ,KAAa,OAAsB;EACvC,MAAM,YAAY,MAAKA,iBAAkB,IAAI,IAAI;AACjD,MAAI,WAAW;AACX,SAAKA,iBAAkB,OAAO,IAAI;AAClC,cAAW,SAAS,YAAY,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BvD,KAAK,KAAa,WAAoB,MAA8B,QAAsB;AACtF,SAAO,IAAI,SAAwB,YAAY;GAC3C,MAAM,YAAY,MAAKA,iBAAkB,IAAI,IAAI,IAAI,EAAE;AACvD,OAAI,UAAU,OACV,SAAQ,MAAR;IACI,KAAK;AACD,eAAU,SAAS,MAAM,EAAE,OAAU,CAAC;AACtC,eAAU,SAAS;AACnB;IACJ,KAAK,SACD,OAAM,IAAI,MAAM,oCAAoC,MAAM;;AAItE,aAAU,KAAK,QAAQ;AACvB,SAAKA,iBAAkB,IAAI,KAAK,UAAU;AAC1C,OAAI,UACA,kBACU;IACF,MAAMC,cAAY,MAAKD,iBAAkB,IAAI,IAAI;AACjD,QAAIC,aAAW,SAAS,QAAQ,EAAE;AAC9B,aAAQ,OAAU;KAClB,MAAM,eAAeA,YAAU,QAAQ,MAAM,MAAM,QAAQ;AAC3D,SAAI,aAAa,OAAQ,OAAKD,iBAAkB,IAAI,KAAK,aAAa;SACjE,OAAKA,iBAAkB,OAAO,IAAI;;MAG/C,UACH;AAGL,OAAI,OACA,QAAO,iBACH,eACM;IACF,MAAMC,cAAY,MAAKD,iBAAkB,IAAI,IAAI;AACjD,QAAIC,aAAW,SAAS,QAAQ,EAAE;AAC9B,aAAQ,OAAU;KAClB,MAAM,eAAeA,YAAU,QAAQ,MAAM,MAAM,QAAQ;AAC3D,SAAI,aAAa,OAAQ,OAAKD,iBAAkB,IAAI,KAAK,aAAa;SACjE,OAAKA,iBAAkB,OAAO,IAAI;;MAG/C,EAAE,MAAM,MAAM,CACjB;IAEP;;;;;;;;;;;;CAaN,cAAc,KAAa,WAAoB,QAAsB;AACjE,SAAO,KAAK,KAAK,KAAK,WAAW,UAAU,OAAO;;;;;;;;;;;;CAatD,WAAW,KAAa,WAAoB,QAAsB;AAC9D,SAAO,KAAK,KAAK,KAAK,WAAW,YAAY,OAAO"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "13.
|
|
4
|
+
"version": "13.3.0",
|
|
5
5
|
"description": "A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
package/src/event-awaiter.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This class allows multiple consumers to `wait` for a specific key
|
|
5
5
|
* and be resolved when `trigger` is called for that key, or automatically
|
|
6
|
-
* resolved with `undefined` if a timeout occurs.
|
|
6
|
+
* resolved with `undefined` if a timeout occurs or an AbortSignal is triggered.
|
|
7
7
|
*
|
|
8
8
|
* Typical use cases include long-polling, request coordination,
|
|
9
9
|
* or implementing event-driven primitives in applications or services.
|
|
@@ -17,7 +17,7 @@ export class EventAwaiter<T> {
|
|
|
17
17
|
* Triggers all pending promises waiting for the given key.
|
|
18
18
|
* Each promise will be resolved with the provided value.
|
|
19
19
|
*
|
|
20
|
-
* @param {string}
|
|
20
|
+
* @param {string} key - Identifier for the awaited event
|
|
21
21
|
* @param {T | undefined} value - The value to resolve the awaiting promises with.
|
|
22
22
|
* May be `undefined` to indicate no result or a timeout-like behavior.
|
|
23
23
|
*/
|
|
@@ -35,6 +35,7 @@ export class EventAwaiter<T> {
|
|
|
35
35
|
* The returned promise will resolve when:
|
|
36
36
|
* - `trigger(key)` is called, in which case it resolves with the provided value.
|
|
37
37
|
* - The optional timeout is reached, in which case it resolves with `undefined`.
|
|
38
|
+
* - The optional `AbortSignal` is aborted, in which case it resolves with `undefined`.
|
|
38
39
|
*
|
|
39
40
|
* Behavior when multiple waiters exist for the same key:
|
|
40
41
|
* - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
|
|
@@ -45,12 +46,13 @@ export class EventAwaiter<T> {
|
|
|
45
46
|
* @param {string} key - Identifier for the awaited event
|
|
46
47
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
47
48
|
* If reached, the promise resolves with `undefined`.
|
|
48
|
-
* @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters
|
|
49
|
+
* @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters
|
|
50
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
49
51
|
*
|
|
50
|
-
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value
|
|
51
|
-
* or `undefined` if timeout occurs
|
|
52
|
+
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value,
|
|
53
|
+
* or `undefined` if timeout or abort occurs
|
|
52
54
|
*/
|
|
53
|
-
wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict') {
|
|
55
|
+
wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict', signal?: AbortSignal) {
|
|
54
56
|
return new Promise<T | undefined>((resolve) => {
|
|
55
57
|
const resolvers = this.#promiseResolvers.get(key) || [];
|
|
56
58
|
if (resolvers.length) {
|
|
@@ -80,6 +82,22 @@ export class EventAwaiter<T> {
|
|
|
80
82
|
timeoutMs,
|
|
81
83
|
);
|
|
82
84
|
}
|
|
85
|
+
|
|
86
|
+
if (signal) {
|
|
87
|
+
signal.addEventListener(
|
|
88
|
+
'abort',
|
|
89
|
+
() => {
|
|
90
|
+
const resolvers = this.#promiseResolvers.get(key);
|
|
91
|
+
if (resolvers?.includes(resolve)) {
|
|
92
|
+
resolve(undefined);
|
|
93
|
+
const newResolvers = resolvers.filter((r) => r !== resolve);
|
|
94
|
+
if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);
|
|
95
|
+
else this.#promiseResolvers.delete(key);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
{ once: true },
|
|
99
|
+
);
|
|
100
|
+
}
|
|
83
101
|
});
|
|
84
102
|
}
|
|
85
103
|
|
|
@@ -91,9 +109,10 @@ export class EventAwaiter<T> {
|
|
|
91
109
|
* @param {string} key - Identifier for the awaited event
|
|
92
110
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
93
111
|
* If reached, the promise resolves with `undefined`.
|
|
112
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
94
113
|
*/
|
|
95
|
-
waitExclusive(key: string, timeoutMs?: number) {
|
|
96
|
-
return this.wait(key, timeoutMs, 'strict');
|
|
114
|
+
waitExclusive(key: string, timeoutMs?: number, signal?: AbortSignal) {
|
|
115
|
+
return this.wait(key, timeoutMs, 'strict', signal);
|
|
97
116
|
}
|
|
98
117
|
|
|
99
118
|
/**
|
|
@@ -104,8 +123,9 @@ export class EventAwaiter<T> {
|
|
|
104
123
|
* @param {string} key - Identifier for the awaited event
|
|
105
124
|
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
106
125
|
* If reached, the promise resolves with `undefined`.
|
|
126
|
+
* @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
|
|
107
127
|
*/
|
|
108
|
-
waitLatest(key: string, timeoutMs?: number) {
|
|
109
|
-
return this.wait(key, timeoutMs, 'override');
|
|
128
|
+
waitLatest(key: string, timeoutMs?: number, signal?: AbortSignal) {
|
|
129
|
+
return this.wait(key, timeoutMs, 'override', signal);
|
|
110
130
|
}
|
|
111
131
|
}
|