import { Transport, TransportMessage } from '../transport'; import { Event, Command, Message, MessageAttributes } from '@node-ts/bus-messages'; import { TypedEmitter, CoreDependencies, MiddlewareDispatcher } from '../util'; import { HandlerDefinition, HandlerRegistry } from '../handler'; import { BusState } from './bus-state'; import { WorkflowRegistry } from '../workflow/registry'; import { ContainerAdapter } from '../container'; import { Receiver } from '../receiver'; export interface BeforeSend { command: Command; attributes: MessageAttributes; } export interface BeforePublish { event: Event; attributes: MessageAttributes; } export interface AfterSend { command: Command; attributes?: MessageAttributes; } export interface AfterPublish { event: Event; attributes?: MessageAttributes; } export interface OnError { message: Message; error: Error; attributes?: MessageAttributes; rawMessage?: TransportMessage; } export interface AfterReceive { message: TransportMessage; } export interface BeforeDispatch { message: Message; attributes: MessageAttributes; handlers: HandlerDefinition[]; } export interface AfterDispatch { message: Message; attributes: MessageAttributes; } export declare class BusInstance { private readonly transport; private readonly concurrency; private readonly workflowRegistry; private readonly coreDependencies; private readonly messageReadMiddleware; private readonly handlerRegistry; private readonly container; private readonly sendOnly; private readonly receiver; /** * Emitted before a command is sent to the transport */ readonly beforeSend: TypedEmitter; /** * Emitted before an event is published to the transport */ readonly beforePublish: TypedEmitter; /** * Emitted after a command has been sent to the transport */ readonly afterSend: TypedEmitter; /** * Emitted after an event has been published to the transport */ readonly afterPublish: TypedEmitter; /** * Emitted when an error occurs during message handling */ readonly onError: TypedEmitter>; /** * Emitted immediately after a message has been received from the transport */ readonly afterReceive: TypedEmitter>; /** * Emitted before a message is dispatched to handlers */ readonly beforeDispatch: TypedEmitter; /** * Emitted after a message has been dispatched and completed all handler invocations */ readonly afterDispatch: TypedEmitter; private internalState; private runningWorkerCount; private logger; private isInitialized; private outbox; constructor(transport: Transport, concurrency: number, workflowRegistry: WorkflowRegistry, coreDependencies: CoreDependencies, messageReadMiddleware: MiddlewareDispatcher>, handlerRegistry: HandlerRegistry, container: ContainerAdapter | undefined, sendOnly: boolean, receiver: Receiver | undefined); /** * Receive one or more messages to dispatch directly to handlers. This can only be called when a Receiver * has been configured using Bus.configure().withReceiver() */ receive(message: unknown): Promise; /** * Initializes the bus with the provided configuration. This must be called before `.start()` * * @throws InvalidOperation if the bus has already been initialized */ initialize(): Promise; /** * Publishes an event to the transport * @param event An event to publish * @param messageAttributes A set of attributes to attach to the outgoing message when published */ publish(event: TEvent, messageAttributes?: Partial): Promise; /** * Sends a command to the transport * @param command A command to send * @param messageAttributes A set of attributes to attach to the outgoing message when sent */ send(command: TCommand, messageAttributes?: Partial): Promise; /** * Instructs the bus that the current message being handled cannot be processed even with * retries and instead should immediately be routed to the dead letter queue * @throws FailMessageOutsideHandlingContext if called outside a message handling context */ failMessage(): Promise; /** * Instructs that the current message should be returned to the queue for retry. * @throws ReturnMessageOutsideHandlingContext if called outside a message handling context */ returnMessage(): Promise; /** * Instructs the bus to start reading messages from the underlying service queue * and dispatching to message handlers. * * @throws InvalidOperation if the bus is configured to be send-only * @throws InvalidOperation if the bus has not been initialized * @throws InvalidOperation if the bus has a receiver set * @throws InvalidBusState if the bus is already started or in a starting state */ start(): Promise; /** * Stops a bus that has been started by `.start()`. This will wait for all running workers to complete * their current message handling contexts before returning. * * @throws InvalidBusState if the bus is already stopped or stopping */ stop(): Promise; /** * Stops and disposes all resources allocated to the bus, as well as removing * all handler registrations. * * The bus instance can not be used after this has been called. */ dispose(): Promise; /** * Gets the current state of a message-handling bus */ get state(): BusState; private applicationLoop; private handleNextMessage; private handleReceivedMessage; private dispatchMessageToHandlers; private prepareTransportOptions; dispatchMessageToHandler(message: Message, attributes: MessageAttributes, handler: HandlerDefinition): Promise; /** * The final middleware that runs, after all the useBeforeHandleNextMessage middlewares have completed * It dispatches a message that has been polled from the queue * and deletes the message from the transport */ private handleNextMessagePolled; /** * Subscribes to the interrupt signals to gracefully stop the bus */ private subscribeToInterruptSignals; }