@ovencord/util 1.1.4 → 1.1.6
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/package.json +7 -10
- package/src/AsyncEventEmitter.ts +43 -2
- package/src/functions/lazy.ts +0 -2
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@ovencord/util",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.6",
|
|
5
5
|
"description": "Utilities shared across Discord.js packages",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "bun test",
|
|
8
8
|
"typecheck": "tsc --noEmit",
|
|
9
|
-
"lint": "
|
|
9
|
+
"lint": "biome check .",
|
|
10
|
+
"format": "biome format --write .",
|
|
11
|
+
"check": "biome check --write ."
|
|
10
12
|
},
|
|
11
13
|
"type": "module",
|
|
12
14
|
"exports": "./src/index.ts",
|
|
@@ -34,16 +36,11 @@
|
|
|
34
36
|
"homepage": "https://ovencord.dev",
|
|
35
37
|
"funding": "https://github.com/ovencord/ovencord?sponsor",
|
|
36
38
|
"dependencies": {
|
|
37
|
-
"discord-api-types": "^0.38.
|
|
38
|
-
},
|
|
39
|
-
"devDependencies": {
|
|
40
|
-
"@types/bun": "^1.3.8",
|
|
41
|
-
"eslint": "^9.39.2",
|
|
42
|
-
"typescript": "^5.9.3",
|
|
43
|
-
"typescript-eslint": "^8.54.0"
|
|
39
|
+
"discord-api-types": "^0.38.40"
|
|
44
40
|
},
|
|
41
|
+
"devDependencies": {},
|
|
45
42
|
"engines": {
|
|
46
|
-
"bun": ">=1.
|
|
43
|
+
"bun": ">=1.3.0"
|
|
47
44
|
},
|
|
48
45
|
"publishConfig": {
|
|
49
46
|
"access": "public"
|
package/src/AsyncEventEmitter.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { AsyncQueue } from './AsyncQueue.js';
|
|
2
|
+
|
|
1
3
|
export type EventMap = Record<string | symbol, any[]>;
|
|
2
4
|
|
|
3
|
-
export class AsyncEventEmitter<Events extends
|
|
5
|
+
export class AsyncEventEmitter<Events extends Record<keyof Events, any[]> = any> {
|
|
4
6
|
private _listeners = new Map<keyof Events | string | symbol, Set<Function>>();
|
|
5
7
|
private _maxListeners = 10;
|
|
6
8
|
|
|
@@ -139,7 +141,6 @@ export class AsyncEventEmitter<Events extends EventMap = EventMap> {
|
|
|
139
141
|
}
|
|
140
142
|
|
|
141
143
|
public waitFor<K extends keyof Events>(event: K, timeout?: number): Promise<Events[K]>;
|
|
142
|
-
public waitFor<K extends string | symbol>(event: K, timeout?: number): Promise<any[]>;
|
|
143
144
|
public waitFor(event: string | symbol, timeout?: number): Promise<any[]> {
|
|
144
145
|
return new Promise((resolve, reject) => {
|
|
145
146
|
let timeoutId: Timer | undefined;
|
|
@@ -159,4 +160,44 @@ export class AsyncEventEmitter<Events extends EventMap = EventMap> {
|
|
|
159
160
|
}
|
|
160
161
|
});
|
|
161
162
|
}
|
|
163
|
+
|
|
164
|
+
public static async *on<T extends AsyncEventEmitter<any>, K extends keyof (T extends AsyncEventEmitter<infer U> ? U : never)>(
|
|
165
|
+
emitter: T,
|
|
166
|
+
eventName: K,
|
|
167
|
+
options?: { signal?: AbortSignal }
|
|
168
|
+
): AsyncIterableIterator<(T extends AsyncEventEmitter<infer U> ? U : never)[K]> {
|
|
169
|
+
const signal = options?.signal;
|
|
170
|
+
if (signal?.aborted) throw new Error('AbortError');
|
|
171
|
+
|
|
172
|
+
const queue = new AsyncQueue();
|
|
173
|
+
const items: any[][] = [];
|
|
174
|
+
|
|
175
|
+
const listener = (...args: any[]) => {
|
|
176
|
+
items.push(args);
|
|
177
|
+
queue.shift();
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const abortHandler = () => {
|
|
181
|
+
emitter.off(eventName as any, listener);
|
|
182
|
+
queue.shift();
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
emitter.on(eventName as any, listener);
|
|
186
|
+
signal?.addEventListener('abort', abortHandler, { once: true });
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
while (true) {
|
|
190
|
+
if (signal?.aborted && items.length === 0) break;
|
|
191
|
+
if (items.length === 0) {
|
|
192
|
+
await queue.wait(options);
|
|
193
|
+
}
|
|
194
|
+
if (signal?.aborted && items.length === 0) break;
|
|
195
|
+
|
|
196
|
+
yield items.shift()! as any;
|
|
197
|
+
}
|
|
198
|
+
} finally {
|
|
199
|
+
emitter.off(eventName as any, listener);
|
|
200
|
+
signal?.removeEventListener('abort', abortHandler);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
162
203
|
}
|
package/src/functions/lazy.ts
CHANGED
|
@@ -10,9 +10,7 @@
|
|
|
10
10
|
* const value = lazy(() => computeExpensiveValue());
|
|
11
11
|
* ```
|
|
12
12
|
*/
|
|
13
|
-
// eslint-disable-next-line
|
|
14
13
|
export function lazy<Value>(cb: () => Value): () => Value {
|
|
15
14
|
let defaultValue: Value;
|
|
16
|
-
// eslint-disable-next-line
|
|
17
15
|
return () => (defaultValue ??= cb());
|
|
18
16
|
}
|