@sapphire/async-queue 1.3.2-next.fb2ce69.0 → 1.4.0-next.c0629e7.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/dist/index.global.js +58 -14
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +58 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -14
- package/dist/index.mjs.map +1 -1
- package/dist/lib/AsyncQueue.d.ts +8 -3
- package/dist/lib/AsyncQueueEntry.d.ts +17 -0
- package/package.json +3 -3
package/dist/index.global.js
CHANGED
|
@@ -30,6 +30,46 @@ var SapphireAsyncQueue = (() => {
|
|
|
30
30
|
AsyncQueue: () => AsyncQueue
|
|
31
31
|
});
|
|
32
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
|
+
|
|
33
73
|
// src/lib/AsyncQueue.ts
|
|
34
74
|
var AsyncQueue = class {
|
|
35
75
|
constructor() {
|
|
@@ -38,22 +78,26 @@ var SapphireAsyncQueue = (() => {
|
|
|
38
78
|
get remaining() {
|
|
39
79
|
return this.promises.length;
|
|
40
80
|
}
|
|
41
|
-
wait() {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
resolve
|
|
46
|
-
}
|
|
47
|
-
this.promises.push(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
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;
|
|
52
91
|
}
|
|
53
92
|
shift() {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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();
|
|
57
101
|
}
|
|
58
102
|
};
|
|
59
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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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
|
@@ -31,6 +31,46 @@ __export(src_exports, {
|
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(src_exports);
|
|
33
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
|
+
|
|
34
74
|
// src/lib/AsyncQueue.ts
|
|
35
75
|
var AsyncQueue = class {
|
|
36
76
|
constructor() {
|
|
@@ -39,22 +79,26 @@ var AsyncQueue = class {
|
|
|
39
79
|
get remaining() {
|
|
40
80
|
return this.promises.length;
|
|
41
81
|
}
|
|
42
|
-
wait() {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
resolve
|
|
47
|
-
}
|
|
48
|
-
this.promises.push(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
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;
|
|
53
92
|
}
|
|
54
93
|
shift() {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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();
|
|
58
102
|
}
|
|
59
103
|
};
|
|
60
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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W,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,aAA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
|
2
3
|
* The AsyncQueue class used to sequentialize burst requests
|
|
3
4
|
*/
|
|
@@ -16,7 +17,7 @@ export declare class AsyncQueue {
|
|
|
16
17
|
* ```typescript
|
|
17
18
|
* const queue = new AsyncQueue();
|
|
18
19
|
* async function request(url, options) {
|
|
19
|
-
* await queue.wait();
|
|
20
|
+
* await queue.wait({ signal: options.signal });
|
|
20
21
|
* try {
|
|
21
22
|
* const result = await fetch(url, options);
|
|
22
23
|
* // Do some operations with 'result'
|
|
@@ -31,10 +32,14 @@ export declare class AsyncQueue {
|
|
|
31
32
|
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
|
32
33
|
* ```
|
|
33
34
|
*/
|
|
34
|
-
wait(): Promise<void>;
|
|
35
|
+
wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void>;
|
|
35
36
|
/**
|
|
36
|
-
*
|
|
37
|
+
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
|
38
|
+
* @returns Whether or not there was an element pending to process.
|
|
37
39
|
*/
|
|
38
40
|
shift(): void;
|
|
39
41
|
}
|
|
42
|
+
export interface AsyncQueueWaitOptions {
|
|
43
|
+
signal?: AbortSignal | undefined | null;
|
|
44
|
+
}
|
|
40
45
|
//# sourceMappingURL=AsyncQueue.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AsyncQueue } from './AsyncQueue';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare class AsyncQueueEntry {
|
|
7
|
+
readonly promise: Promise<void>;
|
|
8
|
+
private resolve;
|
|
9
|
+
private reject;
|
|
10
|
+
private readonly queue;
|
|
11
|
+
private signal;
|
|
12
|
+
private signalListener;
|
|
13
|
+
constructor(queue: AsyncQueue);
|
|
14
|
+
setSignal(signal: AbortSignal): this;
|
|
15
|
+
use(): this;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=AsyncQueueEntry.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/async-queue",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-next.c0629e7.0",
|
|
4
4
|
"description": "Sequential asynchronous lock-based queue for promises",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"access": "public"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@favware/cliff-jumper": "^1.8.
|
|
59
|
-
"tsup": "^6.1
|
|
58
|
+
"@favware/cliff-jumper": "^1.8.6",
|
|
59
|
+
"tsup": "^6.2.1",
|
|
60
60
|
"typescript": "^4.7.4"
|
|
61
61
|
}
|
|
62
62
|
}
|