Class Bus<I, O>

The Bus class presents an easy way to establish duplex streams. Through the on-construction supplied Handle the mount point of the created duplex streaming Bus within the hierarchical structure of streams handled by the BusHandler is designated. Thereby, all Values emitted by the created Bus originate from streams beneath the supplied Handle and when invoking the next method of the implemented Observer contract, the resulting Value will originate from this supplied Handle.

An instantiated Bus allows for two modes of observation to facilitate simple and complex use cases. The implemented Subscribable contract allows for observation of the dematerialized Values, while the well-known Symbol.observable method provides a way to observe the raw Values, including their originating Handles.

Example

Using a duplex streaming Bus:

import { Bus } from '@sgrud/bus';

const bus = new Bus<string, string>('io.github.sgrud.example');

bus.subscribe({ next: console.log });
bus.next('value');
bus.complete();

Type Parameters

  • I

    The input value type of a Bus instance.

  • O

    The output value type of a Bus instance.

Implements

Constructors

  • Public Bus constructor. The Handle supplied to this constructor is assigned as readonly on the constructed Bus instance and will be used to determine the mount point of this duplex stream within the hierarchical structure of streams handled by the BusHandler.

    Type Parameters

    • I

      The input value type of a Bus instance.

    • O

      The output value type of a Bus instance.

    Parameters

    Returns Bus<I, O>

Properties

handle: Handle

The Handle to publish this Bus under.

observe: Observable<Value<O>>

The observed side of this Bus. The Observable assigned to this property is used to fullfil the Subscribable contract and is obtained through the BusHandler.

publish: Subject<I>

The publishing side of this Bus. The Subject assigned to this property is used to fullfil the Observer contract and is provided to the BusHandler for publishment.

Methods

  • Implemented complete method of the Observer contract. Invoking this method will mark the publishing side of this duplex Bus as completed.

    Returns void

    Example

    complete a Bus:

    import { Bus } from '@sgrud/bus';

    const bus = new Bus<string, string>('io.github.sgrud.example');
    bus.complete();
  • Implemented error method of the Observer contract. Invoking this method will throw the supplied error on the publishing side of this duplex Bus.

    Parameters

    • error: unknown

      The error to publish.

    Returns void

    Example

    Throw an error through a Bus:

    import { Bus } from '@sgrud/bus';

    const bus = new Bus<string, string>('io.github.sgrud.example');
    bus.error(new Error('example));
  • Implemented next method of the Observer contract. Invoking this method will provide any observer of the publishing side of this duplex Bus with the next value.

    Parameters

    Returns void

    Example

    Supplying a Bus with a next value:

    import { Bus } from '@sgrud/bus';

    const bus = new Bus<string, string>('io.github.sgrud.example');
    bus.next('value');
  • Implemented subscribe method of the Subscribable contract. Invoking this method while supplying an observer will subscribe the supplied observer to any changes on the observed side of this duplex Bus.

    Parameters

    Returns Unsubscribable

    An Unsubscribable of the ongoing observation.

    Example

    subscribe to a dematerialized Bus:

    import { Bus } from '@sgrud/bus';

    const bus = new Bus<string, string>('io.github.sgrud.example');
    bus.subscribe({ next: console.log });

Generated using TypeDoc