@sapphire/event-iterator 1.7.0-next.2e2c3e9.0 β 1.7.0-next.2e98f8fa.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 +12 -76
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +57 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
5
|
# @sapphire/event-iterator
|
|
6
6
|
|
|
@@ -83,78 +83,14 @@ We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub S
|
|
|
83
83
|
| Patreon | [Click Here](https://sapphirejs.dev/patreon) |
|
|
84
84
|
| PayPal | [Click Here](https://sapphirejs.dev/paypal) |
|
|
85
85
|
|
|
86
|
-
## Contributors
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
<
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
<td align="center"><a href="https://github.com/PyroTechniac"><img src="https://avatars2.githubusercontent.com/u/39341355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gryffon Bellish</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3APyroTechniac" title="Reviewed Pull Requests">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Tests">β οΈ</a></td>
|
|
98
|
-
<td align="center"><a href="https://github.com/vladfrangu"><img src="https://avatars3.githubusercontent.com/u/17960496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vlad Frangu</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3Avladfrangu" title="Bug reports">π</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Avladfrangu" title="Reviewed Pull Requests">π</a> <a href="#userTesting-vladfrangu" title="User Testing">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Tests">β οΈ</a></td>
|
|
99
|
-
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars0.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">π»</a> <a href="#projectManagement-Stitch07" title="Project Management">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">β οΈ</a></td>
|
|
100
|
-
<td align="center"><a href="https://github.com/apps/depfu"><img src="https://avatars3.githubusercontent.com/in/715?v=4?s=100" width="100px;" alt=""/><br /><sub><b>depfu[bot]</b></sub></a><br /><a href="#maintenance-depfu[bot]" title="Maintenance">π§</a></td>
|
|
101
|
-
<td align="center"><a href="https://github.com/apps/allcontributors"><img src="https://avatars0.githubusercontent.com/in/23186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>allcontributors[bot]</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=allcontributors[bot]" title="Documentation">π</a></td>
|
|
102
|
-
</tr>
|
|
103
|
-
<tr>
|
|
104
|
-
<td align="center"><a href="https://github.com/Nytelife26"><img src="https://avatars1.githubusercontent.com/u/22531310?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler J Russell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Nytelife26" title="Documentation">π</a></td>
|
|
105
|
-
<td align="center"><a href="https://github.com/Alcremie"><img src="https://avatars0.githubusercontent.com/u/54785334?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Lieder</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Alcremie" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AAlcremie" title="Bug reports">π</a></td>
|
|
106
|
-
<td align="center"><a href="https://github.com/RealShadowNova"><img src="https://avatars3.githubusercontent.com/u/46537907?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hezekiah Hendry</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=RealShadowNova" title="Code">π»</a> <a href="#tool-RealShadowNova" title="Tools">π§</a></td>
|
|
107
|
-
<td align="center"><a href="https://github.com/Vetlix"><img src="https://avatars.githubusercontent.com/u/31412314?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vetlix</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Vetlix" title="Code">π»</a></td>
|
|
108
|
-
<td align="center"><a href="https://github.com/ethamitc"><img src="https://avatars.githubusercontent.com/u/27776796?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ethan Mitchell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ethamitc" title="Documentation">π</a></td>
|
|
109
|
-
<td align="center"><a href="https://github.com/noftaly"><img src="https://avatars.githubusercontent.com/u/34779161?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elliot</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=noftaly" title="Code">π»</a></td>
|
|
110
|
-
<td align="center"><a href="https://jurien.dev"><img src="https://avatars.githubusercontent.com/u/5418114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jurien Hamaker</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=jurienhamaker" title="Code">π»</a></td>
|
|
111
|
-
</tr>
|
|
112
|
-
<tr>
|
|
113
|
-
<td align="center"><a href="https://fanoulis.dev/"><img src="https://avatars.githubusercontent.com/u/38255093?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charalampos Fanoulis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=cfanoulis" title="Documentation">π</a></td>
|
|
114
|
-
<td align="center"><a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4?s=100" width="100px;" alt=""/><br /><sub><b>dependabot[bot]</b></sub></a><br /><a href="#maintenance-dependabot[bot]" title="Maintenance">π§</a></td>
|
|
115
|
-
<td align="center"><a href="https://kaname.netlify.app/"><img src="https://avatars.githubusercontent.com/u/56084970?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kaname</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kaname-png" title="Code">π»</a></td>
|
|
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
|
-
<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
|
-
<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> <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>
|
|
123
|
-
<td align="center"><a href="https://github.com/apps/renovate"><img src="https://avatars.githubusercontent.com/in/2740?v=4?s=100" width="100px;" alt=""/><br /><sub><b>renovate[bot]</b></sub></a><br /><a href="#maintenance-renovate[bot]" title="Maintenance">π§</a></td>
|
|
124
|
-
<td align="center"><a href="https://renovate.whitesourcesoftware.com/"><img src="https://avatars.githubusercontent.com/u/25180681?v=4?s=100" width="100px;" alt=""/><br /><sub><b>WhiteSource Renovate</b></sub></a><br /><a href="#maintenance-renovate-bot" title="Maintenance">π§</a></td>
|
|
125
|
-
<td align="center"><a href="https://fc5570.me/"><img src="https://avatars.githubusercontent.com/u/68158483?v=4?s=100" width="100px;" alt=""/><br /><sub><b>FC</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=FC5570" title="Code">π»</a></td>
|
|
126
|
-
<td align="center"><a href="https://github.com/Tokipudi"><img src="https://avatars.githubusercontent.com/u/29551076?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JΓ©rΓ©my de Saint Denis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Tokipudi" title="Code">π»</a></td>
|
|
127
|
-
<td align="center"><a href="https://github.com/ItsMrCube"><img src="https://avatars.githubusercontent.com/u/25201357?v=4?s=100" width="100px;" alt=""/><br /><sub><b>MrCube</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ItsMrCube" title="Code">π»</a></td>
|
|
128
|
-
<td align="center"><a href="https://github.com/bitomic"><img src="https://avatars.githubusercontent.com/u/35199700?v=4?s=100" width="100px;" alt=""/><br /><sub><b>bitomic</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=bitomic" title="Code">π»</a></td>
|
|
129
|
-
</tr>
|
|
130
|
-
<tr>
|
|
131
|
-
<td align="center"><a href="https://c43721.dev/"><img src="https://avatars.githubusercontent.com/u/55610086?v=4?s=100" width="100px;" alt=""/><br /><sub><b>c43721</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=c43721" title="Code">π»</a></td>
|
|
132
|
-
<td align="center"><a href="https://commandtechno.com/"><img src="https://avatars.githubusercontent.com/u/68407783?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Commandtechno</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Commandtechno" title="Code">π»</a></td>
|
|
133
|
-
<td align="center"><a href="https://github.com/dhruv-kaushikk"><img src="https://avatars.githubusercontent.com/u/73697546?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aura</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=dhruv-kaushikk" title="Code">π»</a></td>
|
|
134
|
-
<td align="center"><a href="https://axis.moe/"><img src="https://avatars.githubusercontent.com/u/54381371?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jonathan</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=axisiscool" title="Code">π»</a></td>
|
|
135
|
-
<td align="center"><a href="https://github.com/imranbarbhuiya"><img src="https://avatars.githubusercontent.com/u/74945038?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Parbez</b></sub></a><br /><a href="#maintenance-imranbarbhuiya" title="Maintenance">π§</a></td>
|
|
136
|
-
<td align="center"><a href="https://github.com/NotKaskus"><img src="https://avatars.githubusercontent.com/u/75168528?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Paul Andrew</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=NotKaskus" title="Documentation">π</a></td>
|
|
137
|
-
<td align="center"><a href="https://linktr.ee/mzato0001"><img src="https://avatars.githubusercontent.com/u/62367547?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mzato</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Mzato0001" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AMzato0001" title="Bug reports">π</a></td>
|
|
138
|
-
</tr>
|
|
139
|
-
<tr>
|
|
140
|
-
<td align="center"><a href="https://github.com/MajesticString"><img src="https://avatars.githubusercontent.com/u/66224939?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Harry Allen</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=MajesticString" title="Documentation">π</a></td>
|
|
141
|
-
<td align="center"><a href="https://github.com/EvolutionX-10"><img src="https://avatars.githubusercontent.com/u/85353424?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Evo</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=EvolutionX-10" title="Code">π»</a></td>
|
|
142
|
-
<td align="center"><a href="https://enes.ovh/"><img src="https://avatars.githubusercontent.com/u/61084101?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Enes GenΓ§</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=enxg" title="Code">π»</a></td>
|
|
143
|
-
<td align="center"><a href="https://github.com/muchnameless"><img src="https://avatars.githubusercontent.com/u/12682826?v=4?s=100" width="100px;" alt=""/><br /><sub><b>muchnameless</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=muchnameless" title="Code">π»</a></td>
|
|
144
|
-
<td align="center"><a href="https://github.com/r-priyam"><img src="https://avatars.githubusercontent.com/u/50884372?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Priyam</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=r-priyam" title="Code">π»</a></td>
|
|
145
|
-
<td align="center"><a href="https://github.com/legendhimslef"><img src="https://avatars.githubusercontent.com/u/69213593?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Voxelli</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=legendhimslef" title="Code">π»</a></td>
|
|
146
|
-
<td align="center"><a href="https://github.com/CitTheDev"><img src="https://avatars.githubusercontent.com/u/94020875?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cit The Dev</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=CitTheDev" title="Code">π»</a></td>
|
|
147
|
-
</tr>
|
|
148
|
-
<tr>
|
|
149
|
-
<td align="center"><a href="https://www.goestav.com/"><img src="https://avatars.githubusercontent.com/u/27970303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Goestav</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=goestav" title="Code">π»</a></td>
|
|
150
|
-
<td align="center"><a href="https://github.com/didinele"><img src="https://avatars.githubusercontent.com/u/27137376?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DD</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=didinele" title="Code">π»</a></td>
|
|
151
|
-
<td align="center"><a href="https://steamcommunity.com/id/06000208"><img src="https://avatars.githubusercontent.com/u/52764066?v=4?s=100" width="100px;" alt=""/><br /><sub><b>amber</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=06000208" title="Code">π»</a></td>
|
|
152
|
-
</tr>
|
|
153
|
-
</table>
|
|
154
|
-
|
|
155
|
-
<!-- markdownlint-restore -->
|
|
156
|
-
<!-- prettier-ignore-end -->
|
|
157
|
-
|
|
158
|
-
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
159
|
-
|
|
160
|
-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
86
|
+
## Contributors
|
|
87
|
+
|
|
88
|
+
Please make sure to read the [Contributing Guide][contributing] before making a pull request.
|
|
89
|
+
|
|
90
|
+
Thank you to all the people who already contributed to Sapphire!
|
|
91
|
+
|
|
92
|
+
<a href="https://github.com/sapphiredev/utilities/graphs/contributors">
|
|
93
|
+
<img src="https://contrib.rocks/image?repo=sapphiredev/utilities" />
|
|
94
|
+
</a>
|
|
95
|
+
|
|
96
|
+
[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md
|
package/dist/index.js
CHANGED
|
@@ -37,16 +37,52 @@ var __privateWrapper = (obj, member, setter, getter) => ({
|
|
|
37
37
|
// src/index.ts
|
|
38
38
|
var _ended, _idle, _queue, _passed, _limit, _idleTimer, _push;
|
|
39
39
|
var EventIterator = class {
|
|
40
|
+
/**
|
|
41
|
+
* @param emitter The event emitter to listen to.
|
|
42
|
+
* @param event The event we're listening for to receives values from.
|
|
43
|
+
* @param limit The amount of values to receive before ending the iterator.
|
|
44
|
+
* @param options Any extra options.
|
|
45
|
+
*/
|
|
40
46
|
constructor(emitter, event, options = {}) {
|
|
47
|
+
/**
|
|
48
|
+
* The emitter to listen to.
|
|
49
|
+
*/
|
|
41
50
|
__publicField(this, "emitter");
|
|
51
|
+
/**
|
|
52
|
+
* The event the event iterator is listening for to receive values from.
|
|
53
|
+
*/
|
|
42
54
|
__publicField(this, "event");
|
|
55
|
+
/**
|
|
56
|
+
* The filter used to filter out values.
|
|
57
|
+
*/
|
|
43
58
|
__publicField(this, "filter");
|
|
59
|
+
/**
|
|
60
|
+
* Whether or not the EventIterator has ended.
|
|
61
|
+
*/
|
|
44
62
|
__privateAdd(this, _ended, false);
|
|
63
|
+
/**
|
|
64
|
+
* The amount of idle time in ms before moving on.
|
|
65
|
+
*/
|
|
45
66
|
__privateAdd(this, _idle, void 0);
|
|
67
|
+
/**
|
|
68
|
+
* The queue of received values.
|
|
69
|
+
*/
|
|
46
70
|
__privateAdd(this, _queue, []);
|
|
71
|
+
/**
|
|
72
|
+
* The amount of events that have passed the filter.
|
|
73
|
+
*/
|
|
47
74
|
__privateAdd(this, _passed, 0);
|
|
75
|
+
/**
|
|
76
|
+
* The limit before ending the EventIterator.
|
|
77
|
+
*/
|
|
48
78
|
__privateAdd(this, _limit, void 0);
|
|
79
|
+
/**
|
|
80
|
+
* The timer to track when this will idle out.
|
|
81
|
+
*/
|
|
49
82
|
__privateAdd(this, _idleTimer, null);
|
|
83
|
+
/**
|
|
84
|
+
* The push handler with context bound to the instance.
|
|
85
|
+
*/
|
|
50
86
|
__privateAdd(this, _push, void 0);
|
|
51
87
|
this.emitter = emitter;
|
|
52
88
|
this.event = event;
|
|
@@ -61,9 +97,15 @@ var EventIterator = class {
|
|
|
61
97
|
this.emitter.setMaxListeners(maxListeners + 1);
|
|
62
98
|
this.emitter.on(this.event, __privateGet(this, _push));
|
|
63
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Whether or not the EventIterator has ended.
|
|
102
|
+
*/
|
|
64
103
|
get ended() {
|
|
65
104
|
return __privateGet(this, _ended);
|
|
66
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Ends the EventIterator.
|
|
108
|
+
*/
|
|
67
109
|
end() {
|
|
68
110
|
if (__privateGet(this, _ended))
|
|
69
111
|
return;
|
|
@@ -74,6 +116,9 @@ var EventIterator = class {
|
|
|
74
116
|
if (maxListeners !== 0)
|
|
75
117
|
this.emitter.setMaxListeners(maxListeners - 1);
|
|
76
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* The next value that's received from the EventEmitter.
|
|
121
|
+
*/
|
|
77
122
|
async next() {
|
|
78
123
|
if (__privateGet(this, _queue).length) {
|
|
79
124
|
const value = __privateGet(this, _queue).shift();
|
|
@@ -105,17 +150,29 @@ var EventIterator = class {
|
|
|
105
150
|
});
|
|
106
151
|
});
|
|
107
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Handles what happens when you break or return from a loop.
|
|
155
|
+
*/
|
|
108
156
|
return() {
|
|
109
157
|
this.end();
|
|
110
158
|
return Promise.resolve({ done: true, value: void 0 });
|
|
111
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Handles what happens when you encounter an error in a loop.
|
|
162
|
+
*/
|
|
112
163
|
throw() {
|
|
113
164
|
this.end();
|
|
114
165
|
return Promise.resolve({ done: true, value: void 0 });
|
|
115
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* The symbol allowing EventIterators to be used in for-await-of loops.
|
|
169
|
+
*/
|
|
116
170
|
[Symbol.asyncIterator]() {
|
|
117
171
|
return this;
|
|
118
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Pushes a value into the queue.
|
|
175
|
+
*/
|
|
119
176
|
push(...value) {
|
|
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Q,aAAK,IAAI;AAC5C,UAAI,mBAAK;AAAY,2BAAK,YAAW,QAAQ;AAC7C,aAAO,EAAE,MAAM,OAAO,MAAM;AAAA,IAC7B;AAGA,QAAI,mBAAK,SAAQ;AAChB,UAAI,mBAAK;AAAY,qBAAa,mBAAK,WAAU;AACjD,aAAO,EAAE,MAAM,MAAM,OAAO,OAAmB;AAAA,IAChD;AAGA,WAAO,IAAI,QAA2B,CAAC,YAAY;AAClD,UAAI,YAAiC;AAIrC,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;AAtKa;AAmBZ;AAKA;AAKA;AAKA;AAKA;AAKA;AAKA","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.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\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"]}
|
package/dist/index.mjs
CHANGED
|
@@ -35,16 +35,52 @@ var __privateWrapper = (obj, member, setter, getter) => ({
|
|
|
35
35
|
// src/index.ts
|
|
36
36
|
var _ended, _idle, _queue, _passed, _limit, _idleTimer, _push;
|
|
37
37
|
var EventIterator = class {
|
|
38
|
+
/**
|
|
39
|
+
* @param emitter The event emitter to listen to.
|
|
40
|
+
* @param event The event we're listening for to receives values from.
|
|
41
|
+
* @param limit The amount of values to receive before ending the iterator.
|
|
42
|
+
* @param options Any extra options.
|
|
43
|
+
*/
|
|
38
44
|
constructor(emitter, event, options = {}) {
|
|
45
|
+
/**
|
|
46
|
+
* The emitter to listen to.
|
|
47
|
+
*/
|
|
39
48
|
__publicField(this, "emitter");
|
|
49
|
+
/**
|
|
50
|
+
* The event the event iterator is listening for to receive values from.
|
|
51
|
+
*/
|
|
40
52
|
__publicField(this, "event");
|
|
53
|
+
/**
|
|
54
|
+
* The filter used to filter out values.
|
|
55
|
+
*/
|
|
41
56
|
__publicField(this, "filter");
|
|
57
|
+
/**
|
|
58
|
+
* Whether or not the EventIterator has ended.
|
|
59
|
+
*/
|
|
42
60
|
__privateAdd(this, _ended, false);
|
|
61
|
+
/**
|
|
62
|
+
* The amount of idle time in ms before moving on.
|
|
63
|
+
*/
|
|
43
64
|
__privateAdd(this, _idle, void 0);
|
|
65
|
+
/**
|
|
66
|
+
* The queue of received values.
|
|
67
|
+
*/
|
|
44
68
|
__privateAdd(this, _queue, []);
|
|
69
|
+
/**
|
|
70
|
+
* The amount of events that have passed the filter.
|
|
71
|
+
*/
|
|
45
72
|
__privateAdd(this, _passed, 0);
|
|
73
|
+
/**
|
|
74
|
+
* The limit before ending the EventIterator.
|
|
75
|
+
*/
|
|
46
76
|
__privateAdd(this, _limit, void 0);
|
|
77
|
+
/**
|
|
78
|
+
* The timer to track when this will idle out.
|
|
79
|
+
*/
|
|
47
80
|
__privateAdd(this, _idleTimer, null);
|
|
81
|
+
/**
|
|
82
|
+
* The push handler with context bound to the instance.
|
|
83
|
+
*/
|
|
48
84
|
__privateAdd(this, _push, void 0);
|
|
49
85
|
this.emitter = emitter;
|
|
50
86
|
this.event = event;
|
|
@@ -59,9 +95,15 @@ var EventIterator = class {
|
|
|
59
95
|
this.emitter.setMaxListeners(maxListeners + 1);
|
|
60
96
|
this.emitter.on(this.event, __privateGet(this, _push));
|
|
61
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Whether or not the EventIterator has ended.
|
|
100
|
+
*/
|
|
62
101
|
get ended() {
|
|
63
102
|
return __privateGet(this, _ended);
|
|
64
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Ends the EventIterator.
|
|
106
|
+
*/
|
|
65
107
|
end() {
|
|
66
108
|
if (__privateGet(this, _ended))
|
|
67
109
|
return;
|
|
@@ -72,6 +114,9 @@ var EventIterator = class {
|
|
|
72
114
|
if (maxListeners !== 0)
|
|
73
115
|
this.emitter.setMaxListeners(maxListeners - 1);
|
|
74
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* The next value that's received from the EventEmitter.
|
|
119
|
+
*/
|
|
75
120
|
async next() {
|
|
76
121
|
if (__privateGet(this, _queue).length) {
|
|
77
122
|
const value = __privateGet(this, _queue).shift();
|
|
@@ -103,17 +148,29 @@ var EventIterator = class {
|
|
|
103
148
|
});
|
|
104
149
|
});
|
|
105
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Handles what happens when you break or return from a loop.
|
|
153
|
+
*/
|
|
106
154
|
return() {
|
|
107
155
|
this.end();
|
|
108
156
|
return Promise.resolve({ done: true, value: void 0 });
|
|
109
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Handles what happens when you encounter an error in a loop.
|
|
160
|
+
*/
|
|
110
161
|
throw() {
|
|
111
162
|
this.end();
|
|
112
163
|
return Promise.resolve({ done: true, value: void 0 });
|
|
113
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* The symbol allowing EventIterators to be used in for-await-of loops.
|
|
167
|
+
*/
|
|
114
168
|
[Symbol.asyncIterator]() {
|
|
115
169
|
return this;
|
|
116
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Pushes a value into the queue.
|
|
173
|
+
*/
|
|
117
174
|
push(...value) {
|
|
118
175
|
__privateGet(this, _queue).push(value);
|
|
119
176
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AA8BO,IAAM,gBAAN,MAA6E;AAAA,EAyD5E,YAAY,SAAuB,OAAe,UAAmC,CAAC,GAAG;AArDhG,wBAAgB;AAKhB,wBAAgB;AAKhB,wBAAO;AAKP,+BAAS;AAKT;AAKA,+BAAc,CAAC;AAKf,gCAAU;AAKV;AAKA,mCAAkC;AAKlC;AASC,SAAK,UAAU;AACf,SAAK,QAAQ;AACb,uBAAK,QAAS,QAAQ,SAAS;AAC/B,uBAAK,OAAQ,QAAQ;AACrB,SAAK,SAAS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AW,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;AAtKa;AAmBZ;AAKA;AAKA;AAKA;AAKA;AAKA;AAKA","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.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\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/event-iterator",
|
|
3
|
-
"version": "1.7.0-next.
|
|
3
|
+
"version": "1.7.0-next.2e98f8fa.0",
|
|
4
4
|
"description": "Turns event emitter events into async iterators.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"check-update": "cliff-jumper --dry-run"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"tslib": "^2.
|
|
27
|
+
"tslib": "^2.5.2"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|
|
@@ -57,12 +57,12 @@
|
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@favware/cliff-jumper": "^
|
|
61
|
-
"@vitest/coverage-c8": "^0.
|
|
62
|
-
"tsup": "^6.
|
|
63
|
-
"typedoc": "^0.
|
|
64
|
-
"typedoc-json-parser": "^
|
|
65
|
-
"typescript": "^
|
|
66
|
-
"vitest": "^0.
|
|
60
|
+
"@favware/cliff-jumper": "^2.0.1",
|
|
61
|
+
"@vitest/coverage-c8": "^0.31.1",
|
|
62
|
+
"tsup": "^6.7.0",
|
|
63
|
+
"typedoc": "^0.24.7",
|
|
64
|
+
"typedoc-json-parser": "^8.1.2",
|
|
65
|
+
"typescript": "^5.0.4",
|
|
66
|
+
"vitest": "^0.31.1"
|
|
67
67
|
}
|
|
68
68
|
}
|