@sapphire/async-queue 1.5.1-next.f3911e9.0 β 1.5.1-next.f4ec2624.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 -74
- package/dist/index.d.mts +55 -0
- package/dist/index.global.js +46 -6
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +46 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +46 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -11
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
5
|
# @sapphire/async-queue
|
|
6
6
|
|
|
@@ -47,76 +47,14 @@ We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub S
|
|
|
47
47
|
| Patreon | [Click Here](https://sapphirejs.dev/patreon) |
|
|
48
48
|
| PayPal | [Click Here](https://sapphirejs.dev/paypal) |
|
|
49
49
|
|
|
50
|
-
## Contributors
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
<
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<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>
|
|
62
|
-
<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>
|
|
63
|
-
<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>
|
|
64
|
-
<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>
|
|
65
|
-
<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>
|
|
66
|
-
</tr>
|
|
67
|
-
<tr>
|
|
68
|
-
<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>
|
|
69
|
-
<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>
|
|
70
|
-
<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>
|
|
71
|
-
<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>
|
|
72
|
-
<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>
|
|
73
|
-
<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>
|
|
74
|
-
<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>
|
|
75
|
-
</tr>
|
|
76
|
-
<tr>
|
|
77
|
-
<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>
|
|
78
|
-
<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>
|
|
79
|
-
<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>
|
|
80
|
-
<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>
|
|
81
|
-
<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>
|
|
82
|
-
<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>
|
|
83
|
-
<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>
|
|
84
|
-
</tr>
|
|
85
|
-
<tr>
|
|
86
|
-
<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>
|
|
87
|
-
<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>
|
|
88
|
-
<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>
|
|
89
|
-
<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>
|
|
90
|
-
<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>
|
|
91
|
-
<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>
|
|
92
|
-
<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>
|
|
93
|
-
</tr>
|
|
94
|
-
<tr>
|
|
95
|
-
<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>
|
|
96
|
-
<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>
|
|
97
|
-
<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>
|
|
98
|
-
<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>
|
|
99
|
-
<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>
|
|
100
|
-
<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>
|
|
101
|
-
<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>
|
|
102
|
-
</tr>
|
|
103
|
-
<tr>
|
|
104
|
-
<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>
|
|
105
|
-
<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>
|
|
106
|
-
<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>
|
|
107
|
-
<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>
|
|
108
|
-
<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>
|
|
109
|
-
<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>
|
|
110
|
-
<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>
|
|
111
|
-
</tr>
|
|
112
|
-
<tr>
|
|
113
|
-
<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>
|
|
114
|
-
</tr>
|
|
115
|
-
</table>
|
|
116
|
-
|
|
117
|
-
<!-- markdownlint-restore -->
|
|
118
|
-
<!-- prettier-ignore-end -->
|
|
119
|
-
|
|
120
|
-
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
121
|
-
|
|
122
|
-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
50
|
+
## Contributors
|
|
51
|
+
|
|
52
|
+
Please make sure to read the [Contributing Guide][contributing] before making a pull request.
|
|
53
|
+
|
|
54
|
+
Thank you to all the people who already contributed to Sapphire!
|
|
55
|
+
|
|
56
|
+
<a href="https://github.com/sapphiredev/utilities/graphs/contributors">
|
|
57
|
+
<img src="https://contrib.rocks/image?repo=sapphiredev/utilities" />
|
|
58
|
+
</a>
|
|
59
|
+
|
|
60
|
+
[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The AsyncQueue class used to sequentialize burst requests
|
|
3
|
+
*/
|
|
4
|
+
declare class AsyncQueue {
|
|
5
|
+
/**
|
|
6
|
+
* The amount of entries in the queue, including the head.
|
|
7
|
+
* @seealso {@link queued} for the queued count.
|
|
8
|
+
*/
|
|
9
|
+
get remaining(): number;
|
|
10
|
+
/**
|
|
11
|
+
* The amount of queued entries.
|
|
12
|
+
* @seealso {@link remaining} for the count with the head.
|
|
13
|
+
*/
|
|
14
|
+
get queued(): number;
|
|
15
|
+
/**
|
|
16
|
+
* The promises array
|
|
17
|
+
*/
|
|
18
|
+
private promises;
|
|
19
|
+
/**
|
|
20
|
+
* Waits for last promise and queues a new one
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const queue = new AsyncQueue();
|
|
24
|
+
* async function request(url, options) {
|
|
25
|
+
* await queue.wait({ signal: options.signal });
|
|
26
|
+
* try {
|
|
27
|
+
* const result = await fetch(url, options);
|
|
28
|
+
* // Do some operations with 'result'
|
|
29
|
+
* } finally {
|
|
30
|
+
* // Remove first entry from the queue and resolve for the next entry
|
|
31
|
+
* queue.shift();
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
|
36
|
+
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
|
37
|
+
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
|
43
|
+
*/
|
|
44
|
+
shift(): void;
|
|
45
|
+
/**
|
|
46
|
+
* Aborts all the pending promises.
|
|
47
|
+
* @note To avoid race conditions, this does **not** unlock the head lock.
|
|
48
|
+
*/
|
|
49
|
+
abortAll(): void;
|
|
50
|
+
}
|
|
51
|
+
interface AsyncQueueWaitOptions {
|
|
52
|
+
signal?: AbortSignal | undefined | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { AsyncQueue, AsyncQueueWaitOptions };
|
package/dist/index.global.js
CHANGED
|
@@ -10,7 +10,7 @@ var SapphireAsyncQueue = (function (exports) {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/lib/AsyncQueueEntry.ts
|
|
13
|
-
var
|
|
13
|
+
var _AsyncQueueEntry = class _AsyncQueueEntry {
|
|
14
14
|
constructor(queue) {
|
|
15
15
|
__publicField(this, "promise");
|
|
16
16
|
__publicField(this, "resolve");
|
|
@@ -55,19 +55,52 @@ var SapphireAsyncQueue = (function (exports) {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
};
|
|
58
|
-
__name(
|
|
58
|
+
__name(_AsyncQueueEntry, "AsyncQueueEntry");
|
|
59
|
+
var AsyncQueueEntry = _AsyncQueueEntry;
|
|
59
60
|
|
|
60
61
|
// src/lib/AsyncQueue.ts
|
|
61
|
-
var
|
|
62
|
+
var _AsyncQueue = class _AsyncQueue {
|
|
62
63
|
constructor() {
|
|
64
|
+
/**
|
|
65
|
+
* The promises array
|
|
66
|
+
*/
|
|
63
67
|
__publicField(this, "promises", []);
|
|
64
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* The amount of entries in the queue, including the head.
|
|
71
|
+
* @seealso {@link queued} for the queued count.
|
|
72
|
+
*/
|
|
65
73
|
get remaining() {
|
|
66
74
|
return this.promises.length;
|
|
67
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* The amount of queued entries.
|
|
78
|
+
* @seealso {@link remaining} for the count with the head.
|
|
79
|
+
*/
|
|
68
80
|
get queued() {
|
|
69
81
|
return this.remaining === 0 ? 0 : this.remaining - 1;
|
|
70
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Waits for last promise and queues a new one
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const queue = new AsyncQueue();
|
|
88
|
+
* async function request(url, options) {
|
|
89
|
+
* await queue.wait({ signal: options.signal });
|
|
90
|
+
* try {
|
|
91
|
+
* const result = await fetch(url, options);
|
|
92
|
+
* // Do some operations with 'result'
|
|
93
|
+
* } finally {
|
|
94
|
+
* // Remove first entry from the queue and resolve for the next entry
|
|
95
|
+
* queue.shift();
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
|
100
|
+
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
|
101
|
+
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
71
104
|
wait(options) {
|
|
72
105
|
const entry = new AsyncQueueEntry(this);
|
|
73
106
|
if (this.promises.length === 0) {
|
|
@@ -79,6 +112,9 @@ var SapphireAsyncQueue = (function (exports) {
|
|
|
79
112
|
entry.setSignal(options.signal);
|
|
80
113
|
return entry.promise;
|
|
81
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
|
117
|
+
*/
|
|
82
118
|
shift() {
|
|
83
119
|
if (this.promises.length === 0)
|
|
84
120
|
return;
|
|
@@ -89,6 +125,10 @@ var SapphireAsyncQueue = (function (exports) {
|
|
|
89
125
|
this.promises.shift();
|
|
90
126
|
this.promises[0].use();
|
|
91
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Aborts all the pending promises.
|
|
130
|
+
* @note To avoid race conditions, this does **not** unlock the head lock.
|
|
131
|
+
*/
|
|
92
132
|
abortAll() {
|
|
93
133
|
if (this.queued === 0)
|
|
94
134
|
return;
|
|
@@ -98,13 +138,13 @@ var SapphireAsyncQueue = (function (exports) {
|
|
|
98
138
|
this.promises.length = 1;
|
|
99
139
|
}
|
|
100
140
|
};
|
|
101
|
-
__name(
|
|
141
|
+
__name(_AsyncQueue, "AsyncQueue");
|
|
142
|
+
var AsyncQueue = _AsyncQueue;
|
|
102
143
|
|
|
103
144
|
exports.AsyncQueue = AsyncQueue;
|
|
104
145
|
|
|
105
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
106
|
-
|
|
107
146
|
return exports;
|
|
108
147
|
|
|
109
148
|
})({});
|
|
149
|
+
//# sourceMappingURL=out.js.map
|
|
110
150
|
//# sourceMappingURL=index.global.js.map
|
package/dist/index.global.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue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C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAfvC,IAAW,YAAoB;AAC9B,WAAO,KAAK,SAAS;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,SAAiB;AAC3B,WAAO,KAAK,cAAc,IAAI,IAAI,KAAK,YAAY;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA4BO,KAAK,SAA0D;AACrE,UAAM,QAAQ,IAAI,gBAAgB,IAAI;AAEtC,QAAI,KAAK,SAAS,WAAW,GAAG;AAC/B,WAAK,SAAS,KAAK,KAAK;AACxB,aAAO,QAAQ,QAAQ;AAAA,IACxB;AAEA,SAAK,SAAS,KAAK,KAAK;AACxB,QAAI,SAAS;AAAQ,YAAM,UAAU,QAAQ,MAAM;AACnD,WAAO,MAAM;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKO,QAAc;AACpB,QAAI,KAAK,SAAS,WAAW;AAAG;AAChC,QAAI,KAAK,SAAS,WAAW,GAAG;AAE/B,WAAK,SAAS,MAAM;AACpB;AAAA,IACD;AAIA,SAAK,SAAS,MAAM;AACpB,SAAK,SAAS,CAAC,EAAE,IAAI;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,WAAiB;AAEvB,QAAI,KAAK,WAAW;AAAG;AAIvB,aAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,EAAE,GAAG;AAC9C,WAAK,SAAS,CAAC,EAAE,MAAM;AAAA,IACxB;AAEA,SAAK,SAAS,SAAS;AAAA,EACxB;AACD;AAzFwB;AAAjB,IAAM,aAAN","sourcesContent":["import type { AsyncQueue } from './AsyncQueue';\n\n/**\n * @internal\n */\nexport class AsyncQueueEntry {\n\tpublic readonly promise: Promise<void>;\n\tprivate resolve!: () => void;\n\tprivate reject!: (error: Error) => void;\n\tprivate readonly queue: AsyncQueue;\n\tprivate signal: PolyFillAbortSignal | null = null;\n\tprivate signalListener: (() => void) | null = null;\n\n\tpublic constructor(queue: AsyncQueue) {\n\t\tthis.queue = queue;\n\t\tthis.promise = new Promise((resolve, reject) => {\n\t\t\tthis.resolve = resolve;\n\t\t\tthis.reject = reject;\n\t\t});\n\t}\n\n\tpublic setSignal(signal: AbortSignal) {\n\t\tif (signal.aborted) return this;\n\n\t\tthis.signal = signal as PolyFillAbortSignal;\n\t\tthis.signalListener = () => {\n\t\t\tconst index = this.queue['promises'].indexOf(this);\n\t\t\tif (index !== -1) this.queue['promises'].splice(index, 1);\n\n\t\t\tthis.reject(new Error('Request aborted manually'));\n\t\t};\n\t\tthis.signal.addEventListener('abort', this.signalListener);\n\t\treturn this;\n\t}\n\n\tpublic use() {\n\t\tthis.dispose();\n\t\tthis.resolve();\n\t\treturn this;\n\t}\n\n\tpublic abort() {\n\t\tthis.dispose();\n\t\tthis.reject(new Error('Request aborted manually'));\n\t\treturn this;\n\t}\n\n\tprivate dispose() {\n\t\tif (this.signal) {\n\t\t\tthis.signal.removeEventListener('abort', this.signalListener!);\n\t\t\tthis.signal = null;\n\t\t\tthis.signalListener = null;\n\t\t}\n\t}\n}\n\ninterface PolyFillAbortSignal {\n\treadonly aborted: boolean;\n\taddEventListener(type: 'abort', listener: () => void): void;\n\tremoveEventListener(type: 'abort', listener: () => void): void;\n}\n","import { AsyncQueueEntry } from './AsyncQueueEntry';\n\n/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The amount of entries in the queue, including the head.\n\t * @seealso {@link queued} for the queued count.\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The amount of queued entries.\n\t * @seealso {@link remaining} for the count with the head.\n\t */\n\tpublic get queued(): number {\n\t\treturn this.remaining === 0 ? 0 : this.remaining - 1;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises: AsyncQueueEntry[] = [];\n\n\t/**\n\t * Waits for last promise and queues a new one\n\t * @example\n\t * ```typescript\n\t * const queue = new AsyncQueue();\n\t * async function request(url, options) {\n\t * await queue.wait({ signal: options.signal });\n\t * try {\n\t * const result = await fetch(url, options);\n\t * // Do some operations with 'result'\n\t * } finally {\n\t * // Remove first entry from the queue and resolve for the next entry\n\t * queue.shift();\n\t * }\n\t * }\n\t *\n\t * request(someUrl1, someOptions1); // Will call fetch() immediately\n\t * request(someUrl2, someOptions2); // Will call fetch() after the first finished\n\t * request(someUrl3, someOptions3); // Will call fetch() after the second finished\n\t * ```\n\t */\n\tpublic wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void> {\n\t\tconst entry = new AsyncQueueEntry(this);\n\n\t\tif (this.promises.length === 0) {\n\t\t\tthis.promises.push(entry);\n\t\t\treturn Promise.resolve();\n\t\t}\n\n\t\tthis.promises.push(entry);\n\t\tif (options?.signal) entry.setSignal(options.signal);\n\t\treturn entry.promise;\n\t}\n\n\t/**\n\t * Unlocks the head lock and transfers the next lock (if any) to the head.\n\t */\n\tpublic shift(): void {\n\t\tif (this.promises.length === 0) return;\n\t\tif (this.promises.length === 1) {\n\t\t\t// Remove the head entry.\n\t\t\tthis.promises.shift();\n\t\t\treturn;\n\t\t}\n\n\t\t// Remove the head entry, making the 2nd entry the new one.\n\t\t// Then use the head entry, which will unlock the promise.\n\t\tthis.promises.shift();\n\t\tthis.promises[0].use();\n\t}\n\n\t/**\n\t * Aborts all the pending promises.\n\t * @note To avoid race conditions, this does **not** unlock the head lock.\n\t */\n\tpublic abortAll(): void {\n\t\t// If there are no queued entries, skip early.\n\t\tif (this.queued === 0) return;\n\n\t\t// Abort all the entries except the head, that is why the loop starts at\n\t\t// 1 and not at 0.\n\t\tfor (let i = 1; i < this.promises.length; ++i) {\n\t\t\tthis.promises[i].abort();\n\t\t}\n\n\t\tthis.promises.length = 1;\n\t}\n}\n\nexport interface AsyncQueueWaitOptions {\n\tsignal?: AbortSignal | undefined | null;\n}\n"]}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var __defProp = Object.defineProperty;
|
|
6
4
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -11,7 +9,7 @@ var __publicField = (obj, key, value) => {
|
|
|
11
9
|
};
|
|
12
10
|
|
|
13
11
|
// src/lib/AsyncQueueEntry.ts
|
|
14
|
-
var
|
|
12
|
+
var _AsyncQueueEntry = class _AsyncQueueEntry {
|
|
15
13
|
constructor(queue) {
|
|
16
14
|
__publicField(this, "promise");
|
|
17
15
|
__publicField(this, "resolve");
|
|
@@ -56,19 +54,52 @@ var AsyncQueueEntry = class {
|
|
|
56
54
|
}
|
|
57
55
|
}
|
|
58
56
|
};
|
|
59
|
-
__name(
|
|
57
|
+
__name(_AsyncQueueEntry, "AsyncQueueEntry");
|
|
58
|
+
var AsyncQueueEntry = _AsyncQueueEntry;
|
|
60
59
|
|
|
61
60
|
// src/lib/AsyncQueue.ts
|
|
62
|
-
var
|
|
61
|
+
var _AsyncQueue = class _AsyncQueue {
|
|
63
62
|
constructor() {
|
|
63
|
+
/**
|
|
64
|
+
* The promises array
|
|
65
|
+
*/
|
|
64
66
|
__publicField(this, "promises", []);
|
|
65
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* The amount of entries in the queue, including the head.
|
|
70
|
+
* @seealso {@link queued} for the queued count.
|
|
71
|
+
*/
|
|
66
72
|
get remaining() {
|
|
67
73
|
return this.promises.length;
|
|
68
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* The amount of queued entries.
|
|
77
|
+
* @seealso {@link remaining} for the count with the head.
|
|
78
|
+
*/
|
|
69
79
|
get queued() {
|
|
70
80
|
return this.remaining === 0 ? 0 : this.remaining - 1;
|
|
71
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Waits for last promise and queues a new one
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const queue = new AsyncQueue();
|
|
87
|
+
* async function request(url, options) {
|
|
88
|
+
* await queue.wait({ signal: options.signal });
|
|
89
|
+
* try {
|
|
90
|
+
* const result = await fetch(url, options);
|
|
91
|
+
* // Do some operations with 'result'
|
|
92
|
+
* } finally {
|
|
93
|
+
* // Remove first entry from the queue and resolve for the next entry
|
|
94
|
+
* queue.shift();
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
|
99
|
+
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
|
100
|
+
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
72
103
|
wait(options) {
|
|
73
104
|
const entry = new AsyncQueueEntry(this);
|
|
74
105
|
if (this.promises.length === 0) {
|
|
@@ -80,6 +111,9 @@ var AsyncQueue = class {
|
|
|
80
111
|
entry.setSignal(options.signal);
|
|
81
112
|
return entry.promise;
|
|
82
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
|
116
|
+
*/
|
|
83
117
|
shift() {
|
|
84
118
|
if (this.promises.length === 0)
|
|
85
119
|
return;
|
|
@@ -90,6 +124,10 @@ var AsyncQueue = class {
|
|
|
90
124
|
this.promises.shift();
|
|
91
125
|
this.promises[0].use();
|
|
92
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Aborts all the pending promises.
|
|
129
|
+
* @note To avoid race conditions, this does **not** unlock the head lock.
|
|
130
|
+
*/
|
|
93
131
|
abortAll() {
|
|
94
132
|
if (this.queued === 0)
|
|
95
133
|
return;
|
|
@@ -99,7 +137,9 @@ var AsyncQueue = class {
|
|
|
99
137
|
this.promises.length = 1;
|
|
100
138
|
}
|
|
101
139
|
};
|
|
102
|
-
__name(
|
|
140
|
+
__name(_AsyncQueue, "AsyncQueue");
|
|
141
|
+
var AsyncQueue = _AsyncQueue;
|
|
103
142
|
|
|
104
143
|
exports.AsyncQueue = AsyncQueue;
|
|
144
|
+
//# sourceMappingURL=out.js.map
|
|
105
145
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"names":[],"mappings":";;;;;;;;;AAKO,IAAM,
|
|
1
|
+
{"version":3,"sources":["../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"names":[],"mappings":";;;;;;;;;AAKO,IAAM,mBAAN,MAAM,iBAAgB;AAAA,EAQrB,YAAY,OAAmB;AAPtC,wBAAgB;AAChB,wBAAQ;AACR,wBAAQ;AACR,wBAAiB;AACjB,wBAAQ,UAAqC;AAC7C,wBAAQ,kBAAsC;AAG7C,SAAK,QAAQ;AACb,SAAK,UAAU,IAAI,QAAQ,CAAC,SAAS,WAAW;AAC/C,WAAK,UAAU;AACf,WAAK,SAAS;AAAA,IACf,CAAC;AAAA,EACF;AAAA,EAEO,UAAU,QAAqB;AACrC,QAAI,OAAO;AAAS,aAAO;AAE3B,SAAK,SAAS;AACd,SAAK,iBAAiB,MAAM;AAC3B,YAAM,QAAQ,KAAK,MAAM,UAAU,EAAE,QAAQ,IAAI;AACjD,UAAI,UAAU;AAAI,aAAK,MAAM,UAAU,EAAE,OAAO,OAAO,CAAC;AAExD,WAAK,OAAO,IAAI,MAAM,0BAA0B,CAAC;AAAA,IAClD;AACA,SAAK,OAAO,iBAAiB,SAAS,KAAK,cAAc;AACzD,WAAO;AAAA,EACR;AAAA,EAEO,MAAM;AACZ,SAAK,QAAQ;AACb,SAAK,QAAQ;AACb,WAAO;AAAA,EACR;AAAA,E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;AAAA,EAMO,WAAiB;AAEvB,QAAI,KAAK,WAAW;AAAG;AAIvB,aAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,EAAE,GAAG;AAC9C,WAAK,SAAS,CAAC,EAAE,MAAM;AAAA,IACxB;AAEA,SAAK,SAAS,SAAS;AAAA,EACxB;AACD;AAzFwB;AAAjB,IAAM,aAAN","sourcesContent":["import type { AsyncQueue } from './AsyncQueue';\n\n/**\n * @internal\n */\nexport class AsyncQueueEntry {\n\tpublic readonly promise: Promise<void>;\n\tprivate resolve!: () => void;\n\tprivate reject!: (error: Error) => void;\n\tprivate readonly queue: AsyncQueue;\n\tprivate signal: PolyFillAbortSignal | null = null;\n\tprivate signalListener: (() => void) | null = null;\n\n\tpublic constructor(queue: AsyncQueue) {\n\t\tthis.queue = queue;\n\t\tthis.promise = new Promise((resolve, reject) => {\n\t\t\tthis.resolve = resolve;\n\t\t\tthis.reject = reject;\n\t\t});\n\t}\n\n\tpublic setSignal(signal: AbortSignal) {\n\t\tif (signal.aborted) return this;\n\n\t\tthis.signal = signal as PolyFillAbortSignal;\n\t\tthis.signalListener = () => {\n\t\t\tconst index = this.queue['promises'].indexOf(this);\n\t\t\tif (index !== -1) this.queue['promises'].splice(index, 1);\n\n\t\t\tthis.reject(new Error('Request aborted manually'));\n\t\t};\n\t\tthis.signal.addEventListener('abort', this.signalListener);\n\t\treturn this;\n\t}\n\n\tpublic use() {\n\t\tthis.dispose();\n\t\tthis.resolve();\n\t\treturn this;\n\t}\n\n\tpublic abort() {\n\t\tthis.dispose();\n\t\tthis.reject(new Error('Request aborted manually'));\n\t\treturn this;\n\t}\n\n\tprivate dispose() {\n\t\tif (this.signal) {\n\t\t\tthis.signal.removeEventListener('abort', this.signalListener!);\n\t\t\tthis.signal = null;\n\t\t\tthis.signalListener = null;\n\t\t}\n\t}\n}\n\ninterface PolyFillAbortSignal {\n\treadonly aborted: boolean;\n\taddEventListener(type: 'abort', listener: () => void): void;\n\tremoveEventListener(type: 'abort', listener: () => void): void;\n}\n","import { AsyncQueueEntry } from './AsyncQueueEntry';\n\n/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The amount of entries in the queue, including the head.\n\t * @seealso {@link queued} for the queued count.\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The amount of queued entries.\n\t * @seealso {@link remaining} for the count with the head.\n\t */\n\tpublic get queued(): number {\n\t\treturn this.remaining === 0 ? 0 : this.remaining - 1;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises: AsyncQueueEntry[] = [];\n\n\t/**\n\t * Waits for last promise and queues a new one\n\t * @example\n\t * ```typescript\n\t * const queue = new AsyncQueue();\n\t * async function request(url, options) {\n\t * await queue.wait({ signal: options.signal });\n\t * try {\n\t * const result = await fetch(url, options);\n\t * // Do some operations with 'result'\n\t * } finally {\n\t * // Remove first entry from the queue and resolve for the next entry\n\t * queue.shift();\n\t * }\n\t * }\n\t *\n\t * request(someUrl1, someOptions1); // Will call fetch() immediately\n\t * request(someUrl2, someOptions2); // Will call fetch() after the first finished\n\t * request(someUrl3, someOptions3); // Will call fetch() after the second finished\n\t * ```\n\t */\n\tpublic wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void> {\n\t\tconst entry = new AsyncQueueEntry(this);\n\n\t\tif (this.promises.length === 0) {\n\t\t\tthis.promises.push(entry);\n\t\t\treturn Promise.resolve();\n\t\t}\n\n\t\tthis.promises.push(entry);\n\t\tif (options?.signal) entry.setSignal(options.signal);\n\t\treturn entry.promise;\n\t}\n\n\t/**\n\t * Unlocks the head lock and transfers the next lock (if any) to the head.\n\t */\n\tpublic shift(): void {\n\t\tif (this.promises.length === 0) return;\n\t\tif (this.promises.length === 1) {\n\t\t\t// Remove the head entry.\n\t\t\tthis.promises.shift();\n\t\t\treturn;\n\t\t}\n\n\t\t// Remove the head entry, making the 2nd entry the new one.\n\t\t// Then use the head entry, which will unlock the promise.\n\t\tthis.promises.shift();\n\t\tthis.promises[0].use();\n\t}\n\n\t/**\n\t * Aborts all the pending promises.\n\t * @note To avoid race conditions, this does **not** unlock the head lock.\n\t */\n\tpublic abortAll(): void {\n\t\t// If there are no queued entries, skip early.\n\t\tif (this.queued === 0) return;\n\n\t\t// Abort all the entries except the head, that is why the loop starts at\n\t\t// 1 and not at 0.\n\t\tfor (let i = 1; i < this.promises.length; ++i) {\n\t\t\tthis.promises[i].abort();\n\t\t}\n\n\t\tthis.promises.length = 1;\n\t}\n}\n\nexport interface AsyncQueueWaitOptions {\n\tsignal?: AbortSignal | undefined | null;\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@ var __publicField = (obj, key, value) => {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
// src/lib/AsyncQueueEntry.ts
|
|
10
|
-
var
|
|
10
|
+
var _AsyncQueueEntry = class _AsyncQueueEntry {
|
|
11
11
|
constructor(queue) {
|
|
12
12
|
__publicField(this, "promise");
|
|
13
13
|
__publicField(this, "resolve");
|
|
@@ -52,19 +52,52 @@ var AsyncQueueEntry = class {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
};
|
|
55
|
-
__name(
|
|
55
|
+
__name(_AsyncQueueEntry, "AsyncQueueEntry");
|
|
56
|
+
var AsyncQueueEntry = _AsyncQueueEntry;
|
|
56
57
|
|
|
57
58
|
// src/lib/AsyncQueue.ts
|
|
58
|
-
var
|
|
59
|
+
var _AsyncQueue = class _AsyncQueue {
|
|
59
60
|
constructor() {
|
|
61
|
+
/**
|
|
62
|
+
* The promises array
|
|
63
|
+
*/
|
|
60
64
|
__publicField(this, "promises", []);
|
|
61
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The amount of entries in the queue, including the head.
|
|
68
|
+
* @seealso {@link queued} for the queued count.
|
|
69
|
+
*/
|
|
62
70
|
get remaining() {
|
|
63
71
|
return this.promises.length;
|
|
64
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* The amount of queued entries.
|
|
75
|
+
* @seealso {@link remaining} for the count with the head.
|
|
76
|
+
*/
|
|
65
77
|
get queued() {
|
|
66
78
|
return this.remaining === 0 ? 0 : this.remaining - 1;
|
|
67
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Waits for last promise and queues a new one
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const queue = new AsyncQueue();
|
|
85
|
+
* async function request(url, options) {
|
|
86
|
+
* await queue.wait({ signal: options.signal });
|
|
87
|
+
* try {
|
|
88
|
+
* const result = await fetch(url, options);
|
|
89
|
+
* // Do some operations with 'result'
|
|
90
|
+
* } finally {
|
|
91
|
+
* // Remove first entry from the queue and resolve for the next entry
|
|
92
|
+
* queue.shift();
|
|
93
|
+
* }
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
|
97
|
+
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
|
98
|
+
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
68
101
|
wait(options) {
|
|
69
102
|
const entry = new AsyncQueueEntry(this);
|
|
70
103
|
if (this.promises.length === 0) {
|
|
@@ -76,6 +109,9 @@ var AsyncQueue = class {
|
|
|
76
109
|
entry.setSignal(options.signal);
|
|
77
110
|
return entry.promise;
|
|
78
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
|
114
|
+
*/
|
|
79
115
|
shift() {
|
|
80
116
|
if (this.promises.length === 0)
|
|
81
117
|
return;
|
|
@@ -86,6 +122,10 @@ var AsyncQueue = class {
|
|
|
86
122
|
this.promises.shift();
|
|
87
123
|
this.promises[0].use();
|
|
88
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Aborts all the pending promises.
|
|
127
|
+
* @note To avoid race conditions, this does **not** unlock the head lock.
|
|
128
|
+
*/
|
|
89
129
|
abortAll() {
|
|
90
130
|
if (this.queued === 0)
|
|
91
131
|
return;
|
|
@@ -95,7 +135,9 @@ var AsyncQueue = class {
|
|
|
95
135
|
this.promises.length = 1;
|
|
96
136
|
}
|
|
97
137
|
};
|
|
98
|
-
__name(
|
|
138
|
+
__name(_AsyncQueue, "AsyncQueue");
|
|
139
|
+
var AsyncQueue = _AsyncQueue;
|
|
99
140
|
|
|
100
141
|
export { AsyncQueue };
|
|
142
|
+
//# sourceMappingURL=out.js.map
|
|
101
143
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"names":[],"mappings":";;;;;;;;;AAKO,IAAM,
|
|
1
|
+
{"version":3,"sources":["../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"names":[],"mappings":";;;;;;;;;AAKO,IAAM,mBAAN,MAAM,iBAAgB;AAAA,EAQrB,YAAY,OAAmB;AAPtC,wBAAgB;AAChB,wBAAQ;AACR,wBAAQ;AACR,wBAAiB;AACjB,wBAAQ,UAAqC;AAC7C,wBAAQ,kBAAsC;AAG7C,SAAK,QAAQ;AACb,SAAK,UAAU,IAAI,QAAQ,CAAC,SAAS,WAAW;AAC/C,WAAK,UAAU;AACf,WAAK,SAAS;AAAA,IACf,CAAC;AAAA,EACF;AAAA,EAEO,UAAU,QAAqB;AACrC,QAAI,OAAO;AAAS,aAAO;AAE3B,SAAK,SAAS;AACd,SAAK,iBAAiB,MAAM;AAC3B,YAAM,QAAQ,KAAK,MAAM,UAAU,EAAE,QAAQ,IAAI;AACjD,UAAI,UAAU;AAAI,aAAK,MAAM,UAAU,EAAE,OAAO,OAAO,CAAC;AAExD,WAAK,OAAO,IAAI,MAAM,0BAA0B,CAAC;AAAA,IAClD;AACA,SAAK,OAAO,iBAAiB,SAAS,KAAK,cAAc;AACzD,WAAO;AAAA,EACR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K,SAAS,MAAM;AACpB,SAAK,SAAS,CAAC,EAAE,IAAI;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,WAAiB;AAEvB,QAAI,KAAK,WAAW;AAAG;AAIvB,aAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,EAAE,GAAG;AAC9C,WAAK,SAAS,CAAC,EAAE,MAAM;AAAA,IACxB;AAEA,SAAK,SAAS,SAAS;AAAA,EACxB;AACD;AAzFwB;AAAjB,IAAM,aAAN","sourcesContent":["import type { AsyncQueue } from './AsyncQueue';\n\n/**\n * @internal\n */\nexport class AsyncQueueEntry {\n\tpublic readonly promise: Promise<void>;\n\tprivate resolve!: () => void;\n\tprivate reject!: (error: Error) => void;\n\tprivate readonly queue: AsyncQueue;\n\tprivate signal: PolyFillAbortSignal | null = null;\n\tprivate signalListener: (() => void) | null = null;\n\n\tpublic constructor(queue: AsyncQueue) {\n\t\tthis.queue = queue;\n\t\tthis.promise = new Promise((resolve, reject) => {\n\t\t\tthis.resolve = resolve;\n\t\t\tthis.reject = reject;\n\t\t});\n\t}\n\n\tpublic setSignal(signal: AbortSignal) {\n\t\tif (signal.aborted) return this;\n\n\t\tthis.signal = signal as PolyFillAbortSignal;\n\t\tthis.signalListener = () => {\n\t\t\tconst index = this.queue['promises'].indexOf(this);\n\t\t\tif (index !== -1) this.queue['promises'].splice(index, 1);\n\n\t\t\tthis.reject(new Error('Request aborted manually'));\n\t\t};\n\t\tthis.signal.addEventListener('abort', this.signalListener);\n\t\treturn this;\n\t}\n\n\tpublic use() {\n\t\tthis.dispose();\n\t\tthis.resolve();\n\t\treturn this;\n\t}\n\n\tpublic abort() {\n\t\tthis.dispose();\n\t\tthis.reject(new Error('Request aborted manually'));\n\t\treturn this;\n\t}\n\n\tprivate dispose() {\n\t\tif (this.signal) {\n\t\t\tthis.signal.removeEventListener('abort', this.signalListener!);\n\t\t\tthis.signal = null;\n\t\t\tthis.signalListener = null;\n\t\t}\n\t}\n}\n\ninterface PolyFillAbortSignal {\n\treadonly aborted: boolean;\n\taddEventListener(type: 'abort', listener: () => void): void;\n\tremoveEventListener(type: 'abort', listener: () => void): void;\n}\n","import { AsyncQueueEntry } from './AsyncQueueEntry';\n\n/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The amount of entries in the queue, including the head.\n\t * @seealso {@link queued} for the queued count.\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The amount of queued entries.\n\t * @seealso {@link remaining} for the count with the head.\n\t */\n\tpublic get queued(): number {\n\t\treturn this.remaining === 0 ? 0 : this.remaining - 1;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises: AsyncQueueEntry[] = [];\n\n\t/**\n\t * Waits for last promise and queues a new one\n\t * @example\n\t * ```typescript\n\t * const queue = new AsyncQueue();\n\t * async function request(url, options) {\n\t * await queue.wait({ signal: options.signal });\n\t * try {\n\t * const result = await fetch(url, options);\n\t * // Do some operations with 'result'\n\t * } finally {\n\t * // Remove first entry from the queue and resolve for the next entry\n\t * queue.shift();\n\t * }\n\t * }\n\t *\n\t * request(someUrl1, someOptions1); // Will call fetch() immediately\n\t * request(someUrl2, someOptions2); // Will call fetch() after the first finished\n\t * request(someUrl3, someOptions3); // Will call fetch() after the second finished\n\t * ```\n\t */\n\tpublic wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void> {\n\t\tconst entry = new AsyncQueueEntry(this);\n\n\t\tif (this.promises.length === 0) {\n\t\t\tthis.promises.push(entry);\n\t\t\treturn Promise.resolve();\n\t\t}\n\n\t\tthis.promises.push(entry);\n\t\tif (options?.signal) entry.setSignal(options.signal);\n\t\treturn entry.promise;\n\t}\n\n\t/**\n\t * Unlocks the head lock and transfers the next lock (if any) to the head.\n\t */\n\tpublic shift(): void {\n\t\tif (this.promises.length === 0) return;\n\t\tif (this.promises.length === 1) {\n\t\t\t// Remove the head entry.\n\t\t\tthis.promises.shift();\n\t\t\treturn;\n\t\t}\n\n\t\t// Remove the head entry, making the 2nd entry the new one.\n\t\t// Then use the head entry, which will unlock the promise.\n\t\tthis.promises.shift();\n\t\tthis.promises[0].use();\n\t}\n\n\t/**\n\t * Aborts all the pending promises.\n\t * @note To avoid race conditions, this does **not** unlock the head lock.\n\t */\n\tpublic abortAll(): void {\n\t\t// If there are no queued entries, skip early.\n\t\tif (this.queued === 0) return;\n\n\t\t// Abort all the entries except the head, that is why the loop starts at\n\t\t// 1 and not at 0.\n\t\tfor (let i = 1; i < this.promises.length; ++i) {\n\t\t\tthis.promises[i].abort();\n\t\t}\n\n\t\tthis.promises.length = 1;\n\t}\n}\n\nexport interface AsyncQueueWaitOptions {\n\tsignal?: AbortSignal | undefined | null;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/async-queue",
|
|
3
|
-
"version": "1.5.1-next.
|
|
3
|
+
"version": "1.5.1-next.f4ec2624.0",
|
|
4
4
|
"description": "Sequential asynchronous lock-based queue for promises",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,9 +10,14 @@
|
|
|
10
10
|
"unpkg": "dist/index.global.js",
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
|
-
"import":
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
16
21
|
},
|
|
17
22
|
"sideEffects": false,
|
|
18
23
|
"homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/async-queue",
|
|
@@ -56,12 +61,12 @@
|
|
|
56
61
|
"access": "public"
|
|
57
62
|
},
|
|
58
63
|
"devDependencies": {
|
|
59
|
-
"@favware/cliff-jumper": "^
|
|
60
|
-
"@vitest/coverage-
|
|
61
|
-
"tsup": "^
|
|
62
|
-
"typedoc": "^0.
|
|
63
|
-
"typedoc-json-parser": "^
|
|
64
|
-
"typescript": "^
|
|
65
|
-
"vitest": "^0.
|
|
64
|
+
"@favware/cliff-jumper": "^2.2.3",
|
|
65
|
+
"@vitest/coverage-v8": "^0.34.6",
|
|
66
|
+
"tsup": "^7.2.0",
|
|
67
|
+
"typedoc": "^0.25.3",
|
|
68
|
+
"typedoc-json-parser": "^9.0.1",
|
|
69
|
+
"typescript": "^5.2.2",
|
|
70
|
+
"vitest": "^0.34.6"
|
|
66
71
|
}
|
|
67
72
|
}
|