2022-01-27 01:37:54 +00:00
|
|
|
import {BusQueue, Queueable, shouldQueue, ShouldQueue} from '../types'
|
|
|
|
import {Inject, Injectable} from '../../../di'
|
|
|
|
import {Awaitable, Maybe} from '../../../util'
|
|
|
|
import {Bus} from '../Bus'
|
|
|
|
import {PushingToQueue} from './event/PushingToQueue'
|
|
|
|
import {PushedToQueue} from './event/PushedToQueue'
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export abstract class Queue implements BusQueue {
|
|
|
|
@Inject()
|
|
|
|
protected readonly bus!: Bus
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public readonly name: string,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async dispatch<T extends Queueable>(item: T): Promise<void> {
|
|
|
|
if ( shouldQueue(item) ) {
|
2022-01-27 16:34:01 +00:00
|
|
|
await this.bus.push(new PushingToQueue(item, this.name))
|
2022-01-27 01:37:54 +00:00
|
|
|
await this.push(item)
|
2022-01-27 16:34:01 +00:00
|
|
|
await this.bus.push(new PushedToQueue(item, this.name))
|
2022-01-27 01:37:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await item.execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract push<T extends Queueable>(item: ShouldQueue<T>): Awaitable<void>
|
|
|
|
|
|
|
|
abstract pop(): Promise<Maybe<ShouldQueue<Queueable>>>
|
|
|
|
}
|