38 lines
860 B
TypeScript
38 lines
860 B
TypeScript
|
// based on https://github.com/discordjs/discord.js/blob/master/src/rest/AsyncQueue.js
|
||
|
|
||
|
export interface RequestPromise {
|
||
|
resolve: CallableFunction
|
||
|
promise: Promise<any>
|
||
|
}
|
||
|
|
||
|
export class RequestQueue {
|
||
|
promises: RequestPromise[] = []
|
||
|
|
||
|
get remaining(): number {
|
||
|
return this.promises.length
|
||
|
}
|
||
|
|
||
|
async wait(): Promise<any> {
|
||
|
const next =
|
||
|
this.promises.length !== 0
|
||
|
? this.promises[this.promises.length - 1].promise
|
||
|
: Promise.resolve()
|
||
|
let resolveFn: CallableFunction | undefined
|
||
|
const promise = new Promise((resolve) => {
|
||
|
resolveFn = resolve
|
||
|
})
|
||
|
|
||
|
this.promises.push({
|
||
|
resolve: resolveFn!,
|
||
|
promise
|
||
|
})
|
||
|
|
||
|
return next
|
||
|
}
|
||
|
|
||
|
shift(): void {
|
||
|
const deferred = this.promises.shift()
|
||
|
if (typeof deferred !== 'undefined') deferred.resolve()
|
||
|
}
|
||
|
}
|