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

    Interface SubscriptionBuilder

    Allows creating customized subscriptions.

    interface SubscriptionBuilder {
        subscribe<T>(topic: Topic<T>): LazyAsyncSubscription<T>;
        subscribe<T extends [any, ...any[]]>(
            topics: Topics<T>,
        ): LazyAsyncSubscription<T[number]>;
        subscribe<T>(topic: Topic<T>, handler: MessageHandler<T>): Subscription;
        subscribe<T extends [any, ...any[]]>(
            topics: Topics<T>,
            handler: MessageHandler<T[number]>,
        ): Subscription;
        subscribeOnce<T>(topic: Topic<T>): Promise<T>;
        subscribeOnce<T extends [any, ...any[]]>(
            topics: Topics<T>,
        ): Promise<T[number]>;
        subscribeOnce<T>(topic: Topic<T>, handler: MessageHandler<T>): Subscription;
        subscribeOnce<T extends [any, ...any[]]>(
            topics: Topics<T>,
            handler: MessageHandler<T[number]>,
        ): Subscription;
        withLimit(limit: number): SubscriptionBuilder;
        withPriority(priority: number): SubscriptionBuilder;
    }
    Index

    Methods

    • Creates a lazily-initialized subscription to the specified topic that is also an AsyncIterableIterator.

      This allows consuming published messages using the for await ... of syntax. If an async iteration completes or ends early (e.g., via break, return, or an error), the subscription is automatically disposed.

      The subscription is created lazily: it is only registered when the first call to next() or single() occurs. If iteration never begins, no subscription is created.

      Type Parameters

      • T

      Parameters

      • topic: Topic<T>

        The topic to subscribe to.

      Returns LazyAsyncSubscription<T>

      const subscription = messageBus.withLimit(3).subscribe(CommandTopic);

      // Will iterate 3 times max
      for await (const command of subscription) {
      switch (command) {
      case "shutdown":
      // ...
      break;
      case "restart":
      // ...
      break;
      }
      }
    • Type Parameters

      • T extends [any, ...any[]]

      Parameters

      Returns LazyAsyncSubscription<T[number]>

    • Subscribes to the specified topic with a callback.

      The subscription is established immediately, and stays active until disposal.

      Type Parameters

      • T

      Parameters

      • topic: Topic<T>

        The topic to subscribe to.

      • handler: MessageHandler<T>

        A callback invoked on each topic message.

      Returns Subscription

      // The message handler will be invoked 3 times max
      const subscription = messageBus.withLimit(3).subscribe(CommandTopic, (command) => {
      switch (command) {
      case "shutdown":
      // ...
      break;
      case "restart":
      // ...
      break;
      }
      });
    • Type Parameters

      • T extends [any, ...any[]]

      Parameters

      Returns Subscription

    • Subscribes once to the specified topic, returning a promise that resolves with the next published message.

      The subscription will be automatically disposed after receiving the first message. Useful for awaiting a single message without manually managing the subscription.

      Type Parameters

      • T

      Parameters

      • topic: Topic<T>

        The topic to subscribe to.

      Returns Promise<T>

      const command = await messageBus.withPriority(0).subscribeOnce(CommandTopic);
      console.log(`Received command: ${command}`);
    • Type Parameters

      • T extends [any, ...any[]]

      Parameters

      Returns Promise<T[number]>

    • Subscribes once to the specified topic with a callback.

      The callback is invoked with the next message, after which the subscription is disposed.

      Type Parameters

      • T

      Parameters

      • topic: Topic<T>

        The topic to subscribe to.

      • handler: MessageHandler<T>

        A callback invoked on the next topic message.

      Returns Subscription

      // Automatically unsubscribes after the next message
      messageBus.withPriority(0).subscribeOnce(CommandTopic, (command) => {
      console.log(`Received command: ${command}`);
      });
    • Type Parameters

      • T extends [any, ...any[]]

      Parameters

      Returns Subscription

    • Sets the maximum number of messages to receive for the next subscription.

      When the specified limit is reached, the subscription is automatically disposed.

      Parameters

      • limit: number

        The maximum number of messages to receive.

      Returns SubscriptionBuilder

    • Sets the priority for the next subscription.

      Higher priority (lower number) subscriptions are notified before lower priority (higher value) ones. The default priority value is 1.

      Parameters

      • priority: number

        A priority value, where a lower number means higher priority.

      Returns SubscriptionBuilder