@kikiutils/shared 13.0.1 → 13.2.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 CHANGED
@@ -28,7 +28,7 @@ A lightweight and modular utility library for modern JavaScript and TypeScript
28
28
 
29
29
  ## Requirements
30
30
 
31
- - **Node.js** `>= 22.0.0`
31
+ - **Node.js** `>=22.12.0`
32
32
 
33
33
  ## Installation
34
34
 
@@ -0,0 +1,70 @@
1
+ //#region src/event-awaiter.d.ts
2
+ /**
3
+ * EventAwaiter provides a mechanism to wait for events (by key) asynchronously.
4
+ *
5
+ * This class allows multiple consumers to `wait` for a specific key
6
+ * and be resolved when `trigger` is called for that key, or automatically
7
+ * resolved with `undefined` if a timeout occurs.
8
+ *
9
+ * Typical use cases include long-polling, request coordination,
10
+ * or implementing event-driven primitives in applications or services.
11
+ *
12
+ * @template T - The type of value that will be resolved when the event is triggered
13
+ */
14
+ declare class EventAwaiter<T> {
15
+ #private;
16
+ /**
17
+ * Triggers all pending promises waiting for the given key.
18
+ * Each promise will be resolved with the provided value.
19
+ *
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.
23
+ */
24
+ trigger(key: string, value: T | undefined): void;
25
+ /**
26
+ * Waits for an event associated with the given key.
27
+ *
28
+ * The returned promise will resolve when:
29
+ * - `trigger(key)` is called, in which case it resolves with the provided value.
30
+ * - The optional timeout is reached, in which case it resolves with `undefined`.
31
+ *
32
+ * Behavior when multiple waiters exist for the same key:
33
+ * - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
34
+ * - `strict` mode: throws an error if a waiter already exists for the given key.
35
+ * - `override` mode: cancels all existing waiters (resolving them with `undefined`)
36
+ * and only keeps the latest one.
37
+ *
38
+ * @param {string} key - Identifier for the awaited event
39
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
40
+ * If reached, the promise resolves with `undefined`.
41
+ * @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters.
42
+ *
43
+ * @returns {Promise<T | undefined>} A promise that resolves with the triggered value
44
+ * or `undefined` if timeout occurs
45
+ */
46
+ wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict'): Promise<T | undefined>;
47
+ /**
48
+ * Waits for an event in strict mode.
49
+ * Only one waiter is allowed per key. If another waiter already exists,
50
+ * this method will throw an error.
51
+ *
52
+ * @param {string} key - Identifier for the awaited event
53
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
54
+ * If reached, the promise resolves with `undefined`.
55
+ */
56
+ waitExclusive(key: string, timeoutMs?: number): Promise<T | undefined>;
57
+ /**
58
+ * Waits for an event in override mode.
59
+ * If another waiter already exists for the given key, it will be canceled
60
+ * (resolved with `undefined`) and replaced by the new waiter.
61
+ *
62
+ * @param {string} key - Identifier for the awaited event
63
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
64
+ * If reached, the promise resolves with `undefined`.
65
+ */
66
+ waitLatest(key: string, timeoutMs?: number): Promise<T | undefined>;
67
+ }
68
+ //#endregion
69
+ export { EventAwaiter };
70
+ //# sourceMappingURL=event-awaiter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-awaiter.d.ts","names":[],"sources":["../src/event-awaiter.ts"],"sourcesContent":[],"mappings":";;AAYA;;;;;;;;;;;cAAa;;;;;;;;;;8BAWmB;;;;;;;;;;;;;;;;;;;;;;uEA6BsC,QAAA;;;;;;;;;;kDA0CrB,QAAA;;;;;;;;;;+CAaH,QAAA"}
@@ -0,0 +1,103 @@
1
+ //#region src/event-awaiter.ts
2
+ /**
3
+ * EventAwaiter provides a mechanism to wait for events (by key) asynchronously.
4
+ *
5
+ * This class allows multiple consumers to `wait` for a specific key
6
+ * and be resolved when `trigger` is called for that key, or automatically
7
+ * resolved with `undefined` if a timeout occurs.
8
+ *
9
+ * Typical use cases include long-polling, request coordination,
10
+ * or implementing event-driven primitives in applications or services.
11
+ *
12
+ * @template T - The type of value that will be resolved when the event is triggered
13
+ */
14
+ var EventAwaiter = class {
15
+ #promiseResolvers = /* @__PURE__ */ new Map();
16
+ /**
17
+ * Triggers all pending promises waiting for the given key.
18
+ * Each promise will be resolved with the provided value.
19
+ *
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.
23
+ */
24
+ trigger(key, value) {
25
+ const resolvers = this.#promiseResolvers.get(key);
26
+ if (resolvers) {
27
+ this.#promiseResolvers.delete(key);
28
+ resolvers?.forEach((resolve) => resolve(value));
29
+ }
30
+ }
31
+ /**
32
+ * Waits for an event associated with the given key.
33
+ *
34
+ * The returned promise will resolve when:
35
+ * - `trigger(key)` is called, in which case it resolves with the provided value.
36
+ * - The optional timeout is reached, in which case it resolves with `undefined`.
37
+ *
38
+ * Behavior when multiple waiters exist for the same key:
39
+ * - Default (no mode): multiple waiters are allowed, all will be resolved when triggered.
40
+ * - `strict` mode: throws an error if a waiter already exists for the given key.
41
+ * - `override` mode: cancels all existing waiters (resolving them with `undefined`)
42
+ * and only keeps the latest one.
43
+ *
44
+ * @param {string} key - Identifier for the awaited event
45
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
46
+ * If reached, the promise resolves with `undefined`.
47
+ * @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters.
48
+ *
49
+ * @returns {Promise<T | undefined>} A promise that resolves with the triggered value
50
+ * or `undefined` if timeout occurs
51
+ */
52
+ wait(key, timeoutMs, mode) {
53
+ return new Promise((resolve) => {
54
+ const resolvers = this.#promiseResolvers.get(key) || [];
55
+ if (resolvers.length) switch (mode) {
56
+ case "override":
57
+ resolvers.forEach((r) => r(void 0));
58
+ resolvers.length = 0;
59
+ break;
60
+ case "strict": throw new Error(`Duplicate wait detected for key: ${key}`);
61
+ }
62
+ resolvers.push(resolve);
63
+ this.#promiseResolvers.set(key, resolvers);
64
+ if (timeoutMs) setTimeout(() => {
65
+ const resolvers$1 = this.#promiseResolvers.get(key);
66
+ if (resolvers$1?.includes(resolve)) {
67
+ resolve(void 0);
68
+ const newResolvers = resolvers$1.filter((r) => r !== resolve);
69
+ if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);
70
+ else this.#promiseResolvers.delete(key);
71
+ }
72
+ }, timeoutMs);
73
+ });
74
+ }
75
+ /**
76
+ * Waits for an event in strict mode.
77
+ * Only one waiter is allowed per key. If another waiter already exists,
78
+ * this method will throw an error.
79
+ *
80
+ * @param {string} key - Identifier for the awaited event
81
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
82
+ * If reached, the promise resolves with `undefined`.
83
+ */
84
+ waitExclusive(key, timeoutMs) {
85
+ return this.wait(key, timeoutMs, "strict");
86
+ }
87
+ /**
88
+ * Waits for an event in override mode.
89
+ * If another waiter already exists for the given key, it will be canceled
90
+ * (resolved with `undefined`) and replaced by the new waiter.
91
+ *
92
+ * @param {string} key - Identifier for the awaited event
93
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
94
+ * If reached, the promise resolves with `undefined`.
95
+ */
96
+ waitLatest(key, timeoutMs) {
97
+ return this.wait(key, timeoutMs, "override");
98
+ }
99
+ };
100
+
101
+ //#endregion
102
+ export { EventAwaiter };
103
+ //# sourceMappingURL=event-awaiter.js.map
@@ -0,0 +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 | 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 *\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 *\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, mode?: 'override' | 'strict') {\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 }\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 */\n waitExclusive(key: string, timeoutMs?: number) {\n return this.wait(key, timeoutMs, 'strict');\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 */\n waitLatest(key: string, timeoutMs?: number) {\n return this.wait(key, timeoutMs, 'override');\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;;;;;;;;;;;;;;;;;;;;;;;;CAyBvD,KAAK,KAAa,WAAoB,MAA8B;AAChE,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;IAEP;;;;;;;;;;;CAYN,cAAc,KAAa,WAAoB;AAC3C,SAAO,KAAK,KAAK,KAAK,WAAW,SAAS;;;;;;;;;;;CAY9C,WAAW,KAAa,WAAoB;AACxC,SAAO,KAAK,KAAK,KAAK,WAAW,WAAW"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kikiutils/shared",
3
3
  "type": "module",
4
- "version": "13.0.1",
4
+ "version": "13.2.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",
@@ -41,6 +41,7 @@
41
41
  "./element-plus": "./dist/element-plus.js",
42
42
  "./enum": "./dist/enum.js",
43
43
  "./env": "./dist/env.js",
44
+ "./event-awaiter": "./dist/event-awaiter.js",
44
45
  "./general": "./dist/general.js",
45
46
  "./hash": "./dist/hash.js",
46
47
  "./math": "./dist/math.js",
@@ -66,7 +67,7 @@
66
67
  "./src"
67
68
  ],
68
69
  "engines": {
69
- "node": ">=22.0.0"
70
+ "node": ">=22.12.0"
70
71
  },
71
72
  "scripts": {
72
73
  "build": "tsdown",
@@ -78,7 +79,7 @@
78
79
  "release": "pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build && changelogen --hideAuthorEmail --push --release && npm publish",
79
80
  "test": "cross-env TZ=UTC vitest run --coverage",
80
81
  "test:watch": "cross-env TZ=UTC vitest watch --coverage",
81
- "typecheck": "tsc --noEmit",
82
+ "typecheck": "tsc -b --noEmit",
82
83
  "unused-exports": "ts-unused-exports ./tsconfig.json"
83
84
  },
84
85
  "peerDependencies": {
@@ -146,7 +147,7 @@
146
147
  "@kikiutils/eslint-config": "^3.0.1",
147
148
  "@kikiutils/tsconfigs": "^5.0.5",
148
149
  "@noble/hashes": "^2.0.0",
149
- "@types/node": "^24.3.3",
150
+ "@types/node": "^24.5.0",
150
151
  "@vitest/coverage-v8": "^3.2.4",
151
152
  "async-validator": "^4.2.5",
152
153
  "consola": "^3.4.2",
@@ -0,0 +1,111 @@
1
+ /**
2
+ * EventAwaiter provides a mechanism to wait for events (by key) asynchronously.
3
+ *
4
+ * This class allows multiple consumers to `wait` for a specific key
5
+ * and be resolved when `trigger` is called for that key, or automatically
6
+ * resolved with `undefined` if a timeout occurs.
7
+ *
8
+ * Typical use cases include long-polling, request coordination,
9
+ * or implementing event-driven primitives in applications or services.
10
+ *
11
+ * @template T - The type of value that will be resolved when the event is triggered
12
+ */
13
+ export class EventAwaiter<T> {
14
+ #promiseResolvers = new Map<string, ((value: PromiseLike<T | undefined> | T | undefined) => void)[]>();
15
+
16
+ /**
17
+ * Triggers all pending promises waiting for the given key.
18
+ * Each promise will be resolved with the provided value.
19
+ *
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.
23
+ */
24
+ trigger(key: string, value: T | undefined) {
25
+ const resolvers = this.#promiseResolvers.get(key);
26
+ if (resolvers) {
27
+ this.#promiseResolvers.delete(key);
28
+ resolvers?.forEach((resolve) => resolve(value));
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Waits for an event associated with the given key.
34
+ *
35
+ * The returned promise will resolve when:
36
+ * - `trigger(key)` is called, in which case it resolves with the provided value.
37
+ * - The optional timeout is reached, in which case it resolves with `undefined`.
38
+ *
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.
44
+ *
45
+ * @param {string} key - Identifier for the awaited event
46
+ * @param {number} [timeoutMs] - Optional timeout (in milliseconds).
47
+ * If reached, the promise resolves with `undefined`.
48
+ * @param {'override' | 'strict'} [mode] - Optional behavior mode for handling multiple waiters.
49
+ *
50
+ * @returns {Promise<T | undefined>} A promise that resolves with the triggered value
51
+ * or `undefined` if timeout occurs
52
+ */
53
+ wait(key: string, timeoutMs?: number, mode?: 'override' | 'strict') {
54
+ return new Promise<T | undefined>((resolve) => {
55
+ const resolvers = this.#promiseResolvers.get(key) || [];
56
+ if (resolvers.length) {
57
+ switch (mode) {
58
+ case 'override':
59
+ resolvers.forEach((r) => r(undefined));
60
+ resolvers.length = 0;
61
+ break;
62
+ case 'strict':
63
+ throw new Error(`Duplicate wait detected for key: ${key}`);
64
+ }
65
+ }
66
+
67
+ resolvers.push(resolve);
68
+ this.#promiseResolvers.set(key, resolvers);
69
+ if (timeoutMs) {
70
+ setTimeout(
71
+ () => {
72
+ const resolvers = this.#promiseResolvers.get(key);
73
+ if (resolvers?.includes(resolve)) {
74
+ resolve(undefined);
75
+ const newResolvers = resolvers.filter((r) => r !== resolve);
76
+ if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);
77
+ else this.#promiseResolvers.delete(key);
78
+ }
79
+ },
80
+ timeoutMs,
81
+ );
82
+ }
83
+ });
84
+ }
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
+ */
95
+ waitExclusive(key: string, timeoutMs?: number) {
96
+ return this.wait(key, timeoutMs, 'strict');
97
+ }
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
+ */
108
+ waitLatest(key: string, timeoutMs?: number) {
109
+ return this.wait(key, timeoutMs, 'override');
110
+ }
111
+ }