@sapphire/async-queue 1.3.2 β 1.4.1-next.63e15eb.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +3 -0
- package/dist/index.d.ts +45 -2
- package/dist/index.global.js +59 -14
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +59 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
- package/dist/lib/AsyncQueue.d.ts +0 -40
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,35 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
# [@sapphire/async-queue@1.4.0](https://github.com/sapphiredev/utilities/compare/@sapphire/async-queue@1.3.1...@sapphire/async-queue@1.4.0) - (2022-08-07)
|
|
6
|
+
|
|
7
|
+
## π Bug Fixes
|
|
8
|
+
|
|
9
|
+
- **deps:** Update all non-major dependencies ([84af0db](https://github.com/sapphiredev/utilities/commit/84af0db2db749223b036aa99fe19a2e9af5681c6))
|
|
10
|
+
- **deps:** Update all non-major dependencies ([50cd8de](https://github.com/sapphiredev/utilities/commit/50cd8dea593b6f5ae75571209456b3421e2ca59a))
|
|
11
|
+
|
|
12
|
+
## π Documentation
|
|
13
|
+
|
|
14
|
+
- Add @muchnameless as a contributor ([a1221fe](https://github.com/sapphiredev/utilities/commit/a1221fea68506e99591d5d00ec552a07c26833f9))
|
|
15
|
+
- Add @enxg as a contributor ([d2382f0](https://github.com/sapphiredev/utilities/commit/d2382f04e3909cb4ad11798a0a10e683f6cf5383))
|
|
16
|
+
- Add @EvolutionX-10 as a contributor ([efc3a32](https://github.com/sapphiredev/utilities/commit/efc3a320a72ae258996dd62866d206c33f8d4961))
|
|
17
|
+
- Add @MajesticString as a contributor ([295b3e9](https://github.com/sapphiredev/utilities/commit/295b3e9849a4b0fe64074bae02f6426378a303c3))
|
|
18
|
+
- Add @Mzato0001 as a contributor ([c790ef2](https://github.com/sapphiredev/utilities/commit/c790ef25df2d7e22888fa9f8169167aa555e9e19))
|
|
19
|
+
- Add @NotKaskus as a contributor ([00da8f1](https://github.com/sapphiredev/utilities/commit/00da8f199137b9277119823f322d1f2d168d928a))
|
|
20
|
+
- Add @imranbarbhuiya as a contributor ([fb674c2](https://github.com/sapphiredev/utilities/commit/fb674c2c5594d41e71662263553dcb4bac9e37f4))
|
|
21
|
+
- Add @axisiscool as a contributor ([ce1aa31](https://github.com/sapphiredev/utilities/commit/ce1aa316871a88d3663efbdf2a42d3d8dfe6a27f))
|
|
22
|
+
- Add @dhruv-kaushikk as a contributor ([ebbf43f](https://github.com/sapphiredev/utilities/commit/ebbf43f63617daba96e72c50a234bf8b64f6ddc4))
|
|
23
|
+
- Add @Commandtechno as a contributor ([f1d69fa](https://github.com/sapphiredev/utilities/commit/f1d69fabe1ee0abe4be08b19e63dbec03102f7ce))
|
|
24
|
+
- Fix typedoc causing OOM crashes ([63ba41c](https://github.com/sapphiredev/utilities/commit/63ba41c4b6678554b1c7043a22d3296db4f59360))
|
|
25
|
+
|
|
26
|
+
## π Features
|
|
27
|
+
|
|
28
|
+
- **AsyncQueue:** Add AbortSignal support (#417) ([c0629e7](https://github.com/sapphiredev/utilities/commit/c0629e781ebc3f48e496a0851191b32e91f62fe9))
|
|
29
|
+
|
|
30
|
+
## π§ͺ Testing
|
|
31
|
+
|
|
32
|
+
- Migrate to vitest (#380) ([075ec73](https://github.com/sapphiredev/utilities/commit/075ec73c7a8e3374fad3ada612d37eb4ac36ec8d))
|
|
33
|
+
|
|
5
34
|
## [1.3.1](https://github.com/sapphiredev/utilities/compare/@sapphire/async-queue@1.3.0...@sapphire/async-queue@1.3.1) (2022-04-01)
|
|
6
35
|
|
|
7
36
|
**Note:** Version bump only for package @sapphire/async-queue
|
package/README.md
CHANGED
|
@@ -102,6 +102,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
102
102
|
</tr>
|
|
103
103
|
<tr>
|
|
104
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>
|
|
105
108
|
</tr>
|
|
106
109
|
</table>
|
|
107
110
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The AsyncQueue class used to sequentialize burst requests
|
|
3
|
+
*/
|
|
4
|
+
declare class AsyncQueue {
|
|
5
|
+
/**
|
|
6
|
+
* The remaining amount of queued promises
|
|
7
|
+
*/
|
|
8
|
+
get remaining(): number;
|
|
9
|
+
/**
|
|
10
|
+
* The promises array
|
|
11
|
+
*/
|
|
12
|
+
private promises;
|
|
13
|
+
/**
|
|
14
|
+
* Waits for last promise and queues a new one
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const queue = new AsyncQueue();
|
|
18
|
+
* async function request(url, options) {
|
|
19
|
+
* await queue.wait({ signal: options.signal });
|
|
20
|
+
* try {
|
|
21
|
+
* const result = await fetch(url, options);
|
|
22
|
+
* // Do some operations with 'result'
|
|
23
|
+
* } finally {
|
|
24
|
+
* // Remove first entry from the queue and resolve for the next entry
|
|
25
|
+
* queue.shift();
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
|
30
|
+
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
|
31
|
+
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
|
37
|
+
* @returns Whether or not there was an element pending to process.
|
|
38
|
+
*/
|
|
39
|
+
shift(): void;
|
|
40
|
+
}
|
|
41
|
+
interface AsyncQueueWaitOptions {
|
|
42
|
+
signal?: AbortSignal | undefined | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { AsyncQueue, AsyncQueueWaitOptions };
|
package/dist/index.global.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var SapphireAsyncQueue = (() => {
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -29,6 +30,46 @@ var SapphireAsyncQueue = (() => {
|
|
|
29
30
|
AsyncQueue: () => AsyncQueue
|
|
30
31
|
});
|
|
31
32
|
|
|
33
|
+
// src/lib/AsyncQueueEntry.ts
|
|
34
|
+
var AsyncQueueEntry = class {
|
|
35
|
+
constructor(queue) {
|
|
36
|
+
__publicField(this, "promise");
|
|
37
|
+
__publicField(this, "resolve");
|
|
38
|
+
__publicField(this, "reject");
|
|
39
|
+
__publicField(this, "queue");
|
|
40
|
+
__publicField(this, "signal", null);
|
|
41
|
+
__publicField(this, "signalListener", null);
|
|
42
|
+
this.queue = queue;
|
|
43
|
+
this.promise = new Promise((resolve, reject) => {
|
|
44
|
+
this.resolve = resolve;
|
|
45
|
+
this.reject = reject;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
setSignal(signal) {
|
|
49
|
+
if (signal.aborted)
|
|
50
|
+
return this;
|
|
51
|
+
this.signal = signal;
|
|
52
|
+
this.signalListener = () => {
|
|
53
|
+
const index = this.queue["promises"].indexOf(this);
|
|
54
|
+
if (index !== -1)
|
|
55
|
+
this.queue["promises"].splice(index, 1);
|
|
56
|
+
this.reject(new Error("Request aborted manually"));
|
|
57
|
+
};
|
|
58
|
+
this.signal.addEventListener("abort", this.signalListener);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
use() {
|
|
62
|
+
if (this.signal) {
|
|
63
|
+
this.signal.removeEventListener("abort", this.signalListener);
|
|
64
|
+
this.signal = null;
|
|
65
|
+
this.signalListener = null;
|
|
66
|
+
}
|
|
67
|
+
this.resolve();
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
__name(AsyncQueueEntry, "AsyncQueueEntry");
|
|
72
|
+
|
|
32
73
|
// src/lib/AsyncQueue.ts
|
|
33
74
|
var AsyncQueue = class {
|
|
34
75
|
constructor() {
|
|
@@ -37,22 +78,26 @@ var SapphireAsyncQueue = (() => {
|
|
|
37
78
|
get remaining() {
|
|
38
79
|
return this.promises.length;
|
|
39
80
|
}
|
|
40
|
-
wait() {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
resolve
|
|
45
|
-
}
|
|
46
|
-
this.promises.push(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return next;
|
|
81
|
+
wait(options) {
|
|
82
|
+
const entry = new AsyncQueueEntry(this);
|
|
83
|
+
if (this.promises.length === 0) {
|
|
84
|
+
this.promises.push(entry);
|
|
85
|
+
return Promise.resolve();
|
|
86
|
+
}
|
|
87
|
+
this.promises.push(entry);
|
|
88
|
+
if (options?.signal)
|
|
89
|
+
entry.setSignal(options.signal);
|
|
90
|
+
return entry.promise;
|
|
51
91
|
}
|
|
52
92
|
shift() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
93
|
+
if (this.promises.length === 0)
|
|
94
|
+
return;
|
|
95
|
+
if (this.promises.length === 1) {
|
|
96
|
+
this.promises.shift();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
this.promises.shift();
|
|
100
|
+
this.promises[0].use();
|
|
56
101
|
}
|
|
57
102
|
};
|
|
58
103
|
__name(AsyncQueue, "AsyncQueue");
|
package/dist/index.global.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/lib/AsyncQueue.ts"],"sourcesContent":["export * from './lib/AsyncQueue';\n","/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises:
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"sourcesContent":["export * from './lib/AsyncQueue';\n","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\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\n\t\tthis.resolve();\n\t\treturn this;\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 remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\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 * @returns Whether or not there was an element pending to process.\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\nexport interface AsyncQueueWaitOptions {\n\tsignal?: AbortSignal | undefined | null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;;;ACKO,MAAM,kBAAN,MAAsB;AAAA,IAQ5B,AAAO,YAAY,OAAmB;AAPtC,0BAAgB;AAChB,0BAAQ;AACR,0BAAQ;AACR,0BAAiB;AACjB,0BAAQ,UAAqC;AAC7C,0BAAQ,kBAAsC;AAG7C,WAAK,QAAQ;AACb,WAAK,UAAU,IAAI,QAAQ,CAAC,SAAS,WAAW;AAC/C,aAAK,UAAU;AACf,aAAK,SAAS;AAAA,MACf,CAAC;AAAA,IACF;AAAA,IAEA,AAAO,UAAU,QAAqB;AACrC,UAAI,OAAO;AAAS,eAAO;AAE3B,WAAK,SAAS;AACd,WAAK,iBAAiB,MAAM;AAC3B,cAAM,QAAQ,KAAK,MAAM,YAAY,QAAQ,IAAI;AACjD,YAAI,UAAU;AAAI,eAAK,MAAM,YAAY,OAAO,OAAO,CAAC;AAExD,aAAK,OAAO,IAAI,MAAM,0BAA0B,CAAC;AAAA,MAClD;AACA,WAAK,OAAO,iBAAiB,SAAS,KAAK,cAAc;AACzD,aAAO;AAAA,IACR;AAAA,IAEA,AAAO,MAAM;AACZ,UAAI,KAAK,QAAQ;AAChB,aAAK,OAAO,oBAAoB,SAAS,KAAK,cAAe;AAC7D,aAAK,SAAS;AACd,aAAK,iBAAiB;AAAA,MACvB;AAEA,WAAK,QAAQ;AACb,aAAO;AAAA,IACR;AAAA,EACD;AAxCa;;;ACAN,MAAM,aAAN,MAAiB;AAAA,IAAjB;AAWN,0BAAQ,YAA8B,CAAC;AAAA;AAAA,IAPvC,IAAW,YAAoB;AAC9B,aAAO,KAAK,SAAS;AAAA,IACtB;AAAA,IA4BA,AAAO,KAAK,SAA0D;AACrE,YAAM,QAAQ,IAAI,gBAAgB,IAAI;AAEtC,UAAI,KAAK,SAAS,WAAW,GAAG;AAC/B,aAAK,SAAS,KAAK,KAAK;AACxB,eAAO,QAAQ,QAAQ;AAAA,MACxB;AAEA,WAAK,SAAS,KAAK,KAAK;AACxB,UAAI,SAAS;AAAQ,cAAM,UAAU,QAAQ,MAAM;AACnD,aAAO,MAAM;AAAA,IACd;AAAA,IAMA,AAAO,QAAc;AACpB,UAAI,KAAK,SAAS,WAAW;AAAG;AAChC,UAAI,KAAK,SAAS,WAAW,GAAG;AAE/B,aAAK,SAAS,MAAM;AACpB;AAAA,MACD;AAIA,WAAK,SAAS,MAAM;AACpB,WAAK,SAAS,GAAG,IAAI;AAAA,IACtB;AAAA,EACD;AAhEa;","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
"use strict";
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -30,6 +31,46 @@ __export(src_exports, {
|
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(src_exports);
|
|
32
33
|
|
|
34
|
+
// src/lib/AsyncQueueEntry.ts
|
|
35
|
+
var AsyncQueueEntry = class {
|
|
36
|
+
constructor(queue) {
|
|
37
|
+
__publicField(this, "promise");
|
|
38
|
+
__publicField(this, "resolve");
|
|
39
|
+
__publicField(this, "reject");
|
|
40
|
+
__publicField(this, "queue");
|
|
41
|
+
__publicField(this, "signal", null);
|
|
42
|
+
__publicField(this, "signalListener", null);
|
|
43
|
+
this.queue = queue;
|
|
44
|
+
this.promise = new Promise((resolve, reject) => {
|
|
45
|
+
this.resolve = resolve;
|
|
46
|
+
this.reject = reject;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
setSignal(signal) {
|
|
50
|
+
if (signal.aborted)
|
|
51
|
+
return this;
|
|
52
|
+
this.signal = signal;
|
|
53
|
+
this.signalListener = () => {
|
|
54
|
+
const index = this.queue["promises"].indexOf(this);
|
|
55
|
+
if (index !== -1)
|
|
56
|
+
this.queue["promises"].splice(index, 1);
|
|
57
|
+
this.reject(new Error("Request aborted manually"));
|
|
58
|
+
};
|
|
59
|
+
this.signal.addEventListener("abort", this.signalListener);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
use() {
|
|
63
|
+
if (this.signal) {
|
|
64
|
+
this.signal.removeEventListener("abort", this.signalListener);
|
|
65
|
+
this.signal = null;
|
|
66
|
+
this.signalListener = null;
|
|
67
|
+
}
|
|
68
|
+
this.resolve();
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
__name(AsyncQueueEntry, "AsyncQueueEntry");
|
|
73
|
+
|
|
33
74
|
// src/lib/AsyncQueue.ts
|
|
34
75
|
var AsyncQueue = class {
|
|
35
76
|
constructor() {
|
|
@@ -38,22 +79,26 @@ var AsyncQueue = class {
|
|
|
38
79
|
get remaining() {
|
|
39
80
|
return this.promises.length;
|
|
40
81
|
}
|
|
41
|
-
wait() {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
resolve
|
|
46
|
-
}
|
|
47
|
-
this.promises.push(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return next;
|
|
82
|
+
wait(options) {
|
|
83
|
+
const entry = new AsyncQueueEntry(this);
|
|
84
|
+
if (this.promises.length === 0) {
|
|
85
|
+
this.promises.push(entry);
|
|
86
|
+
return Promise.resolve();
|
|
87
|
+
}
|
|
88
|
+
this.promises.push(entry);
|
|
89
|
+
if (options?.signal)
|
|
90
|
+
entry.setSignal(options.signal);
|
|
91
|
+
return entry.promise;
|
|
52
92
|
}
|
|
53
93
|
shift() {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
94
|
+
if (this.promises.length === 0)
|
|
95
|
+
return;
|
|
96
|
+
if (this.promises.length === 1) {
|
|
97
|
+
this.promises.shift();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.promises.shift();
|
|
101
|
+
this.promises[0].use();
|
|
57
102
|
}
|
|
58
103
|
};
|
|
59
104
|
__name(AsyncQueue, "AsyncQueue");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/lib/AsyncQueue.ts"],"sourcesContent":["export * from './lib/AsyncQueue';\n","/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises:
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"sourcesContent":["export * from './lib/AsyncQueue';\n","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\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\n\t\tthis.resolve();\n\t\treturn this;\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 remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\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 * @returns Whether or not there was an element pending to process.\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\nexport interface AsyncQueueWaitOptions {\n\tsignal?: AbortSignal | undefined | null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,kBAAN,MAAsB;AAAA,EAQ5B,AAAO,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,EAEA,AAAO,UAAU,QAAqB;AACrC,QAAI,OAAO;AAAS,aAAO;AAE3B,SAAK,SAAS;AACd,SAAK,iBAAiB,MAAM;AAC3B,YAAM,QAAQ,KAAK,MAAM,YAAY,QAAQ,IAAI;AACjD,UAAI,UAAU;AAAI,aAAK,MAAM,YAAY,OAAO,OAAO,CAAC;AAExD,WAAK,OAAO,IAAI,MAAM,0BAA0B,CAAC;AAAA,IAClD;AACA,SAAK,OAAO,iBAAiB,SAAS,KAAK,cAAc;AACzD,WAAO;AAAA,EACR;AAAA,EAEA,AAAO,MAAM;AACZ,QAAI,KAAK,QAAQ;AAChB,WAAK,OAAO,oBAAoB,SAAS,KAAK,cAAe;AAC7D,WAAK,SAAS;AACd,WAAK,iBAAiB;AAAA,IACvB;AAEA,SAAK,QAAQ;AACb,WAAO;AAAA,EACR;AACD;AAxCa;;;ACAN,IAAM,aAAN,MAAiB;AAAA,EAAjB;AAWN,wBAAQ,YAA8B,CAAC;AAAA;AAAA,EAPvC,IAAW,YAAoB;AAC9B,WAAO,KAAK,SAAS;AAAA,EACtB;AAAA,EA4BA,AAAO,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,EAMA,AAAO,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,GAAG,IAAI;AAAA,EACtB;AACD;AAhEa;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,46 @@ var __publicField = (obj, key, value) => {
|
|
|
6
6
|
return value;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
// src/lib/AsyncQueueEntry.ts
|
|
10
|
+
var AsyncQueueEntry = class {
|
|
11
|
+
constructor(queue) {
|
|
12
|
+
__publicField(this, "promise");
|
|
13
|
+
__publicField(this, "resolve");
|
|
14
|
+
__publicField(this, "reject");
|
|
15
|
+
__publicField(this, "queue");
|
|
16
|
+
__publicField(this, "signal", null);
|
|
17
|
+
__publicField(this, "signalListener", null);
|
|
18
|
+
this.queue = queue;
|
|
19
|
+
this.promise = new Promise((resolve, reject) => {
|
|
20
|
+
this.resolve = resolve;
|
|
21
|
+
this.reject = reject;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
setSignal(signal) {
|
|
25
|
+
if (signal.aborted)
|
|
26
|
+
return this;
|
|
27
|
+
this.signal = signal;
|
|
28
|
+
this.signalListener = () => {
|
|
29
|
+
const index = this.queue["promises"].indexOf(this);
|
|
30
|
+
if (index !== -1)
|
|
31
|
+
this.queue["promises"].splice(index, 1);
|
|
32
|
+
this.reject(new Error("Request aborted manually"));
|
|
33
|
+
};
|
|
34
|
+
this.signal.addEventListener("abort", this.signalListener);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
use() {
|
|
38
|
+
if (this.signal) {
|
|
39
|
+
this.signal.removeEventListener("abort", this.signalListener);
|
|
40
|
+
this.signal = null;
|
|
41
|
+
this.signalListener = null;
|
|
42
|
+
}
|
|
43
|
+
this.resolve();
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
__name(AsyncQueueEntry, "AsyncQueueEntry");
|
|
48
|
+
|
|
9
49
|
// src/lib/AsyncQueue.ts
|
|
10
50
|
var AsyncQueue = class {
|
|
11
51
|
constructor() {
|
|
@@ -14,22 +54,26 @@ var AsyncQueue = class {
|
|
|
14
54
|
get remaining() {
|
|
15
55
|
return this.promises.length;
|
|
16
56
|
}
|
|
17
|
-
wait() {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
resolve
|
|
22
|
-
}
|
|
23
|
-
this.promises.push(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return next;
|
|
57
|
+
wait(options) {
|
|
58
|
+
const entry = new AsyncQueueEntry(this);
|
|
59
|
+
if (this.promises.length === 0) {
|
|
60
|
+
this.promises.push(entry);
|
|
61
|
+
return Promise.resolve();
|
|
62
|
+
}
|
|
63
|
+
this.promises.push(entry);
|
|
64
|
+
if (options?.signal)
|
|
65
|
+
entry.setSignal(options.signal);
|
|
66
|
+
return entry.promise;
|
|
28
67
|
}
|
|
29
68
|
shift() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
69
|
+
if (this.promises.length === 0)
|
|
70
|
+
return;
|
|
71
|
+
if (this.promises.length === 1) {
|
|
72
|
+
this.promises.shift();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
this.promises.shift();
|
|
76
|
+
this.promises[0].use();
|
|
33
77
|
}
|
|
34
78
|
};
|
|
35
79
|
__name(AsyncQueue, "AsyncQueue");
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/AsyncQueue.ts"],"sourcesContent":["/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises:
|
|
1
|
+
{"version":3,"sources":["../src/lib/AsyncQueueEntry.ts","../src/lib/AsyncQueue.ts"],"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\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\n\t\tthis.resolve();\n\t\treturn this;\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 remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\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 * @returns Whether or not there was an element pending to process.\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\nexport interface AsyncQueueWaitOptions {\n\tsignal?: AbortSignal | undefined | null;\n}\n"],"mappings":";;;;;;;;;AAKO,IAAM,kBAAN,MAAsB;AAAA,EAQ5B,AAAO,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,EAEA,AAAO,UAAU,QAAqB;AACrC,QAAI,OAAO;AAAS,aAAO;AAE3B,SAAK,SAAS;AACd,SAAK,iBAAiB,MAAM;AAC3B,YAAM,QAAQ,KAAK,MAAM,YAAY,QAAQ,IAAI;AACjD,UAAI,UAAU;AAAI,aAAK,MAAM,YAAY,OAAO,OAAO,CAAC;AAExD,WAAK,OAAO,IAAI,MAAM,0BAA0B,CAAC;AAAA,IAClD;AACA,SAAK,OAAO,iBAAiB,SAAS,KAAK,cAAc;AACzD,WAAO;AAAA,EACR;AAAA,EAEA,AAAO,MAAM;AACZ,QAAI,KAAK,QAAQ;AAChB,WAAK,OAAO,oBAAoB,SAAS,KAAK,cAAe;AAC7D,WAAK,SAAS;AACd,WAAK,iBAAiB;AAAA,IACvB;AAEA,SAAK,QAAQ;AACb,WAAO;AAAA,EACR;AACD;AAxCa;;;ACAN,IAAM,aAAN,MAAiB;AAAA,EAAjB;AAWN,wBAAQ,YAA8B,CAAC;AAAA;AAAA,EAPvC,IAAW,YAAoB;AAC9B,WAAO,KAAK,SAAS;AAAA,EACtB;AAAA,EA4BA,AAAO,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,EAMA,AAAO,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,GAAG,IAAI;AAAA,EACtB;AACD;AAhEa;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/async-queue",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1-next.63e15eb.0",
|
|
4
4
|
"description": "Sequential asynchronous lock-based queue for promises",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"test": "vitest run",
|
|
21
21
|
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
|
|
22
|
-
"build": "tsup
|
|
22
|
+
"build": "tsup",
|
|
23
23
|
"prepack": "yarn build",
|
|
24
24
|
"bump": "cliff-jumper",
|
|
25
25
|
"check-update": "cliff-jumper --dry-run"
|
|
@@ -55,7 +55,8 @@
|
|
|
55
55
|
"access": "public"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"
|
|
58
|
+
"@favware/cliff-jumper": "^1.8.6",
|
|
59
|
+
"tsup": "^6.2.1",
|
|
59
60
|
"typescript": "^4.7.4"
|
|
60
61
|
}
|
|
61
62
|
}
|
package/dist/lib/AsyncQueue.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The AsyncQueue class used to sequentialize burst requests
|
|
3
|
-
*/
|
|
4
|
-
export declare class AsyncQueue {
|
|
5
|
-
/**
|
|
6
|
-
* The remaining amount of queued promises
|
|
7
|
-
*/
|
|
8
|
-
get remaining(): number;
|
|
9
|
-
/**
|
|
10
|
-
* The promises array
|
|
11
|
-
*/
|
|
12
|
-
private promises;
|
|
13
|
-
/**
|
|
14
|
-
* Waits for last promise and queues a new one
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const queue = new AsyncQueue();
|
|
18
|
-
* async function request(url, options) {
|
|
19
|
-
* await queue.wait();
|
|
20
|
-
* try {
|
|
21
|
-
* const result = await fetch(url, options);
|
|
22
|
-
* // Do some operations with 'result'
|
|
23
|
-
* } finally {
|
|
24
|
-
* // Remove first entry from the queue and resolve for the next entry
|
|
25
|
-
* queue.shift();
|
|
26
|
-
* }
|
|
27
|
-
* }
|
|
28
|
-
*
|
|
29
|
-
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
|
30
|
-
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
|
31
|
-
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
wait(): Promise<void>;
|
|
35
|
-
/**
|
|
36
|
-
* Frees the queue's lock for the next item to process
|
|
37
|
-
*/
|
|
38
|
-
shift(): void;
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=AsyncQueue.d.ts.map
|