UNPKG

@arpinum/messaging

Version:
40 lines (39 loc) 1.57 kB
import { Message, MessageBus, MessageHandler } from "./types"; export interface MessageBusOptions { log?: (...args: unknown[]) => void; exclusiveHandlers?: boolean; ensureAtLeastOneHandler?: boolean; handlersConcurrency?: number; beforePost?: MessageHookOption[]; afterPost?: ResultsHookOption[]; beforeHandle?: MessageHookOption[]; afterHandle?: ResultHookOption[]; } export type MessageBusSafeOptions = Required<MessageBusOptions>; export type MessageHookOption = (message: Message) => Promise<Message> | Message; export type ResultHookOption = (result: unknown) => Promise<unknown> | unknown; export type ResultsHookOption = (result: unknown[]) => Promise<unknown[]> | unknown[]; export declare class DefaultMessageBus implements MessageBus { private options; private handlerMap; private readonly beforeHandle; private readonly afterHandle; private readonly beforePost; private readonly afterPost; constructor(options?: MessageBusOptions); private validateOptions; postAll(messages: Message[]): Promise<unknown[]>; post(message: Message): Promise<unknown[]>; private postToHandlers; private validatedMessage; private checkHandlerRequirements; private postForExclusiveHandlers; private postForStandardHandlers; private handle; register(type: string, handler: MessageHandler): () => void; private ensureHandlerExclusivity; unregisterAll(...types: string[]): void; handlerCount(type: string): number; private handlersFor; private updateHandlers; }