feat: initial Papo bot scaffold
This commit is contained in:
commit
000481a3b0
12168 files changed
+1584750
No files matched your search
+137
@@ -0,0 +1,137 @@
|
||||
'use strict';
|
||||
|
||||
var __defProp = Object.defineProperty;
|
||||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
||||
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
||||
|
||||
// src/lib/_AsyncQueueEntry.ts
|
||||
var _AsyncQueueEntry = class _AsyncQueueEntry {
|
||||
constructor(queue) {
|
||||
__publicField(this, "promise");
|
||||
__publicField(this, "resolve");
|
||||
__publicField(this, "reject");
|
||||
__publicField(this, "queue");
|
||||
__publicField(this, "signal", null);
|
||||
__publicField(this, "signalListener", null);
|
||||
this.queue = queue;
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
});
|
||||
}
|
||||
setSignal(signal) {
|
||||
if (signal.aborted) return this;
|
||||
this.signal = signal;
|
||||
this.signalListener = () => {
|
||||
const index = this.queue["promises"].indexOf(this);
|
||||
if (index !== -1) this.queue["promises"].splice(index, 1);
|
||||
this.reject(new Error("Request aborted manually"));
|
||||
};
|
||||
this.signal.addEventListener("abort", this.signalListener);
|
||||
return this;
|
||||
}
|
||||
use() {
|
||||
this.dispose();
|
||||
this.resolve();
|
||||
return this;
|
||||
}
|
||||
abort() {
|
||||
this.dispose();
|
||||
this.reject(new Error("Request aborted manually"));
|
||||
return this;
|
||||
}
|
||||
dispose() {
|
||||
if (this.signal) {
|
||||
this.signal.removeEventListener("abort", this.signalListener);
|
||||
this.signal = null;
|
||||
this.signalListener = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
__name(_AsyncQueueEntry, "AsyncQueueEntry");
|
||||
var AsyncQueueEntry = _AsyncQueueEntry;
|
||||
|
||||
// src/lib/AsyncQueue.ts
|
||||
var _AsyncQueue = class _AsyncQueue {
|
||||
constructor() {
|
||||
/**
|
||||
* The promises array
|
||||
*/
|
||||
__publicField(this, "promises", []);
|
||||
}
|
||||
/**
|
||||
* The amount of entries in the queue, including the head.
|
||||
* @seealso {@link queued} for the queued count.
|
||||
*/
|
||||
get remaining() {
|
||||
return this.promises.length;
|
||||
}
|
||||
/**
|
||||
* The amount of queued entries.
|
||||
* @seealso {@link remaining} for the count with the head.
|
||||
*/
|
||||
get queued() {
|
||||
return this.remaining === 0 ? 0 : this.remaining - 1;
|
||||
}
|
||||
/**
|
||||
* Waits for last promise and queues a new one
|
||||
* @example
|
||||
* ```typescript
|
||||
* const queue = new AsyncQueue();
|
||||
* async function request(url, options) {
|
||||
* await queue.wait({ signal: options.signal });
|
||||
* try {
|
||||
* const result = await fetch(url, options);
|
||||
* // Do some operations with 'result'
|
||||
* } finally {
|
||||
* // Remove first entry from the queue and resolve for the next entry
|
||||
* queue.shift();
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
||||
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
||||
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
||||
* ```
|
||||
*/
|
||||
wait(options) {
|
||||
const entry = new AsyncQueueEntry(this);
|
||||
if (this.promises.length === 0) {
|
||||
this.promises.push(entry);
|
||||
return Promise.resolve();
|
||||
}
|
||||
this.promises.push(entry);
|
||||
if (options?.signal) entry.setSignal(options.signal);
|
||||
return entry.promise;
|
||||
}
|
||||
/**
|
||||
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
||||
*/
|
||||
shift() {
|
||||
if (this.promises.length === 0) return;
|
||||
if (this.promises.length === 1) {
|
||||
this.promises.shift();
|
||||
return;
|
||||
}
|
||||
this.promises.shift();
|
||||
this.promises[0].use();
|
||||
}
|
||||
/**
|
||||
* Aborts all the pending promises.
|
||||
* @note To avoid race conditions, this does **not** unlock the head lock.
|
||||
*/
|
||||
abortAll() {
|
||||
if (this.queued === 0) return;
|
||||
for (let i = 1; i < this.promises.length; ++i) {
|
||||
this.promises[i].abort();
|
||||
}
|
||||
this.promises.length = 1;
|
||||
}
|
||||
};
|
||||
__name(_AsyncQueue, "AsyncQueue");
|
||||
var AsyncQueue = _AsyncQueue;
|
||||
|
||||
exports.AsyncQueue = AsyncQueue;
|
||||
//# sourceMappingURL=index.cjs.map
|
||||
//# sourceMappingURL=index.cjs.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../src/lib/_AsyncQueueEntry.ts","../../src/lib/AsyncQueue.ts"],"names":[],"mappings":";;;;;;;;AAKO,IAAM,gBAAA,GAAN,MAAM,gBAAgB,CAAA;AAAA,EAQrB,YAAY,KAAmB,EAAA;AAPtC,IAAgB,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAChB,IAAQ,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AACR,IAAQ,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA;AACR,IAAiB,aAAA,CAAA,IAAA,EAAA,OAAA,CAAA;AACjB,IAAA,aAAA,CAAA,IAAA,EAAQ,QAAqC,EAAA,IAAA,CAAA;AAC7C,IAAA,aAAA,CAAA,IAAA,EAAQ,gBAAsC,EAAA,IAAA,CAAA;AAG7C,IAAA,IAAA,CAAK,KAAQ,GAAA,KAAA;AACb,IAAA,IAAA,CAAK,OAAU,GAAA,IAAI,OAAQ,CAAA,CAAC,SAAS,MAAW,KAAA;AAC/C,MAAA,IAAA,CAAK,OAAU,GAAA,OAAA;AACf,MAAA,IAAA,CAAK,MAAS,GAAA,MAAA;AAAA,KACd,CAAA;AAAA;AACF,EAEO,UAAU,MAAqB,EAAA;AACrC,IAAI,IAAA,MAAA,CAAO,SAAgB,OAAA,IAAA;AAE3B,IAAA,IAAA,CAAK,MAAS,GAAA,MAAA;AACd,IAAA,IAAA,CAAK,iBAAiB,MAAM;AAC3B,MAAA,MAAM,QAAQ,IAA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A,CAAA,GAAI,KAAK,QAAS,CAAA,MAAA,EAAQ,EAAE,CAAG,EAAA;AAC9C,MAAK,IAAA,CAAA,QAAA,CAAS,CAAC,CAAA,CAAE,KAAM,EAAA;AAAA;AAGxB,IAAA,IAAA,CAAK,SAAS,MAAS,GAAA,CAAA;AAAA;AAEzB,CAAA;AAzFwB,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA;AAAjB,IAAM,UAAN,GAAA","file":"index.cjs","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.\nLine truncated
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* The AsyncQueue class used to sequentialize burst requests
|
||||
*/
|
||||
declare class AsyncQueue {
|
||||
/**
|
||||
* The amount of entries in the queue, including the head.
|
||||
* @seealso {@link queued} for the queued count.
|
||||
*/
|
||||
get remaining(): number;
|
||||
/**
|
||||
* The amount of queued entries.
|
||||
* @seealso {@link remaining} for the count with the head.
|
||||
*/
|
||||
get queued(): number;
|
||||
/**
|
||||
* The promises array
|
||||
*/
|
||||
private readonly promises;
|
||||
/**
|
||||
* Waits for last promise and queues a new one
|
||||
* @example
|
||||
* ```typescript
|
||||
* const queue = new AsyncQueue();
|
||||
* async function request(url, options) {
|
||||
* await queue.wait({ signal: options.signal });
|
||||
* try {
|
||||
* const result = await fetch(url, options);
|
||||
* // Do some operations with 'result'
|
||||
* } finally {
|
||||
* // Remove first entry from the queue and resolve for the next entry
|
||||
* queue.shift();
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* request(someUrl1, someOptions1); // Will call fetch() immediately
|
||||
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
|
||||
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
|
||||
* ```
|
||||
*/
|
||||
wait(options?: Readonly<AsyncQueueWaitOptions>): Promise<void>;
|
||||
/**
|
||||
* Unlocks the head lock and transfers the next lock (if any) to the head.
|
||||
*/
|
||||
shift(): void;
|
||||
/**
|
||||
* Aborts all the pending promises.
|
||||
* @note To avoid race conditions, this does **not** unlock the head lock.
|
||||
*/
|
||||
abortAll(): void;
|
||||
}
|
||||
interface AsyncQueueWaitOptions {
|
||||
signal?: AbortSignal | undefined | null;
|
||||
}
|
||||
|
||||
export { AsyncQueue, type AsyncQueueWaitOptions };
|
||||
Reference in new issue
Block a user