mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
Update types for Queue
This commit is contained in:
parent
772d13fd94
commit
b254022db2
1 changed files with 10 additions and 9 deletions
|
@ -1,23 +1,24 @@
|
||||||
import { SECONDS } from "./utils";
|
import { SECONDS } from "./utils";
|
||||||
|
|
||||||
type QueueFn = (...args: any[]) => Promise<any>;
|
type InternalQueueFn = () => Promise<void>;
|
||||||
|
type AnyFn = (...args: any[]) => any;
|
||||||
|
|
||||||
const DEFAULT_TIMEOUT = 10 * SECONDS;
|
const DEFAULT_TIMEOUT = 10 * SECONDS;
|
||||||
|
|
||||||
export class Queue {
|
export class Queue<TQueueFunction extends AnyFn = AnyFn> {
|
||||||
protected running: boolean = false;
|
protected running = false;
|
||||||
protected queue: QueueFn[] = [];
|
protected queue: InternalQueueFn[] = [];
|
||||||
protected timeout: number;
|
protected timeout: number;
|
||||||
|
|
||||||
constructor(timeout = DEFAULT_TIMEOUT) {
|
constructor(timeout = DEFAULT_TIMEOUT) {
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public add(fn) {
|
public add(fn: TQueueFunction): Promise<void> {
|
||||||
const promise = new Promise(resolve => {
|
const promise = new Promise<void>(resolve => {
|
||||||
this.queue.push(async () => {
|
this.queue.push(async () => {
|
||||||
await fn();
|
await fn();
|
||||||
resolve(undefined);
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!this.running) this.next();
|
if (!this.running) this.next();
|
||||||
|
@ -26,7 +27,7 @@ export class Queue {
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public next() {
|
public next(): void {
|
||||||
this.running = true;
|
this.running = true;
|
||||||
|
|
||||||
if (this.queue.length === 0) {
|
if (this.queue.length === 0) {
|
||||||
|
@ -37,7 +38,7 @@ export class Queue {
|
||||||
const fn = this.queue.shift()!;
|
const fn = this.queue.shift()!;
|
||||||
new Promise(resolve => {
|
new Promise(resolve => {
|
||||||
// Either fn() completes or the timeout is reached
|
// Either fn() completes or the timeout is reached
|
||||||
fn().then(resolve);
|
void fn().then(resolve);
|
||||||
setTimeout(resolve, this.timeout);
|
setTimeout(resolve, this.timeout);
|
||||||
}).then(() => this.next());
|
}).then(() => this.next());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue