harmony/src/rest/queue.ts

38 lines
860 B
TypeScript
Raw Normal View History

2021-04-08 08:52:33 +00:00
// 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()
}
}