Function Publish

  • Prototype property decorator factory. This decorator Publishes a newly instantiated Subject under the supplied handle and assigns it to the decorated property. Depending on the value of the suffix parameter, this newly instantiated Subject is either assigned directly to the prototype and Published using the supplied handle, or, if a truthy value is supplied for the suffix parameter, this value is assumed to reference another property of the class containing this decorated property. The first truthy value assigned to this suffix property on an instance of the class containing this Publish decorator will then be used to suffix the supplied handle upon Publishment of the newly instantiated Subject, which is assigned to the decorated instance property.

    Through these two different modes of operation, the Subject that will be Published can be assigned statically to the prototype of the class containing the decorated property, or this assignment can be deferred until an instance of the class containing the decorated property is constructed and a truthy value is assigned to its suffix property.

    This decorator is more or less the opposite of the Observe decorator, while both rely on the BusHandler to fulfill contracts. Furthermore, precautions should be taken to ensure the completion of the Published Subject as memory leaks may occur due to dangling subscriptions.

    Parameters

    • handle: Handle

      The Bus.Handle to Publish.

    • Optional suffix: PropertyKey

      An optional suffix property for the handle.

    Returns ((prototype, propertyKey) => void)

    A prototype property decorator.

      • (prototype, propertyKey): void
      • Parameters

        • prototype: object
        • propertyKey: PropertyKey

        Returns void

    Example

    Publish the 'io.github.sgrud.example' stream:

    import { Publish } from '@sgrud/bus';
    import { type Subject } from 'rxjs';

    export class Publisher {

    ⁠@Publish('io.github.sgrud.example')
    public readonly stream!: Subject<unknown>;

    }

    Publisher.prototype.stream.next('value');
    Publisher.prototype.stream.complete();

    Example

    Publish the 'io.github.sgrud.example' stream:

    import { Publish } from '@sgrud/bus';
    import { type Subject } from 'rxjs';

    export class Publisher {

    ⁠@Publish('io.github.sgrud', 'suffix')
    public readonly stream: Subject<unknown>;

    public constructor(
    private readonly suffix: string
    ) {}

    }

    const publisher = new Publisher('example');
    publisher.stream.next('value');
    publisher.stream.complete();

    See

Generated using TypeDoc