@lppedd/message-bus - v0.10.0
    Preparing search index...

    Interface MessageInterceptor

    A message interceptor that allows observing, modifying, or preventing message dispatch before messages are dispatched to topic subscribers.

    A MessageInterceptor can:

    • Veto (cancel) message dispatching entirely via isVetoed.
    • Wrap or replace the topic's handler invocation logic via handler.

    Message interceptors are registered using MessageBus.addInterceptor.

    interface MessageInterceptor {
        handler: (
            topic: Topic,
            next: MessageHandler,
            data: unknown,
            ...other: any[],
        ) => unknown;
        isVetoed?: (topic: Topic, data: unknown) => boolean | Promise<boolean>;
    }
    Index

    Properties

    Properties

    handler: (
        topic: Topic,
        next: MessageHandler,
        data: unknown,
        ...other: any[],
    ) => unknown

    A function to optionally wrap or replace the original topic's subscription handler.

    This method is invoked before the topic's subscriber handler executes. Implementations can transform the message payload, inject behavior (such as logging or timing), or completely replace the handler logic.

    The returned value, or resolved promise value, becomes the handler's result.

    Type Declaration

      • (topic: Topic, next: MessageHandler, data: unknown, ...other: any[]): unknown
      • Parameters

        • topic: Topic

          The topic being published to.

        • next: MessageHandler

          The original MessageHandler registered for the topic. The interceptor may call it to invoke the next handler in the chain.

        • data: unknown

          The message payload associated with the publication.

        • ...other: any[]

          Optional additional message handler arguments injected by the interceptor chain.

        Returns unknown

    const perfInterceptor: MessageInterceptor = {
    handler: async (topic, data, next) => {
    const start = performance.now();
    const result = await next(data);
    const duration = performance.now() - start;
    console.log(`Handler for ${topic} took ${duration.toFixed(2)} ms`);
    return result;
    },
    };
    isVetoed?: (topic: Topic, data: unknown) => boolean | Promise<boolean>

    An optional function to determine whether a message for the given topic should be vetoed (prevented from being dispatched to subscribers).

    If this method returns or resolves to true, the message will not be dispatched to any subscribers, and other interceptors will not be evaluated.

    Type Declaration

      • (topic: Topic, data: unknown): boolean | Promise<boolean>
      • Parameters

        • topic: Topic

          The topic being published to.

        • data: unknown

          The message payload associated with the publication.

        Returns boolean | Promise<boolean>

    const authInterceptor: MessageInterceptor = {
    isVetoed: async (topic, data) => {
    const user = await getCurrentUser();
    return !user.hasPermission(topic);
    },
    handler: (_, data, next) => next(data),
    };