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(item: T): Promise { if ( shouldQueue(item) ) { await this.bus.push(new PushingToQueue(item, this.name)) await this.push(item) await this.bus.push(new PushedToQueue(item, this.name)) return } await item.execute() } protected abstract push(item: ShouldQueue): Awaitable abstract pop(): Promise>> }