@kikiutils/shared 13.1.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.
@@ -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,27 +17,57 @@ 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} [key] - Identifier for the awaited event
21
- * @param {T} [value] - Optional value to resolve the awaiting promises with
20
+ * @param {string} key - Identifier for the awaited event
21
+ * @param {T | undefined} value - The value to resolve the awaiting promises with.
22
+ * May be `undefined` to indicate no result or a timeout-like behavior.
22
23
  */
23
- trigger(key: string, value?: T): void;
24
+ trigger(key: string, value: T | undefined): void;
24
25
  /**
25
26
  * Waits for an event associated with the given key.
26
27
  *
27
28
  * The returned promise will resolve when:
28
29
  * - `trigger(key)` is called, in which case it resolves with the provided value.
29
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`.
30
32
  *
31
- * Multiple consumers can wait on the same key; all of them will be notified when triggered.
33
+ * Behavior when multiple waiters exist for the same key:
34
+ * - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
35
+ * - `strict` mode: throws an error if a waiter already exists for the given key.
36
+ * - `override` mode: cancels all existing waiters (resolving them with `undefined`)
37
+ * and only keeps the latest one.
32
38
  *
33
- * @param {string} [key] - Identifier for the awaited event
39
+ * @param {string} key - Identifier for the awaited event
34
40
  * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
35
- * If reached, the promise resolves with `undefined`
41
+ * If reached, the promise resolves with `undefined`.
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`
36
44
  *
37
- * @returns {Promise<T | undefined>} A promise that resolves with the triggered value
38
- * 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
39
47
  */
40
- wait(key: string, timeoutMs?: number): Promise<T | undefined>;
48
+ wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict', signal?: AbortSignal): Promise<T | undefined>;
49
+ /**
50
+ * Waits for an event in strict mode.
51
+ * Only one waiter is allowed per key. If another waiter already exists,
52
+ * this method will throw an error.
53
+ *
54
+ * @param {string} key - Identifier for the awaited event
55
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
56
+ * If reached, the promise resolves with `undefined`.
57
+ * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
58
+ */
59
+ waitExclusive(key: string, timeoutMs?: number, signal?: AbortSignal): Promise<T | undefined>;
60
+ /**
61
+ * Waits for an event in override mode.
62
+ * If another waiter already exists for the given key, it will be canceled
63
+ * (resolved with `undefined`) and replaced by the new waiter.
64
+ *
65
+ * @param {string} key - Identifier for the awaited event
66
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
67
+ * If reached, the promise resolves with `undefined`.
68
+ * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
69
+ */
70
+ waitLatest(key: string, timeoutMs?: number, signal?: AbortSignal): Promise<T | undefined>;
41
71
  }
42
72
  //#endregion
43
73
  export { EventAwaiter };
@@ -1 +1 @@
1
- {"version":3,"file":"event-awaiter.d.ts","names":[],"sources":["../src/event-awaiter.ts"],"sourcesContent":[],"mappings":";;AAYA;;;;;;;;;;;cAAa;;;;;;;;;+BAUoB;;;;;;;;;;;;;;;;;yCAwBO,QAAA"}
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"}
@@ -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,8 +17,9 @@ 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} [key] - Identifier for the awaited event
21
- * @param {T} [value] - Optional value to resolve the awaiting promises with
20
+ * @param {string} key - Identifier for the awaited event
21
+ * @param {T | undefined} value - The value to resolve the awaiting promises with.
22
+ * May be `undefined` to indicate no result or a timeout-like behavior.
22
23
  */
23
24
  trigger(key, value) {
24
25
  const resolvers = this.#promiseResolvers.get(key);
@@ -33,19 +34,33 @@ var EventAwaiter = class {
33
34
  * The returned promise will resolve when:
34
35
  * - `trigger(key)` is called, in which case it resolves with the provided value.
35
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`.
36
38
  *
37
- * Multiple consumers can wait on the same key; all of them will be notified when triggered.
39
+ * Behavior when multiple waiters exist for the same key:
40
+ * - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
41
+ * - `strict` mode: throws an error if a waiter already exists for the given key.
42
+ * - `override` mode: cancels all existing waiters (resolving them with `undefined`)
43
+ * and only keeps the latest one.
38
44
  *
39
- * @param {string} [key] - Identifier for the awaited event
45
+ * @param {string} key - Identifier for the awaited event
40
46
  * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
41
- * If reached, the promise resolves with `undefined`
47
+ * If reached, the promise resolves with `undefined`.
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`
42
50
  *
43
- * @returns {Promise<T | undefined>} A promise that resolves with the triggered value
44
- * 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
45
53
  */
46
- wait(key, timeoutMs) {
54
+ wait(key, timeoutMs, mode, signal) {
47
55
  return new Promise((resolve) => {
48
56
  const resolvers = this.#promiseResolvers.get(key) || [];
57
+ if (resolvers.length) switch (mode) {
58
+ case "override":
59
+ resolvers.forEach((r) => r(void 0));
60
+ resolvers.length = 0;
61
+ break;
62
+ case "strict": throw new Error(`Duplicate wait detected for key: ${key}`);
63
+ }
49
64
  resolvers.push(resolve);
50
65
  this.#promiseResolvers.set(key, resolvers);
51
66
  if (timeoutMs) setTimeout(() => {
@@ -57,8 +72,43 @@ var EventAwaiter = class {
57
72
  else this.#promiseResolvers.delete(key);
58
73
  }
59
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 });
60
84
  });
61
85
  }
86
+ /**
87
+ * Waits for an event in strict mode.
88
+ * Only one waiter is allowed per key. If another waiter already exists,
89
+ * this method will throw an error.
90
+ *
91
+ * @param {string} key - Identifier for the awaited event
92
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
93
+ * If reached, the promise resolves with `undefined`.
94
+ * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
95
+ */
96
+ waitExclusive(key, timeoutMs, signal) {
97
+ return this.wait(key, timeoutMs, "strict", signal);
98
+ }
99
+ /**
100
+ * Waits for an event in override mode.
101
+ * If another waiter already exists for the given key, it will be canceled
102
+ * (resolved with `undefined`) and replaced by the new waiter.
103
+ *
104
+ * @param {string} key - Identifier for the awaited event
105
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
106
+ * If reached, the promise resolves with `undefined`.
107
+ * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
108
+ */
109
+ waitLatest(key, timeoutMs, signal) {
110
+ return this.wait(key, timeoutMs, "override", signal);
111
+ }
62
112
  };
63
113
 
64
114
  //#endregion
@@ -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} [key] - Identifier for the awaited event\n * @param {T} [value] - Optional value to resolve the awaiting promises with\n */\n trigger(key: string, value?: T) {\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 *\n * Multiple consumers can wait on the same key; all of them will be notified when triggered.\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 *\n * @returns {Promise<T | undefined>} A promise that resolves with the triggered value\n * or `undefined` if timeout occurs\n */\n wait(key: string, timeoutMs?: number) {\n return new Promise<T | undefined>((resolve) => {\n const resolvers = this.#promiseResolvers.get(key) || [];\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 }\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,IAAa,eAAb,MAA6B;CACzB,oCAAoB,IAAI,KAA8E;;;;;;;;CAStG,QAAQ,KAAa,OAAW;EAC5B,MAAM,YAAY,MAAKA,iBAAkB,IAAI,IAAI;AACjD,MAAI,WAAW;AACX,SAAKA,iBAAkB,OAAO,IAAI;AAClC,cAAW,SAAS,YAAY,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;;CAoBvD,KAAK,KAAa,WAAoB;AAClC,SAAO,IAAI,SAAwB,YAAY;GAC3C,MAAM,YAAY,MAAKA,iBAAkB,IAAI,IAAI,IAAI,EAAE;AACvD,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;IAEP"}
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.1.0",
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",
@@ -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,10 +17,11 @@ 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} [key] - Identifier for the awaited event
21
- * @param {T} [value] - Optional value to resolve the awaiting promises with
20
+ * @param {string} key - Identifier for the awaited event
21
+ * @param {T | undefined} value - The value to resolve the awaiting promises with.
22
+ * May be `undefined` to indicate no result or a timeout-like behavior.
22
23
  */
23
- trigger(key: string, value?: T) {
24
+ trigger(key: string, value: T | undefined) {
24
25
  const resolvers = this.#promiseResolvers.get(key);
25
26
  if (resolvers) {
26
27
  this.#promiseResolvers.delete(key);
@@ -34,19 +35,37 @@ export class EventAwaiter<T> {
34
35
  * The returned promise will resolve when:
35
36
  * - `trigger(key)` is called, in which case it resolves with the provided value.
36
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`.
37
39
  *
38
- * Multiple consumers can wait on the same key; all of them will be notified when triggered.
40
+ * Behavior when multiple waiters exist for the same key:
41
+ * - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
42
+ * - `strict` mode: throws an error if a waiter already exists for the given key.
43
+ * - `override` mode: cancels all existing waiters (resolving them with `undefined`)
44
+ * and only keeps the latest one.
39
45
  *
40
- * @param {string} [key] - Identifier for the awaited event
46
+ * @param {string} key - Identifier for the awaited event
41
47
  * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
42
- * If reached, the promise resolves with `undefined`
48
+ * If reached, the promise resolves with `undefined`.
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`
43
51
  *
44
- * @returns {Promise<T | undefined>} A promise that resolves with the triggered value
45
- * 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
46
54
  */
47
- wait(key: string, timeoutMs?: number) {
55
+ wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict', signal?: AbortSignal) {
48
56
  return new Promise<T | undefined>((resolve) => {
49
57
  const resolvers = this.#promiseResolvers.get(key) || [];
58
+ if (resolvers.length) {
59
+ switch (mode) {
60
+ case 'override':
61
+ resolvers.forEach((r) => r(undefined));
62
+ resolvers.length = 0;
63
+ break;
64
+ case 'strict':
65
+ throw new Error(`Duplicate wait detected for key: ${key}`);
66
+ }
67
+ }
68
+
50
69
  resolvers.push(resolve);
51
70
  this.#promiseResolvers.set(key, resolvers);
52
71
  if (timeoutMs) {
@@ -63,6 +82,50 @@ export class EventAwaiter<T> {
63
82
  timeoutMs,
64
83
  );
65
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
+ }
66
101
  });
67
102
  }
103
+
104
+ /**
105
+ * Waits for an event in strict mode.
106
+ * Only one waiter is allowed per key. If another waiter already exists,
107
+ * this method will throw an error.
108
+ *
109
+ * @param {string} key - Identifier for the awaited event
110
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
111
+ * If reached, the promise resolves with `undefined`.
112
+ * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
113
+ */
114
+ waitExclusive(key: string, timeoutMs?: number, signal?: AbortSignal) {
115
+ return this.wait(key, timeoutMs, 'strict', signal);
116
+ }
117
+
118
+ /**
119
+ * Waits for an event in override mode.
120
+ * If another waiter already exists for the given key, it will be canceled
121
+ * (resolved with `undefined`) and replaced by the new waiter.
122
+ *
123
+ * @param {string} key - Identifier for the awaited event
124
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
125
+ * If reached, the promise resolves with `undefined`.
126
+ * @param {AbortSignal} [signal] - Optional AbortSignal. If aborted, the promise resolves with `undefined`
127
+ */
128
+ waitLatest(key: string, timeoutMs?: number, signal?: AbortSignal) {
129
+ return this.wait(key, timeoutMs, 'override', signal);
130
+ }
68
131
  }