@sapphire/event-iterator 1.3.3 → 1.4.0-next.f178b6d.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
@@ -116,7 +116,10 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
116
116
  <td align="center"><a href="https://github.com/nandhagk"><img src="https://avatars.githubusercontent.com/u/62976649?v=4?s=100" width="100px;" alt=""/><br /><sub><b>nandhagk</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/issues?q=author%3Anandhagk" title="Bug reports">🐛</a></td>
117
117
  <td align="center"><a href="https://megatank58.me/"><img src="https://avatars.githubusercontent.com/u/51410502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Megatank58</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Megatank58" title="Code">💻</a></td>
118
118
  <td align="center"><a href="https://github.com/UndiedGamer"><img src="https://avatars.githubusercontent.com/u/84702365?v=4?s=100" width="100px;" alt=""/><br /><sub><b>UndiedGamer</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=UndiedGamer" title="Code">💻</a></td>
119
- <td align="center"><a href="https://github.com/Lioness100"><img src="https://avatars.githubusercontent.com/u/65814829?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lioness100</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Lioness100" title="Documentation">📖</a></td>
119
+ <td align="center"><a href="https://github.com/Lioness100"><img src="https://avatars.githubusercontent.com/u/65814829?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lioness100</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Lioness100" title="Documentation">📖</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Lioness100" title="Code">💻</a></td>
120
+ </tr>
121
+ <tr>
122
+ <td align="center"><a href="https://gitlab.com/DavidPH/"><img src="https://avatars.githubusercontent.com/u/44669930?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=DavidPHH" title="Code">💻</a></td>
120
123
  </tr>
121
124
  </table>
122
125
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import type { EventEmitter } from 'events';
2
+ import type { EventEmitter } from 'node:events';
3
3
  /**
4
4
  * A filter for an EventIterator.
5
5
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAE3C;;GAEG;AACH,oBAAY,mBAAmB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACtC;;OAEG;IACH,MAAM,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC;IAEhC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,qBAAa,aAAa,CAAC,CAAC,SAAS,OAAO,EAAE,CAAE,YAAW,qBAAqB,CAAC,CAAC,CAAC;;IAClF;;OAEG;IACH,SAAgB,OAAO,EAAE,YAAY,CAAC;IAEtC;;OAEG;IACH,SAAgB,KAAK,EAAE,MAAM,CAAC;IAE9B;;OAEG;IACI,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC;IAqCtC;;;;;OAKG;gBACgB,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,oBAAoB,CAAC,CAAC,CAAM;IAmB9F;;OAEG;IACH,IAAW,KAAK,IAAI,OAAO,CAE1B;IAED;;OAEG;IACI,GAAG,IAAI,IAAI;IAWlB;;OAEG;IACU,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAs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U,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAsC/C;;OAEG;IACI,MAAM,IAAI,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAK3C;;OAEG;IACI,KAAK,IAAI,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAK1C;;OAEG;IACI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,qBAAqB,CAAC,CAAC,CAAC;IAIzD;;OAEG;IACH,SAAS,CAAC,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,IAAI;CAGjC"}
package/dist/index.js CHANGED
@@ -1,38 +1,8 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- /*! *****************************************************************************
6
- Copyright (c) Microsoft Corporation.
7
-
8
- Permission to use, copy, modify, and/or distribute this software for any
9
- purpose with or without fee is hereby granted.
10
-
11
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
- PERFORMANCE OF THIS SOFTWARE.
18
- ***************************************************************************** */
19
-
20
- function __classPrivateFieldGet(receiver, privateMap) {
21
- if (!privateMap.has(receiver)) {
22
- throw new TypeError("attempted to get private field on non-instance");
23
- }
24
- return privateMap.get(receiver);
25
- }
26
-
27
- function __classPrivateFieldSet(receiver, privateMap, value) {
28
- if (!privateMap.has(receiver)) {
29
- throw new TypeError("attempted to set private field on non-instance");
30
- }
31
- privateMap.set(receiver, value);
32
- return value;
33
- }
34
-
1
+ "use strict";
35
2
  var _EventIterator_ended, _EventIterator_idle, _EventIterator_queue, _EventIterator_passed, _EventIterator_limit, _EventIterator_idleTimer, _EventIterator_push;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.EventIterator = void 0;
5
+ const tslib_1 = require("tslib");
36
6
  /**
37
7
  * An EventIterator, used for asynchronously iterating over received values.
38
8
  */
@@ -44,33 +14,6 @@ class EventIterator {
44
14
  * @param options Any extra options.
45
15
  */
46
16
  constructor(emitter, event, options = {}) {
47
- /**
48
- * The emitter to listen to.
49
- */
50
- Object.defineProperty(this, "emitter", {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: void 0
55
- });
56
- /**
57
- * The event the event iterator is listening for to receive values from.
58
- */
59
- Object.defineProperty(this, "event", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: void 0
64
- });
65
- /**
66
- * The filter used to filter out values.
67
- */
68
- Object.defineProperty(this, "filter", {
69
- enumerable: true,
70
- configurable: true,
71
- writable: true,
72
- value: void 0
73
- });
74
17
  /**
75
18
  * Whether or not the EventIterator has ended.
76
19
  */
@@ -101,36 +44,36 @@ class EventIterator {
101
44
  _EventIterator_push.set(this, void 0);
102
45
  this.emitter = emitter;
103
46
  this.event = event;
104
- __classPrivateFieldSet(this, _EventIterator_limit, options.limit ?? Infinity);
105
- __classPrivateFieldSet(this, _EventIterator_idle, options.idle);
47
+ (0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_limit, options.limit ?? Infinity, "f");
48
+ (0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_idle, options.idle, "f");
106
49
  this.filter = options.filter ?? (() => true);
107
50
  // This timer is to idle out on lack of valid responses
108
- if (__classPrivateFieldGet(this, _EventIterator_idle))
109
- __classPrivateFieldSet(this, _EventIterator_idleTimer, setTimeout(this.end.bind(this), __classPrivateFieldGet(this, _EventIterator_idle)));
51
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idle, "f"))
52
+ (0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_idleTimer, setTimeout(this.end.bind(this), (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idle, "f")), "f");
110
53
  // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
111
- __classPrivateFieldSet(this, _EventIterator_push, this.push.bind(this));
54
+ (0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_push, this.push.bind(this), "f");
112
55
  const maxListeners = this.emitter.getMaxListeners();
113
56
  if (maxListeners !== 0)
114
57
  this.emitter.setMaxListeners(maxListeners + 1);
115
58
  // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
116
- this.emitter.on(this.event, __classPrivateFieldGet(this, _EventIterator_push));
59
+ this.emitter.on(this.event, (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_push, "f"));
117
60
  }
118
61
  /**
119
62
  * Whether or not the EventIterator has ended.
120
63
  */
121
64
  get ended() {
122
- return __classPrivateFieldGet(this, _EventIterator_ended);
65
+ return (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_ended, "f");
123
66
  }
124
67
  /**
125
68
  * Ends the EventIterator.
126
69
  */
127
70
  end() {
128
- if (__classPrivateFieldGet(this, _EventIterator_ended))
71
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_ended, "f"))
129
72
  return;
130
- __classPrivateFieldSet(this, _EventIterator_ended, true);
131
- __classPrivateFieldSet(this, _EventIterator_queue, []);
73
+ (0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_ended, true, "f");
74
+ (0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_queue, [], "f");
132
75
  // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
133
- this.emitter.off(this.event, __classPrivateFieldGet(this, _EventIterator_push));
76
+ this.emitter.off(this.event, (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_push, "f"));
134
77
  const maxListeners = this.emitter.getMaxListeners();
135
78
  if (maxListeners !== 0)
136
79
  this.emitter.setMaxListeners(maxListeners - 1);
@@ -141,20 +84,20 @@ class EventIterator {
141
84
  async next() {
142
85
  var _a;
143
86
  // If there are elements in the queue, return an undone response:
144
- if (__classPrivateFieldGet(this, _EventIterator_queue).length) {
145
- const value = __classPrivateFieldGet(this, _EventIterator_queue).shift();
87
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_queue, "f").length) {
88
+ const value = (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_queue, "f").shift();
146
89
  if (!this.filter(value))
147
90
  return this.next();
148
- if (__classPrivateFieldSet(this, _EventIterator_passed, (_a = __classPrivateFieldGet(this, _EventIterator_passed), ++_a)) >= __classPrivateFieldGet(this, _EventIterator_limit))
91
+ if ((0, tslib_1.__classPrivateFieldSet)(this, _EventIterator_passed, (_a = (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_passed, "f"), ++_a), "f") >= (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_limit, "f"))
149
92
  this.end();
150
- if (__classPrivateFieldGet(this, _EventIterator_idleTimer))
151
- __classPrivateFieldGet(this, _EventIterator_idleTimer).refresh();
93
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idleTimer, "f"))
94
+ (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idleTimer, "f").refresh();
152
95
  return { done: false, value };
153
96
  }
154
97
  // If the iterator ended, clean-up timer and return a done response:
155
- if (__classPrivateFieldGet(this, _EventIterator_ended)) {
156
- if (__classPrivateFieldGet(this, _EventIterator_idleTimer))
157
- clearTimeout(__classPrivateFieldGet(this, _EventIterator_idleTimer));
98
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_ended, "f")) {
99
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idleTimer, "f"))
100
+ clearTimeout((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idleTimer, "f"));
158
101
  return { done: true, value: undefined };
159
102
  }
160
103
  // Listen for a new element from the emitter:
@@ -162,11 +105,11 @@ class EventIterator {
162
105
  let idleTimer = null;
163
106
  // If there is an idle time set, we will create a temporary timer,
164
107
  // which will cause the iterator to end if no new elements are received:
165
- if (__classPrivateFieldGet(this, _EventIterator_idle)) {
108
+ if ((0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idle, "f")) {
166
109
  idleTimer = setTimeout(() => {
167
110
  this.end();
168
111
  resolve(this.next());
169
- }, __classPrivateFieldGet(this, _EventIterator_idle));
112
+ }, (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_idle, "f"));
170
113
  }
171
114
  // Once it has received at least one value, we will clear the timer (if defined),
172
115
  // and resolve with the new value:
@@ -201,9 +144,8 @@ class EventIterator {
201
144
  * Pushes a value into the queue.
202
145
  */
203
146
  push(...value) {
204
- __classPrivateFieldGet(this, _EventIterator_queue).push(value);
147
+ (0, tslib_1.__classPrivateFieldGet)(this, _EventIterator_queue, "f").push(value);
205
148
  }
206
149
  }
207
-
208
150
  exports.EventIterator = EventIterator;
209
- //# sourceMappingURL=index.js.map
151
+ //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\nimport type { EventEmitter } from '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.Timer | 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\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\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.Timer | 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"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;MAGa,aAAa;;;;;;;IAyDzB,YAAmB,OAAqB,EAAE,KAAa,EAAE,UAAmC,EAAE;;;;QArD9F;;;;;WAAsC;;;;QAKtC;;;;;WAA8B;;;;QAK9B;;;;;WAAsC;;;;QAKtC,+BAAS,KAAK,EAAC;;;;QAKf,sCAAe;;;;QAKf,+BAAc,EAAE,EAAC;;;;QAKjB,gCAAU,CAAC,EAAC;;;;QAKZ,uCAAe;;;;QAKf,mCAAkC,IAAI,EAAC;;;;QAKvC,sCAAqD;QASpD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,uBAAA,IAAI,wBAAU,OAAO,CAAC,KAAK,IAAI,SAAQ,CAAC;QACxC,uBAAA,IAAI,uBAAS,OAAO,CAAC,KAAI,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,KAAK,MAAe,IAAI,CAAC,CAAC;;QAGtD,IAAI,uBAAA,IAAI,sBAAM;YAAE,uBAAA,IAAI,4BAAc,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,uBAAA,IAAI,sBAAM,EAAC,CAAC;;QAG9E,uBAAA,I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5B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;OAEG;IACO,IAAI,CAAC,GAAG,KAAQ;QACzB,oCAAA,IAAI,4BAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;CACD;AAvKD,sCAuKC"}
package/dist/index.mjs CHANGED
@@ -1,205 +1,4 @@
1
- /*! *****************************************************************************
2
- Copyright (c) Microsoft Corporation.
3
-
4
- Permission to use, copy, modify, and/or distribute this software for any
5
- purpose with or without fee is hereby granted.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
- PERFORMANCE OF THIS SOFTWARE.
14
- ***************************************************************************** */
15
-
16
- function __classPrivateFieldGet(receiver, privateMap) {
17
- if (!privateMap.has(receiver)) {
18
- throw new TypeError("attempted to get private field on non-instance");
19
- }
20
- return privateMap.get(receiver);
21
- }
22
-
23
- function __classPrivateFieldSet(receiver, privateMap, value) {
24
- if (!privateMap.has(receiver)) {
25
- throw new TypeError("attempted to set private field on non-instance");
26
- }
27
- privateMap.set(receiver, value);
28
- return value;
29
- }
1
+ import mod from "./index.js";
30
2
 
31
- var _EventIterator_ended, _EventIterator_idle, _EventIterator_queue, _EventIterator_passed, _EventIterator_limit, _EventIterator_idleTimer, _EventIterator_push;
32
- /**
33
- * An EventIterator, used for asynchronously iterating over received values.
34
- */
35
- class EventIterator {
36
- /**
37
- * @param emitter The event emitter to listen to.
38
- * @param event The event we're listening for to receives values from.
39
- * @param limit The amount of values to receive before ending the iterator.
40
- * @param options Any extra options.
41
- */
42
- constructor(emitter, event, options = {}) {
43
- /**
44
- * The emitter to listen to.
45
- */
46
- Object.defineProperty(this, "emitter", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: void 0
51
- });
52
- /**
53
- * The event the event iterator is listening for to receive values from.
54
- */
55
- Object.defineProperty(this, "event", {
56
- enumerable: true,
57
- configurable: true,
58
- writable: true,
59
- value: void 0
60
- });
61
- /**
62
- * The filter used to filter out values.
63
- */
64
- Object.defineProperty(this, "filter", {
65
- enumerable: true,
66
- configurable: true,
67
- writable: true,
68
- value: void 0
69
- });
70
- /**
71
- * Whether or not the EventIterator has ended.
72
- */
73
- _EventIterator_ended.set(this, false);
74
- /**
75
- * The amount of idle time in ms before moving on.
76
- */
77
- _EventIterator_idle.set(this, void 0);
78
- /**
79
- * The queue of received values.
80
- */
81
- _EventIterator_queue.set(this, []);
82
- /**
83
- * The amount of events that have passed the filter.
84
- */
85
- _EventIterator_passed.set(this, 0);
86
- /**
87
- * The limit before ending the EventIterator.
88
- */
89
- _EventIterator_limit.set(this, void 0);
90
- /**
91
- * The timer to track when this will idle out.
92
- */
93
- _EventIterator_idleTimer.set(this, null);
94
- /**
95
- * The push handler with context bound to the instance.
96
- */
97
- _EventIterator_push.set(this, void 0);
98
- this.emitter = emitter;
99
- this.event = event;
100
- __classPrivateFieldSet(this, _EventIterator_limit, options.limit ?? Infinity);
101
- __classPrivateFieldSet(this, _EventIterator_idle, options.idle);
102
- this.filter = options.filter ?? (() => true);
103
- // This timer is to idle out on lack of valid responses
104
- if (__classPrivateFieldGet(this, _EventIterator_idle))
105
- __classPrivateFieldSet(this, _EventIterator_idleTimer, setTimeout(this.end.bind(this), __classPrivateFieldGet(this, _EventIterator_idle)));
106
- // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
107
- __classPrivateFieldSet(this, _EventIterator_push, this.push.bind(this));
108
- const maxListeners = this.emitter.getMaxListeners();
109
- if (maxListeners !== 0)
110
- this.emitter.setMaxListeners(maxListeners + 1);
111
- // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
112
- this.emitter.on(this.event, __classPrivateFieldGet(this, _EventIterator_push));
113
- }
114
- /**
115
- * Whether or not the EventIterator has ended.
116
- */
117
- get ended() {
118
- return __classPrivateFieldGet(this, _EventIterator_ended);
119
- }
120
- /**
121
- * Ends the EventIterator.
122
- */
123
- end() {
124
- if (__classPrivateFieldGet(this, _EventIterator_ended))
125
- return;
126
- __classPrivateFieldSet(this, _EventIterator_ended, true);
127
- __classPrivateFieldSet(this, _EventIterator_queue, []);
128
- // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
129
- this.emitter.off(this.event, __classPrivateFieldGet(this, _EventIterator_push));
130
- const maxListeners = this.emitter.getMaxListeners();
131
- if (maxListeners !== 0)
132
- this.emitter.setMaxListeners(maxListeners - 1);
133
- }
134
- /**
135
- * The next value that's received from the EventEmitter.
136
- */
137
- async next() {
138
- var _a;
139
- // If there are elements in the queue, return an undone response:
140
- if (__classPrivateFieldGet(this, _EventIterator_queue).length) {
141
- const value = __classPrivateFieldGet(this, _EventIterator_queue).shift();
142
- if (!this.filter(value))
143
- return this.next();
144
- if (__classPrivateFieldSet(this, _EventIterator_passed, (_a = __classPrivateFieldGet(this, _EventIterator_passed), ++_a)) >= __classPrivateFieldGet(this, _EventIterator_limit))
145
- this.end();
146
- if (__classPrivateFieldGet(this, _EventIterator_idleTimer))
147
- __classPrivateFieldGet(this, _EventIterator_idleTimer).refresh();
148
- return { done: false, value };
149
- }
150
- // If the iterator ended, clean-up timer and return a done response:
151
- if (__classPrivateFieldGet(this, _EventIterator_ended)) {
152
- if (__classPrivateFieldGet(this, _EventIterator_idleTimer))
153
- clearTimeout(__classPrivateFieldGet(this, _EventIterator_idleTimer));
154
- return { done: true, value: undefined };
155
- }
156
- // Listen for a new element from the emitter:
157
- return new Promise((resolve) => {
158
- let idleTimer = null;
159
- // If there is an idle time set, we will create a temporary timer,
160
- // which will cause the iterator to end if no new elements are received:
161
- if (__classPrivateFieldGet(this, _EventIterator_idle)) {
162
- idleTimer = setTimeout(() => {
163
- this.end();
164
- resolve(this.next());
165
- }, __classPrivateFieldGet(this, _EventIterator_idle));
166
- }
167
- // Once it has received at least one value, we will clear the timer (if defined),
168
- // and resolve with the new value:
169
- this.emitter.once(this.event, () => {
170
- if (idleTimer)
171
- clearTimeout(idleTimer);
172
- resolve(this.next());
173
- });
174
- });
175
- }
176
- /**
177
- * Handles what happens when you break or return from a loop.
178
- */
179
- return() {
180
- this.end();
181
- return Promise.resolve({ done: true, value: undefined });
182
- }
183
- /**
184
- * Handles what happens when you encounter an error in a loop.
185
- */
186
- throw() {
187
- this.end();
188
- return Promise.resolve({ done: true, value: undefined });
189
- }
190
- /**
191
- * The symbol allowing EventIterators to be used in for-await-of loops.
192
- */
193
- [(_EventIterator_ended = new WeakMap(), _EventIterator_idle = new WeakMap(), _EventIterator_queue = new WeakMap(), _EventIterator_passed = new WeakMap(), _EventIterator_limit = new WeakMap(), _EventIterator_idleTimer = new WeakMap(), _EventIterator_push = new WeakMap(), Symbol.asyncIterator)]() {
194
- return this;
195
- }
196
- /**
197
- * Pushes a value into the queue.
198
- */
199
- push(...value) {
200
- __classPrivateFieldGet(this, _EventIterator_queue).push(value);
201
- }
202
- }
203
-
204
- export { EventIterator };
205
- //# sourceMappingURL=index.mjs.map
3
+ export default mod;
4
+ export const EventIterator = mod.EventIterator;
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "@sapphire/event-iterator",
3
- "version": "1.3.3",
3
+ "version": "1.4.0-next.f178b6d.0",
4
4
  "description": "Turns event emitter events into async iterators.",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.mjs",
9
- "browser": "dist/index.umd.js",
10
- "unpkg": "dist/index.umd.js",
11
9
  "types": "dist/index.d.ts",
12
10
  "typedocMain": "src/index.ts",
13
11
  "exports": {
@@ -19,10 +17,13 @@
19
17
  "scripts": {
20
18
  "test": "jest",
21
19
  "lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
22
- "build": "rollup -c",
20
+ "build": "tsc -b src && gen-esm-wrapper dist/index.js dist/index.mjs",
23
21
  "start": "yarn build -w",
24
22
  "prepublishOnly": "yarn build"
25
23
  },
24
+ "dependencies": {
25
+ "tslib": "^2.3.1"
26
+ },
26
27
  "repository": {
27
28
  "type": "git",
28
29
  "url": "git+https://github.com/sapphiredev/utilities.git",
@@ -52,5 +53,5 @@
52
53
  "publishConfig": {
53
54
  "access": "public"
54
55
  },
55
- "gitHead": "4889ea35a0b081021981ff3d73e5005dd1e922ce"
56
+ "gitHead": "f178b6dcc93f17b030b4af188ec388fe49e12f48"
56
57
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\nimport type { EventEmitter } from '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.Timer | 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\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\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.Timer | 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"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;MAGa,aAAa;;;;;;;IAyDzB,YAAmB,OAAqB,EAAE,KAAa,EAAE,UAAmC,EAAE;;;;QArD9F;;;;;WAAsC;;;;QAKtC;;;;;WAA8B;;;;QAK9B;;;;;WAAsC;;;;QAKtC,+BAAS,KAAK,EAAC;;;;QAKf,sCAAe;;;;QAKf,+BAAc,EAAE,EAAC;;;;QAKjB,gCAAU,CAAC,EAAC;;;;QAKZ,uCAAe;;;;QAKf,mCAAkC,IAAI,EAAC;;;;QAKvC,sCAAqD;QASpD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,uBAAA,IAAI,wBAAU,OAAO,CAAC,KAAK,IAAI,SAAQ,CAAC;QACxC,uBAAA,IAAI,uBAAS,OAAO,CAAC,KAAI,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,KAAK,MAAe,IAAI,CAAC,CAAC;;QAGtD,IAAI,uBAAA,IAAI,sBAAM;YAAE,uBAAA,IAAI,4BAAc,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,uBAAA,IAAI,sBAAM,EAAC,CAAC;;QAG9E,uBAAA,IAAI,uBAAS,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAC,CAAC;QAClC,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC;QACpD,IAAI,YAAY,KAAK,CAAC;YAAE,IAAI,CAAC,OAAO,CAAC,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AI,2BAAW,CAAC,CAAC;YACnD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAkB,EAAE,CAAC;SACjD;;QAGD,OAAO,IAAI,OAAO,CAAoB,CAAC,OAAO;YAC7C,IAAI,SAAS,GAAwB,IAAI,CAAC;;;YAI1C,IAAI,uBAAA,IAAI,sBAAM,EAAE;gBACf,SAAS,GAAG,UAAU,CAAC;oBACtB,IAAI,CAAC,GAAG,EAAE,CAAC;oBACX,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;iBACrB,EAAE,uBAAA,IAAI,sBAAM,CAAC,CAAC;aACf;;;YAID,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;gBAC7B,IAAI,SAAS;oBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;gBACvC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;aACrB,CAAC,CAAC;SACH,CAAC,CAAC;KACH;;;;IAKM,MAAM;QACZ,IAAI,CAAC,GAAG,EAAE,CAAC;QACX,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAkB,EAAE,CAAC,CAAC;KAClE;;;;IAKM,KAAK;QACX,IAAI,CAAC,GAAG,EAAE,CAAC;QACX,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAkB,EAAE,CAAC,CAAC;KAClE;;;;IAKM,+QAAC,MAAM,CAAC,aAAa,EAAC;QAC5B,OAAO,IAAI,CAAC;KACZ;;;;IAKS,IAAI,CAAC,GAAG,KAAQ;QACzB,uBAAA,IAAI,uBAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;KACxB;;;;;"}
package/dist/index.umd.js DELETED
@@ -1,215 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SapphireEventIterator = {}));
5
- })(this, (function (exports) { 'use strict';
6
-
7
- /*! *****************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
-
22
- function __classPrivateFieldGet(receiver, privateMap) {
23
- if (!privateMap.has(receiver)) {
24
- throw new TypeError("attempted to get private field on non-instance");
25
- }
26
- return privateMap.get(receiver);
27
- }
28
-
29
- function __classPrivateFieldSet(receiver, privateMap, value) {
30
- if (!privateMap.has(receiver)) {
31
- throw new TypeError("attempted to set private field on non-instance");
32
- }
33
- privateMap.set(receiver, value);
34
- return value;
35
- }
36
-
37
- var _EventIterator_ended, _EventIterator_idle, _EventIterator_queue, _EventIterator_passed, _EventIterator_limit, _EventIterator_idleTimer, _EventIterator_push;
38
- /**
39
- * An EventIterator, used for asynchronously iterating over received values.
40
- */
41
- class EventIterator {
42
- /**
43
- * @param emitter The event emitter to listen to.
44
- * @param event The event we're listening for to receives values from.
45
- * @param limit The amount of values to receive before ending the iterator.
46
- * @param options Any extra options.
47
- */
48
- constructor(emitter, event, options = {}) {
49
- /**
50
- * The emitter to listen to.
51
- */
52
- Object.defineProperty(this, "emitter", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: void 0
57
- });
58
- /**
59
- * The event the event iterator is listening for to receive values from.
60
- */
61
- Object.defineProperty(this, "event", {
62
- enumerable: true,
63
- configurable: true,
64
- writable: true,
65
- value: void 0
66
- });
67
- /**
68
- * The filter used to filter out values.
69
- */
70
- Object.defineProperty(this, "filter", {
71
- enumerable: true,
72
- configurable: true,
73
- writable: true,
74
- value: void 0
75
- });
76
- /**
77
- * Whether or not the EventIterator has ended.
78
- */
79
- _EventIterator_ended.set(this, false);
80
- /**
81
- * The amount of idle time in ms before moving on.
82
- */
83
- _EventIterator_idle.set(this, void 0);
84
- /**
85
- * The queue of received values.
86
- */
87
- _EventIterator_queue.set(this, []);
88
- /**
89
- * The amount of events that have passed the filter.
90
- */
91
- _EventIterator_passed.set(this, 0);
92
- /**
93
- * The limit before ending the EventIterator.
94
- */
95
- _EventIterator_limit.set(this, void 0);
96
- /**
97
- * The timer to track when this will idle out.
98
- */
99
- _EventIterator_idleTimer.set(this, null);
100
- /**
101
- * The push handler with context bound to the instance.
102
- */
103
- _EventIterator_push.set(this, void 0);
104
- this.emitter = emitter;
105
- this.event = event;
106
- __classPrivateFieldSet(this, _EventIterator_limit, options.limit ?? Infinity);
107
- __classPrivateFieldSet(this, _EventIterator_idle, options.idle);
108
- this.filter = options.filter ?? (() => true);
109
- // This timer is to idle out on lack of valid responses
110
- if (__classPrivateFieldGet(this, _EventIterator_idle))
111
- __classPrivateFieldSet(this, _EventIterator_idleTimer, setTimeout(this.end.bind(this), __classPrivateFieldGet(this, _EventIterator_idle)));
112
- // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
113
- __classPrivateFieldSet(this, _EventIterator_push, this.push.bind(this));
114
- const maxListeners = this.emitter.getMaxListeners();
115
- if (maxListeners !== 0)
116
- this.emitter.setMaxListeners(maxListeners + 1);
117
- // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
118
- this.emitter.on(this.event, __classPrivateFieldGet(this, _EventIterator_push));
119
- }
120
- /**
121
- * Whether or not the EventIterator has ended.
122
- */
123
- get ended() {
124
- return __classPrivateFieldGet(this, _EventIterator_ended);
125
- }
126
- /**
127
- * Ends the EventIterator.
128
- */
129
- end() {
130
- if (__classPrivateFieldGet(this, _EventIterator_ended))
131
- return;
132
- __classPrivateFieldSet(this, _EventIterator_ended, true);
133
- __classPrivateFieldSet(this, _EventIterator_queue, []);
134
- // @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.
135
- this.emitter.off(this.event, __classPrivateFieldGet(this, _EventIterator_push));
136
- const maxListeners = this.emitter.getMaxListeners();
137
- if (maxListeners !== 0)
138
- this.emitter.setMaxListeners(maxListeners - 1);
139
- }
140
- /**
141
- * The next value that's received from the EventEmitter.
142
- */
143
- async next() {
144
- var _a;
145
- // If there are elements in the queue, return an undone response:
146
- if (__classPrivateFieldGet(this, _EventIterator_queue).length) {
147
- const value = __classPrivateFieldGet(this, _EventIterator_queue).shift();
148
- if (!this.filter(value))
149
- return this.next();
150
- if (__classPrivateFieldSet(this, _EventIterator_passed, (_a = __classPrivateFieldGet(this, _EventIterator_passed), ++_a)) >= __classPrivateFieldGet(this, _EventIterator_limit))
151
- this.end();
152
- if (__classPrivateFieldGet(this, _EventIterator_idleTimer))
153
- __classPrivateFieldGet(this, _EventIterator_idleTimer).refresh();
154
- return { done: false, value };
155
- }
156
- // If the iterator ended, clean-up timer and return a done response:
157
- if (__classPrivateFieldGet(this, _EventIterator_ended)) {
158
- if (__classPrivateFieldGet(this, _EventIterator_idleTimer))
159
- clearTimeout(__classPrivateFieldGet(this, _EventIterator_idleTimer));
160
- return { done: true, value: undefined };
161
- }
162
- // Listen for a new element from the emitter:
163
- return new Promise((resolve) => {
164
- let idleTimer = null;
165
- // If there is an idle time set, we will create a temporary timer,
166
- // which will cause the iterator to end if no new elements are received:
167
- if (__classPrivateFieldGet(this, _EventIterator_idle)) {
168
- idleTimer = setTimeout(() => {
169
- this.end();
170
- resolve(this.next());
171
- }, __classPrivateFieldGet(this, _EventIterator_idle));
172
- }
173
- // Once it has received at least one value, we will clear the timer (if defined),
174
- // and resolve with the new value:
175
- this.emitter.once(this.event, () => {
176
- if (idleTimer)
177
- clearTimeout(idleTimer);
178
- resolve(this.next());
179
- });
180
- });
181
- }
182
- /**
183
- * Handles what happens when you break or return from a loop.
184
- */
185
- return() {
186
- this.end();
187
- return Promise.resolve({ done: true, value: undefined });
188
- }
189
- /**
190
- * Handles what happens when you encounter an error in a loop.
191
- */
192
- throw() {
193
- this.end();
194
- return Promise.resolve({ done: true, value: undefined });
195
- }
196
- /**
197
- * The symbol allowing EventIterators to be used in for-await-of loops.
198
- */
199
- [(_EventIterator_ended = new WeakMap(), _EventIterator_idle = new WeakMap(), _EventIterator_queue = new WeakMap(), _EventIterator_passed = new WeakMap(), _EventIterator_limit = new WeakMap(), _EventIterator_idleTimer = new WeakMap(), _EventIterator_push = new WeakMap(), Symbol.asyncIterator)]() {
200
- return this;
201
- }
202
- /**
203
- * Pushes a value into the queue.
204
- */
205
- push(...value) {
206
- __classPrivateFieldGet(this, _EventIterator_queue).push(value);
207
- }
208
- }
209
-
210
- exports.EventIterator = EventIterator;
211
-
212
- Object.defineProperty(exports, '__esModule', { value: true });
213
-
214
- }));
215
- //# sourceMappingURL=index.umd.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\nimport type { EventEmitter } from '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.Timer | 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\t// @ts-expect-error Silence weird compiler issue regarding `this.push`'s arguments not being `any`.\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.Timer | 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"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA4BA;;;UAGa,aAAa;;;;;;;QAyDzB,YAAmB,OAAqB,EAAE,KAAa,EAAE,UAAmC,EAAE;;;;YArD9F;;;;;eAAsC;;;;YAKtC;;;;;eAA8B;;;;YAK9B;;;;;eAAsC;;;;YAKtC,+BAAS,KAAK,EAAC;;;;YAKf,sCAAe;;;;YAKf,+BAAc,EAAE,EAAC;;;;YAKjB,gCAAU,CAAC,EAAC;;;;YAKZ,uCAAe;;;;YAKf,mCAAkC,IAAI,EAAC;;;;YAKvC,sCAAqD;YASpD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;YACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YACnB,uBAAA,IAAI,wBAAU,OAAO,CAAC,KAAK,IAAI,SAAQ,CAAC;YACxC,uBAAA,IAAI,uBAAS,OAAO,CAAC,KAAI,CAAC;YAC1B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,KAAK,MAAe,IAAI,CAAC,CAAC;;YAGtD,IAAI,uBAAA,IAAI,sBAAM;gBAA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