import {Awaitable, JSONState, Maybe, Pipeline, TypeTag, uuid4} from '../../util' import {Instantiable, StaticInstantiable} from '../../di' // eslint-disable-next-line @typescript-eslint/no-unused-vars export interface SerialPayload extends JSONState { serializer: string payload: TSerial } export interface Serializer { matchActual(some: TActual): boolean matchSerial(serial: SerialPayload): boolean encode(actual: TActual): Awaitable> decode(serial: SerialPayload): Awaitable } export interface Event { eventUuid: string eventName: string originBusUuid?: string shouldBroadcast?: boolean | (() => Awaitable) } export abstract class BaseEvent implements Event { public readonly eventUuid = uuid4() public abstract eventName: string public shouldBroadcast(): Awaitable { return true } } export type EventHandlerReturn = Awaitable export type EventHandler = (event: T) => EventHandlerReturn export interface EventHandlerSubscription { unsubscribe(): Awaitable } export interface Queueable { execute(): Awaitable shouldQueue?: boolean | (() => boolean) defaultQueue?: string | (() => string) } export type ShouldQueue = T & TypeTag<'@extollo/lib:ShouldQueue'> export function shouldQueue(something: T): something is ShouldQueue { if ( typeof something.shouldQueue === 'function' ) { return something.shouldQueue() } return ( typeof something.shouldQueue === 'undefined' || something.shouldQueue ) } export interface EventBus { readonly uuid: string subscribe(eventKey: StaticInstantiable, handler: EventHandler): Awaitable pipe(eventKey: StaticInstantiable, line: Pipeline): Awaitable push(event: TEvent): Awaitable up(): Awaitable down(): Awaitable isConnected(): boolean } /** Internal storage format for local event bus subscribers. */ export interface BusSubscriber { uuid: string eventName: string eventKey: StaticInstantiable handler: EventHandler } export interface BusQueue { readonly name: string dispatch(item: T): Promise pop(): Promise> } export interface RedisBusConnectorConfig { type: 'redis' } export type BusConnectorConfig = RedisBusConnectorConfig export interface QueueConfig { driver?: Instantiable, /* queues?: ({ name: string, driver: Instantiable, })[] */ }