Class BusHandler

The BusHandler implements and orchestrates the establishment, transferral and deconstruction of any number of Observable streams. It operates in conjunction with the BusWorker Thread which is run in the background. To designate and organize different Observable streams, the string literal helper type Bus.Handle is employed. As an example, let the following hierarchical structure be given:

io.github.sgrud
├── io.github.sgrud.core
│   ├── io.github.sgrud.core.kernel
│   └── io.github.sgrud.core.transit
├── io.github.sgrud.data
│   ├── io.github.sgrud.data.model.current
│   └── io.github.sgrud.data.model.global
└── io.github.sgrud.shell
│   └── io.github.sgrud.shell.route
└── io.github.sgrud.store
    ├── io.github.sgrud.store.global
    └── io.github.sgrud.store.local

Depending on the Bus.Handle, one may observe all established streams beneath the root io.github.sgrud Bus.Handle or only one specific stream, e.g., io.github.sgrud.core.kernel. The Observable returned from the observe method will emit all Bus.Values originating from all streams beneath the root Bus.Handle in the first case, or only Bus.Values from one stream, in the second case.

Decorator

Singleton

See

BusWorker

Constructors

Properties

Private static ReplaySubject used as the BusHandler loader. This loader emits once after the BusHandler has been successfully initialized.

worker: Thread<BusWorker>

The worker Thread and main background workhorse. The underlying BusWorker is run inside a Worker context in the background and transparently handles published and observed streams and the aggregation of their values depending on their Bus.Handle, i.e., hierarchy.

Methods

  • Invoking this method observes the Observable stream represented by the supplied handle. The method will return an Observable originating from the BusWorker which emits all Bus.Values published under the supplied handle. When the observe method is invoked with 'io.github.sgrud', all streams hierarchically beneath this Bus.Handle, e.g., 'io.github.bus.status', will also be emitted by the returned Observable.

    Type Parameters

    Parameters

    Returns Observable<Value<T>>

    An Observable bus for handle.

    Example

    observe the 'io.github.sgrud' stream:

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

    const busHandler = new BusHandler();
    const handle = 'io.github.sgrud.example';

    busHandler.observe(handle).subscribe(console.log);
  • Invoking this method publishes the supplied Observable stream under the supplied handle. This method returns an Observable of the publishment of the supplied Observable stream under the supplied handle with the BusWorker. When the published source Observable completes, the registration within the BusWorker will automatically self-destruct.

    Type Parameters

    Parameters

    Returns Observable<void>

    An Observable of the stream publishment.

    Example

    publish a stream under 'io.github.sgrud.example':

    import { BusHandler } from '@sgrud/bus';
    import { of } from 'rxjs';

    const busHandler = new BusHandler();
    const handle = 'io.github.sgrud.example';
    const stream = of('published');

    busHandler.publish(handle, stream).subscribe();
  • Invoking this method uplinks the supplied handle to the supplied url by establishing a WebSocket connection between the endpoint behind the supplied url and the BusWorker. This method returns an Observable of the uplink Subscription which can be used to cancel the uplink. When the uplinked WebSocket is closed or throws an error, it is automatically cleaned up and unsubscribed from.

    Parameters

    • handle: Handle

      The Bus.Handle to uplink.

    • url: string

      The endpoint url to establish an uplink to.

    Returns Observable<Subscription>

    An Observable of the uplink Subscription.

    Example

    uplink the 'io.github.sgrud.uplink' Bus.Handle:

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

    const busHandler = new BusHandler();
    const handle = 'io.github.sgrud.example';
    const url = 'https://example.com/websocket';

    const uplink = busHandler.uplink(handle, url).subscribe();

Generated using TypeDoc