@sapphire/event-iterator 1.7.0-pr-601.2f2c308a.0 โ 1.7.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/CHANGELOG.md +55 -0
- package/dist/index.d.mts +78 -0
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -12
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,61 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
# [@sapphire/event-iterator@1.7.0](https://github.com/sapphiredev/utilities/compare/@sapphire/event-iterator@1.6.1...@sapphire/event-iterator@1.7.0) - (2023-09-10)
|
|
6
|
+
|
|
7
|
+
## ๐ Refactor
|
|
8
|
+
|
|
9
|
+
- Split `@sapphire/time-utilities` into 4 sub-packages (#462) ([574299a](https://github.com/sapphiredev/utilities/commit/574299a99e658f6500a2a7efa587a0919b2d1313))
|
|
10
|
+
|
|
11
|
+
## ๐ Bug Fixes
|
|
12
|
+
|
|
13
|
+
- **event-iterator:** Fixed support for `@types/node` `>=v20` ([323a8d0](https://github.com/sapphiredev/utilities/commit/323a8d042dfc2fdc443b2833008d969e62cfeea1))
|
|
14
|
+
- Update export mapping for proper ESM/CJS split ([dd0cff8](https://github.com/sapphiredev/utilities/commit/dd0cff8e9b03a15812f25f7a1180501a92422629))
|
|
15
|
+
- **deps:** Update all non-major dependencies (#607) ([9cc8bd0](https://github.com/sapphiredev/utilities/commit/9cc8bd0d4b5d650deab2c913e6c3d713861bae28))
|
|
16
|
+
- **deps:** Update all non-major dependencies (#577) ([291dd67](https://github.com/sapphiredev/utilities/commit/291dd6783e57d8f075ce566218ba076ef6c4bbbd))
|
|
17
|
+
- **deps:** Update all non-major dependencies (#545) ([40ca040](https://github.com/sapphiredev/utilities/commit/40ca040a21d8a0949682051a3a974538183a400e))
|
|
18
|
+
- **deps:** Update all non-major dependencies (#544) ([cc78f17](https://github.com/sapphiredev/utilities/commit/cc78f17390c7f3db08af92bf46a5a70a9c11dd5f))
|
|
19
|
+
- **deps:** Update all non-major dependencies (#532) ([8033d1f](https://github.com/sapphiredev/utilities/commit/8033d1ff7a5a1974134c61f424f171cccb2915e1))
|
|
20
|
+
- **deps:** Update all non-major dependencies (#514) ([21b07d5](https://github.com/sapphiredev/utilities/commit/21b07d5db529a0d982647a60de98e46f36f1ac93))
|
|
21
|
+
- **deps:** Update all non-major dependencies (#505) ([6178296](https://github.com/sapphiredev/utilities/commit/617829649e1e4deeee02b14533b5377cd5bc1fb3))
|
|
22
|
+
- **deps:** Update all non-major dependencies (#466) ([dc08606](https://github.com/sapphiredev/utilities/commit/dc08606a97154e47c65536123ac5f8b1262f7bd2))
|
|
23
|
+
- **deps:** Update all non-major dependencies ([e20f299](https://github.com/sapphiredev/utilities/commit/e20f29906e83cee000aaba9c6827e3bec5173d28))
|
|
24
|
+
- **deps:** Update all non-major dependencies ([2308bd7](https://github.com/sapphiredev/utilities/commit/2308bd74356b6b2e0c12995b25f4d8ade4803fe9))
|
|
25
|
+
- **deps:** Update all non-major dependencies ([84af0db](https://github.com/sapphiredev/utilities/commit/84af0db2db749223b036aa99fe19a2e9af5681c6))
|
|
26
|
+
- **deps:** Update all non-major dependencies ([50cd8de](https://github.com/sapphiredev/utilities/commit/50cd8dea593b6f5ae75571209456b3421e2ca59a))
|
|
27
|
+
|
|
28
|
+
## ๐ Documentation
|
|
29
|
+
|
|
30
|
+
- Add @06000208 as a contributor ([fa3349e](https://github.com/sapphiredev/utilities/commit/fa3349e55ce4ad008785211dec7bf8e2b5d933df))
|
|
31
|
+
- Add @didinele as a contributor ([42ef7b6](https://github.com/sapphiredev/utilities/commit/42ef7b656c48fd0e720119db1d622c8bba2791e9))
|
|
32
|
+
- Add @goestav as a contributor ([0e56a92](https://github.com/sapphiredev/utilities/commit/0e56a92a4e2d0942bfa207f81a8cb03b32312034))
|
|
33
|
+
- Add @CitTheDev as a contributor ([34169ea](https://github.com/sapphiredev/utilities/commit/34169eae1dc0476ccf5a6c4f36e28602a204829e))
|
|
34
|
+
- Add @legendhimslef as a contributor ([059b6f1](https://github.com/sapphiredev/utilities/commit/059b6f1ab5362d46d58624d06c1aa39192b0716f))
|
|
35
|
+
- Add @r-priyam as a contributor ([fb278ba](https://github.com/sapphiredev/utilities/commit/fb278bacf627ec6fc88752eafeb12df5f3177a2c))
|
|
36
|
+
- Change name of @kyranet (#451) ([df4fdef](https://github.com/sapphiredev/utilities/commit/df4fdefce18659975a4ebc224723638507d02d35))
|
|
37
|
+
- Update @RealShadowNova as a contributor ([a869ba0](https://github.com/sapphiredev/utilities/commit/a869ba0abfad041610b9115187d426aebe671af6))
|
|
38
|
+
- Add @muchnameless as a contributor ([a1221fe](https://github.com/sapphiredev/utilities/commit/a1221fea68506e99591d5d00ec552a07c26833f9))
|
|
39
|
+
- Add @enxg as a contributor ([d2382f0](https://github.com/sapphiredev/utilities/commit/d2382f04e3909cb4ad11798a0a10e683f6cf5383))
|
|
40
|
+
- Add @EvolutionX-10 as a contributor ([efc3a32](https://github.com/sapphiredev/utilities/commit/efc3a320a72ae258996dd62866d206c33f8d4961))
|
|
41
|
+
- Add @MajesticString as a contributor ([295b3e9](https://github.com/sapphiredev/utilities/commit/295b3e9849a4b0fe64074bae02f6426378a303c3))
|
|
42
|
+
- Add @Mzato0001 as a contributor ([c790ef2](https://github.com/sapphiredev/utilities/commit/c790ef25df2d7e22888fa9f8169167aa555e9e19))
|
|
43
|
+
- Add @NotKaskus as a contributor ([00da8f1](https://github.com/sapphiredev/utilities/commit/00da8f199137b9277119823f322d1f2d168d928a))
|
|
44
|
+
- Add @imranbarbhuiya as a contributor ([fb674c2](https://github.com/sapphiredev/utilities/commit/fb674c2c5594d41e71662263553dcb4bac9e37f4))
|
|
45
|
+
- Add @axisiscool as a contributor ([ce1aa31](https://github.com/sapphiredev/utilities/commit/ce1aa316871a88d3663efbdf2a42d3d8dfe6a27f))
|
|
46
|
+
- Add @dhruv-kaushikk as a contributor ([ebbf43f](https://github.com/sapphiredev/utilities/commit/ebbf43f63617daba96e72c50a234bf8b64f6ddc4))
|
|
47
|
+
- Add @Commandtechno as a contributor ([f1d69fa](https://github.com/sapphiredev/utilities/commit/f1d69fabe1ee0abe4be08b19e63dbec03102f7ce))
|
|
48
|
+
- Fix typedoc causing OOM crashes ([63ba41c](https://github.com/sapphiredev/utilities/commit/63ba41c4b6678554b1c7043a22d3296db4f59360))
|
|
49
|
+
|
|
50
|
+
## ๐ Features
|
|
51
|
+
|
|
52
|
+
- **utilities:** Add possibility to import single functions by appending them to the import path. (#454) ([374c145](https://github.com/sapphiredev/utilities/commit/374c145a5dd329cfc1a867ed6720abf408683a88))
|
|
53
|
+
|
|
54
|
+
## ๐งช Testing
|
|
55
|
+
|
|
56
|
+
- Update vitest to coverage v8 ([a4bc6e4](https://github.com/sapphiredev/utilities/commit/a4bc6e4f24ea60143a150ecc76fda6484f172ab9))
|
|
57
|
+
- Cleanup tests ([aec1bb2](https://github.com/sapphiredev/utilities/commit/aec1bb290d0f3c00a1ae4f4c86302ebbb161d348))
|
|
58
|
+
- Migrate to vitest (#380) ([075ec73](https://github.com/sapphiredev/utilities/commit/075ec73c7a8e3374fad3ada612d37eb4ac36ec8d))
|
|
59
|
+
|
|
5
60
|
## [1.6.1](https://github.com/sapphiredev/utilities/compare/@sapphire/event-iterator@1.6.0...@sapphire/event-iterator@1.6.1) (2022-04-01)
|
|
6
61
|
|
|
7
62
|
**Note:** Version bump only for package @sapphire/event-iterator
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A filter for an EventIterator.
|
|
5
|
+
*/
|
|
6
|
+
type EventIteratorFilter<V> = (value: V) => boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Options to be passed to an EventIterator.
|
|
9
|
+
*/
|
|
10
|
+
interface EventIteratorOptions<V> {
|
|
11
|
+
/**
|
|
12
|
+
* The filter.
|
|
13
|
+
*/
|
|
14
|
+
filter?: EventIteratorFilter<V>;
|
|
15
|
+
/**
|
|
16
|
+
* The timeout in ms before ending the EventIterator.
|
|
17
|
+
*/
|
|
18
|
+
idle?: number;
|
|
19
|
+
/**
|
|
20
|
+
* The limit of events that pass the filter to iterate.
|
|
21
|
+
*/
|
|
22
|
+
limit?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* An EventIterator, used for asynchronously iterating over received values.
|
|
26
|
+
*/
|
|
27
|
+
declare class EventIterator<V extends unknown[]> implements AsyncIterableIterator<V> {
|
|
28
|
+
#private;
|
|
29
|
+
/**
|
|
30
|
+
* The emitter to listen to.
|
|
31
|
+
*/
|
|
32
|
+
readonly emitter: EventEmitter;
|
|
33
|
+
/**
|
|
34
|
+
* The event the event iterator is listening for to receive values from.
|
|
35
|
+
*/
|
|
36
|
+
readonly event: string;
|
|
37
|
+
/**
|
|
38
|
+
* The filter used to filter out values.
|
|
39
|
+
*/
|
|
40
|
+
filter: EventIteratorFilter<V>;
|
|
41
|
+
/**
|
|
42
|
+
* @param emitter The event emitter to listen to.
|
|
43
|
+
* @param event The event we're listening for to receives values from.
|
|
44
|
+
* @param limit The amount of values to receive before ending the iterator.
|
|
45
|
+
* @param options Any extra options.
|
|
46
|
+
*/
|
|
47
|
+
constructor(emitter: EventEmitter, event: string, options?: EventIteratorOptions<V>);
|
|
48
|
+
/**
|
|
49
|
+
* Whether or not the EventIterator has ended.
|
|
50
|
+
*/
|
|
51
|
+
get ended(): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Ends the EventIterator.
|
|
54
|
+
*/
|
|
55
|
+
end(): void;
|
|
56
|
+
/**
|
|
57
|
+
* The next value that's received from the EventEmitter.
|
|
58
|
+
*/
|
|
59
|
+
next(): Promise<IteratorResult<V>>;
|
|
60
|
+
/**
|
|
61
|
+
* Handles what happens when you break or return from a loop.
|
|
62
|
+
*/
|
|
63
|
+
return(): Promise<IteratorResult<V>>;
|
|
64
|
+
/**
|
|
65
|
+
* Handles what happens when you encounter an error in a loop.
|
|
66
|
+
*/
|
|
67
|
+
throw(): Promise<IteratorResult<V>>;
|
|
68
|
+
/**
|
|
69
|
+
* The symbol allowing EventIterators to be used in for-await-of loops.
|
|
70
|
+
*/
|
|
71
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<V>;
|
|
72
|
+
/**
|
|
73
|
+
* Pushes a value into the queue.
|
|
74
|
+
*/
|
|
75
|
+
protected push(...value: V): void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { EventIterator, EventIteratorFilter, EventIteratorOptions };
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,7 @@ var __privateWrapper = (obj, member, setter, getter) => ({
|
|
|
36
36
|
|
|
37
37
|
// src/index.ts
|
|
38
38
|
var _ended, _idle, _queue, _passed, _limit, _idleTimer, _push;
|
|
39
|
-
var
|
|
39
|
+
var _EventIterator = class _EventIterator {
|
|
40
40
|
/**
|
|
41
41
|
* @param emitter The event emitter to listen to.
|
|
42
42
|
* @param event The event we're listening for to receives values from.
|
|
@@ -177,7 +177,6 @@ var EventIterator = class {
|
|
|
177
177
|
__privateGet(this, _queue).push(value);
|
|
178
178
|
}
|
|
179
179
|
};
|
|
180
|
-
__name(EventIterator, "EventIterator");
|
|
181
180
|
_ended = new WeakMap();
|
|
182
181
|
_idle = new WeakMap();
|
|
183
182
|
_queue = new WeakMap();
|
|
@@ -185,6 +184,8 @@ _passed = new WeakMap();
|
|
|
185
184
|
_limit = new WeakMap();
|
|
186
185
|
_idleTimer = new WeakMap();
|
|
187
186
|
_push = new WeakMap();
|
|
187
|
+
__name(_EventIterator, "EventIterator");
|
|
188
|
+
var EventIterator = _EventIterator;
|
|
188
189
|
|
|
189
190
|
exports.EventIterator = EventIterator;
|
|
190
191
|
//# sourceMappingURL=out.js.map
|
package/dist/index.js.map
CHAN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AAE,MAAM,MAAM,OAAO,OAAmB,CAAC;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKA,CAAQ,OAAO,aAAa,IAA8B;AACzD,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA,EAKU,QAAQ,OAAgB;AACjC,uBAAK,QAAO,KAAK,KAAK;AAAA,EACvB;AACD;AAnJC;AAKA;AAKA;AAKA;AAKA;AAKA;AAKA;AAjDmF;AAA7E,IAAM,gBAAN","sourcesContent":["import type { EventEmitter } from 'node:events';\n\n/**\n * A filter for an EventIterator.\n */\nexport type EventIteratorFilter<V> = (value: V) => boolean;\n\n/**\n * Options to be passed to an EventIterator.\n */\nexport interface EventIteratorOptions<V> {\n\t/**\n\t * The filter.\n\t */\n\tfilter?: EventIteratorFilter<V>;\n\n\t/**\n\t * The timeout in ms before ending the EventIterator.\n\t */\n\tidle?: number;\n\n\t/**\n\t * The limit of events that pass the filter to iterate.\n\t */\n\tlimit?: number;\n}\n\n/**\n * An EventIterator, used for asynchronously iterating over received values.\n */\nexport class EventIterator<V extends unknown[]> implements AsyncIterableIterator<V> {\n\t/**\n\t * The emitter to listen to.\n\t */\n\tpublic readonly emitter: EventEmitter;\n\n\t/**\n\t * The event the event iterator is listening for to receive values from.\n\t */\n\tpublic readonly event: string;\n\n\t/**\n\t * The filter used to filter out values.\n\t */\n\tpublic filter: EventIteratorFilter<V>;\n\n\t/**\n\t * Whether or not the EventIterator has ended.\n\t */\n\t#ended = false;\n\n\t/**\n\t * The amount of idle time in ms before moving on.\n\t */\n\t#idle?: number;\n\n\t/**\n\t * The queue of received values.\n\t */\n\t#queue: V[] = [];\n\n\t/**\n\t * The amount of events that have passed the filter.\n\t */\n\t#passed = 0;\n\n\t/**\n\t * The limit before ending the EventIterator.\n\t */\n\t#limit: number;\n\n\t/**\n\t * The timer to track when this will idle out.\n\t */\n\t#idleTimer: NodeJS.Timeout | undefined | null = null;\n\n\t/**\n\t * The push handler with context bound to the instance.\n\t */\n\t#push: (this: EventIterator<V>, ...value: V) => void;\n\n\t/**\n\t * @param emitter The event emitter to listen to.\n\t * @param event The event we're listening for to receives values from.\n\t * @param limit The amount of values to receive before ending the iterator.\n\t * @param options Any extra options.\n\t */\n\tpublic constructor(emitter: EventEmitter, event: string, options: EventIteratorOptions<V> = {}) {\n\t\tthis.emitter = emitter;\n\t\tthis.event = event;\n\t\tthis.#limit = options.limit ?? Infinity;\n\t\tthis.#idle = options.idle;\n\t\tthis.filter = options.filter ?? ((): boolean => true);\n\n\t\t// This timer is to idle out on lack of valid responses\n\t\tif (this.#idle) this.#idleTimer = setTimeout(this.end.bind(this), this.#idle);\n\n\t\tthis.#push = this.push.bind(this);\n\t\tconst maxListeners = this.emitter.getMaxListeners();\n\t\tif (maxListeners !== 0) this.emitter.setMaxListeners(maxListeners + 1);\n\n\t\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\n\t\tthis.emitter.on(this.event, this.#push);\n\t}\n\n\t/**\n\t * Whether or not the EventIterator has ended.\n\t */\n\tpublic get ended(): boolean {\n\t\treturn this.#ended;\n\t}\n\n\t/**\n\t * Ends the EventIterator.\n\t */\n\tpublic end(): void {\n\t\tif (this.#ended) return;\n\t\tthis.#ended = true;\n\t\tthis.#queue = [];\n\n\t\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\n\t\tthis.emitter.off(this.event, this.#push);\n\t\tconst maxListeners = this.emitter.getMaxListeners();\n\t\tif (maxListeners !== 0) this.emitter.setMaxListeners(maxListeners - 1);\n\t}\n\n\t/**\n\t * The next value that's received from the EventEmitter.\n\t */\n\tpublic async next(): Promise<IteratorResult<V>> {\n\t\t// If there are elements in the queue, return an undone response:\n\t\tif (this.#queue.length) {\n\t\t\tconst value = this.#queue.shift()!;\n\t\t\tif (!this.filter(value)) return this.next();\n\t\t\tif (++this.#passed >= this.#limit) this.end();\n\t\t\tif (this.#idleTimer) this.#idleTimer.refresh();\n\t\t\treturn { done: false, value };\n\t\t}\n\n\t\t// If the iterator ended, clean-up timer and return a done response:\n\t\tif (this.#ended) {\n\t\t\tif (this.#idleTimer) clearTimeout(this.#idleTimer);\n\t\t\treturn { done: true, value: undefined as never };\n\t\t}\n\n\t\t// Listen for a new element from the emitter:\n\t\treturn new Promise<IteratorResult<V>>((resolve) => {\n\t\t\tlet idleTimer: NodeJS.Timeout | undefined | null = null;\n\n\t\t\t// If there is an idle time set, we will create a temporary timer,\n\t\t\t// which will cause the iterator to end if no new elements are received:\n\t\t\tif (this.#idle) {\n\t\t\t\tidleTimer = setTimeout(() => {\n\t\t\t\t\tthis.end();\n\t\t\t\t\tresolve(this.next());\n\t\t\t\t}, this.#idle);\n\t\t\t}\n\n\t\t\t// Once it has received at least one value, we will clear the timer (if defined),\n\t\t\t// and resolve with the new value:\n\t\t\tthis.emitter.once(this.event, () => {\n\t\t\t\tif (idleTimer) clearTimeout(idleTimer);\n\t\t\t\tresolve(this.next());\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Handles what happens when you break or return from a loop.\n\t */\n\tpublic return(): Promise<IteratorResult<V>> {\n\t\tthis.end();\n\t\treturn Promise.resolve({ done: true, value: undefined as never });\n\t}\n\n\t/**\n\t * Handles what happens when you encounter an error in a loop.\n\t */\n\tpublic throw(): Promise<IteratorResult<V>> {\n\t\tthis.end();\n\t\treturn Promise.resolve({ done: true, value: undefined as never });\n\t}\n\n\t/**\n\t * The symbol allowing EventIterators to be used in for-await-of loops.\n\t */\n\tpublic [Symbol.asyncIterator](): AsyncIterableIterator<V> {\n\t\treturn this;\n\t}\n\n\t/**\n\t * Pushes a value into the queue.\n\t */\n\tprotected push(...value: V): void {\n\t\tthis.#queue.push(value);\n\t}\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -34,7 +34,7 @@ var __privateWrapper = (obj, member, setter, getter) => ({
|
|
|
34
34
|
|
|
35
35
|
// src/index.ts
|
|
36
36
|
var _ended, _idle, _queue, _passed, _limit, _idleTimer, _push;
|
|
37
|
-
var
|
|
37
|
+
var _EventIterator = class _EventIterator {
|
|
38
38
|
/**
|
|
39
39
|
* @param emitter The event emitter to listen to.
|
|
40
40
|
* @param event The event we're listening for to receives values from.
|
|
@@ -175,7 +175,6 @@ var EventIterator = class {
|
|
|
175
175
|
__privateGet(this, _queue).push(value);
|
|
176
176
|
}
|
|
177
177
|
};
|
|
178
|
-
__name(EventIterator, "EventIterator");
|
|
179
178
|
_ended = new WeakMap();
|
|
180
179
|
_idle = new WeakMap();
|
|
181
180
|
_queue = new WeakMap();
|
|
@@ -183,6 +182,8 @@ _passed = new WeakMap();
|
|
|
183
182
|
_limit = new WeakMap();
|
|
184
183
|
_idleTimer = new WeakMap();
|
|
185
184
|
_push = new WeakMap();
|
|
185
|
+
__name(_EventIterator, "EventIterator");
|
|
186
|
+
var EventIterator = _EventIterator;
|
|
186
187
|
|
|
187
188
|
export { EventIterator };
|
|
188
189
|
//# sourceMappingURL=out.js.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AA8BO,IAAM,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AA8BO,IAAM,iBAAN,MAAM,eAAuE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAyD5E,YAAY,SAAuB,OAAe,UAAmC,CAAC,GAAG;AArDhG;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAO;AAKP;AAAA;AAAA;AAAA,+BAAS;AAKT;AAAA;AAAA;AAAA;AAKA;AAAA;AAAA;AAAA,+BAAc,CAAC;AAKf;AAAA;AAAA;AAAA,gCAAU;AAKV;AAAA;AAAA;AAAA;AAKA;AAAA;AAAA;AAAA,mCA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,MAAM,MAAM,OAAO,OAAmB;AAAA,IAChD;AAGA,WAAO,IAAI,QAA2B,CAAC,YAAY;AAClD,UAAI,YAA+C;AAInD,UAAI,mBAAK,QAAO;AACf,oBAAY,WAAW,MAAM;AAC5B,eAAK,IAAI;AACT,kBAAQ,KAAK,KAAK,CAAC;AAAA,QACpB,GAAG,mBAAK,MAAK;AAAA,MACd;AAIA,WAAK,QAAQ,KAAK,KAAK,OAAO,MAAM;AACnC,YAAI;AAAW,uBAAa,SAAS;AACrC,gBAAQ,KAAK,KAAK,CAAC;AAAA,MACpB,CAAC;AAAA,IACF,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKO,SAAqC;AAC3C,SAAK,IAAI;AACT,WAAO,QAAQ,QAAQ,EAAE,MAAM,MAAM,OAAO,OAAmB,CAAC;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKO,QAAoC;AAC1C,SAAK,IAAI;AACT,WAAO,QAAQ,QAAQ,EAAE,MAAM,MAAM,OAAO,OAAmB,CAAC;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKA,CAAQ,OAAO,aAAa,IAA8B;AACzD,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA,EAKU,QAAQ,OAAgB;AACjC,uBAAK,QAAO,KAAK,KAAK;AAAA,EACvB;AACD;AAnJC;AAKA;AAKA;AAKA;AAKA;AAKA;AAKA;AAjDmF;AAA7E,IAAM,gBAAN","sourcesContent":["import type { EventEmitter } from 'node:events';\n\n/**\n * A filter for an EventIterator.\n */\nexport type EventIteratorFilter<V> = (value: V) => boolean;\n\n/**\n * Options to be passed to an EventIterator.\n */\nexport interface EventIteratorOptions<V> {\n\t/**\n\t * The filter.\n\t */\n\tfilter?: EventIteratorFilter<V>;\n\n\t/**\n\t * The timeout in ms before ending the EventIterator.\n\t */\n\tidle?: number;\n\n\t/**\n\t * The limit of events that pass the filter to iterate.\n\t */\n\tlimit?: number;\n}\n\n/**\n * An EventIterator, used for asynchronously iterating over received values.\n */\nexport class EventIterator<V extends unknown[]> implements AsyncIterableIterator<V> {\n\t/**\n\t * The emitter to listen to.\n\t */\n\tpublic readonly emitter: EventEmitter;\n\n\t/**\n\t * The event the event iterator is listening for to receive values from.\n\t */\n\tpublic readonly event: string;\n\n\t/**\n\t * The filter used to filter out values.\n\t */\n\tpublic filter: EventIteratorFilter<V>;\n\n\t/**\n\t * Whether or not the EventIterator has ended.\n\t */\n\t#ended = false;\n\n\t/**\n\t * The amount of idle time in ms before moving on.\n\t */\n\t#idle?: number;\n\n\t/**\n\t * The queue of received values.\n\t */\n\t#queue: V[] = [];\n\n\t/**\n\t * The amount of events that have passed the filter.\n\t */\n\t#passed = 0;\n\n\t/**\n\t * The limit before ending the EventIterator.\n\t */\n\t#limit: number;\n\n\t/**\n\t * The timer to track when this will idle out.\n\t */\n\t#idleTimer: NodeJS.Timeout | undefined | null = null;\n\n\t/**\n\t * The push handler with context bound to the instance.\n\t */\n\t#push: (this: EventIterator<V>, ...value: V) => void;\n\n\t/**\n\t * @param emitter The event emitter to listen to.\n\t * @param event The event we're listening for to receives values from.\n\t * @param limit The amount of values to receive before ending the iterator.\n\t * @param options Any extra options.\n\t */\n\tpublic constructor(emitter: EventEmitter, event: string, options: EventIteratorOptions<V> = {}) {\n\t\tthis.emitter = emitter;\n\t\tthis.event = event;\n\t\tthis.#limit = options.limit ?? Infinity;\n\t\tthis.#idle = options.idle;\n\t\tthis.filter = options.filter ?? ((): boolean => true);\n\n\t\t// This timer is to idle out on lack of valid responses\n\t\tif (this.#idle) this.#idleTimer = setTimeout(this.end.bind(this), this.#idle);\n\n\t\tthis.#push = this.push.bind(this);\n\t\tconst maxListeners = this.emitter.getMaxListeners();\n\t\tif (maxListeners !== 0) this.emitter.setMaxListeners(maxListeners + 1);\n\n\t\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\n\t\tthis.emitter.on(this.event, this.#push);\n\t}\n\n\t/**\n\t * Whether or not the EventIterator has ended.\n\t */\n\tpublic get ended(): boolean {\n\t\treturn this.#ended;\n\t}\n\n\t/**\n\t * Ends the EventIterator.\n\t */\n\tpublic end(): void {\n\t\tif (this.#ended) return;\n\t\tthis.#ended = true;\n\t\tthis.#queue = [];\n\n\t\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\n\t\tthis.emitter.off(this.event, this.#push);\n\t\tconst maxListeners = this.emitter.getMaxListeners();\n\t\tif (maxListeners !== 0) this.emitter.setMaxListeners(maxListeners - 1);\n\t}\n\n\t/**\n\t * The next value that's received from the EventEmitter.\n\t */\n\tpublic async next(): Promise<IteratorResult<V>> {\n\t\t// If there are elements in the queue, return an undone response:\n\t\tif (this.#queue.length) {\n\t\t\tconst value = this.#queue.shift()!;\n\t\t\tif (!this.filter(value)) return this.next();\n\t\t\tif (++this.#passed >= this.#limit) this.end();\n\t\t\tif (this.#idleTimer) this.#idleTimer.refresh();\n\t\t\treturn { done: false, value };\n\t\t}\n\n\t\t// If the iterator ended, clean-up timer and return a done response:\n\t\tif (this.#ended) {\n\t\t\tif (this.#idleTimer) clearTimeout(this.#idleTimer);\n\t\t\treturn { done: true, value: undefined as never };\n\t\t}\n\n\t\t// Listen for a new element from the emitter:\n\t\treturn new Promise<IteratorResult<V>>((resolve) => {\n\t\t\tlet idleTimer: NodeJS.Timeout | undefined | null = null;\n\n\t\t\t// If there is an idle time set, we will create a temporary timer,\n\t\t\t// which will cause the iterator to end if no new elements are received:\n\t\t\tif (this.#idle) {\n\t\t\t\tidleTimer = setTimeout(() => {\n\t\t\t\t\tthis.end();\n\t\t\t\t\tresolve(this.next());\n\t\t\t\t}, this.#idle);\n\t\t\t}\n\n\t\t\t// Once it has received at least one value, we will clear the timer (if defined),\n\t\t\t// and resolve with the new value:\n\t\t\tthis.emitter.once(this.event, () => {\n\t\t\t\tif (idleTimer) clearTimeout(idleTimer);\n\t\t\t\tresolve(this.next());\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Handles what happens when you break or return from a loop.\n\t */\n\tpublic return(): Promise<IteratorResult<V>> {\n\t\tthis.end();\n\t\treturn Promise.resolve({ done: true, value: undefined as never });\n\t}\n\n\t/**\n\t * Handles what happens when you encounter an error in a loop.\n\t */\n\tpublic throw(): Promise<IteratorResult<V>> {\n\t\tthis.end();\n\t\treturn Promise.resolve({ done: true, value: undefined as never });\n\t}\n\n\t/**\n\t * The symbol allowing EventIterators to be used in for-await-of loops.\n\t */\n\tpublic [Symbol.asyncIterator](): AsyncIterableIterator<V> {\n\t\treturn this;\n\t}\n\n\t/**\n\t * Pushes a value into the queue.\n\t */\n\tprotected push(...value: V): void {\n\t\tthis.#queue.push(value);\n\t}\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/event-iterator",
|
|
3
|
-
"version": "1.7.0
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Turns event emitter events into async iterators.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,9 +8,14 @@
|
|
|
8
8
|
"module": "dist/index.mjs",
|
|
9
9
|
"types": "dist/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
|
-
"import":
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
14
19
|
},
|
|
15
20
|
"sideEffects": false,
|
|
16
21
|
"homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/event-iterator",
|
|
@@ -24,7 +29,7 @@
|
|
|
24
29
|
"check-update": "cliff-jumper --dry-run"
|
|
25
30
|
},
|
|
26
31
|
"dependencies": {
|
|
27
|
-
"tslib": "^2.
|
|
32
|
+
"tslib": "^2.6.2"
|
|
28
33
|
},
|
|
29
34
|
"repository": {
|
|
30
35
|
"type": "git",
|
|
@@ -57,12 +62,12 @@
|
|
|
57
62
|
"access": "public"
|
|
58
63
|
},
|
|
59
64
|
"devDependencies": {
|
|
60
|
-
"@favware/cliff-jumper": "^2.
|
|
61
|
-
"@vitest/coverage-
|
|
62
|
-
"tsup": "^
|
|
63
|
-
"typedoc": "^0.
|
|
64
|
-
"typedoc-json-parser": "^
|
|
65
|
-
"typescript": "^5.
|
|
66
|
-
"vitest": "^0.
|
|
65
|
+
"@favware/cliff-jumper": "^2.1.2",
|
|
66
|
+
"@vitest/coverage-v8": "^0.34.4",
|
|
67
|
+
"tsup": "^7.2.0",
|
|
68
|
+
"typedoc": "^0.25.1",
|
|
69
|
+
"typedoc-json-parser": "^9.0.0",
|
|
70
|
+
"typescript": "^5.2.2",
|
|
71
|
+
"vitest": "^0.34.4"
|
|
67
72
|
}
|
|
68
73
|
}
|