UNPKG

ng-event-bus

Version:

RxJS-based message/event bus service for Angular.

134 lines (129 loc) 3.51 kB
import { Observable } from 'rxjs'; /** * Metadata of the messages sent through the events bus. * * @typeParam T Payload type. Defaults to `undefined` when no payload is declared. * @author Cristiam Mercado * @since 2.0.0 * @version 11.0.0 */ declare class MetaData<T = undefined> { /** * A unique identifier of the message sent through the events bus. * @private */ private readonly _id; /** * Original key associated to the message. * @private */ private readonly _key; /** * Payload associated with the message. * @private */ private readonly _data; /** * Time in milliseconds in which the message was generated. * @private */ private readonly _timestamp; /** * Constructor for this class. * * @param key Original key associated to the message sent through the events bus. * @param [data] Optional payload sent with the message. Its type is preserved as `T`. */ constructor(key: string, data?: T); /** * Gets unique identifier of the message sent through the events bus. */ get id(): string; /** * Original key associated to the message. */ get key(): string; /** * Gets the payload associated with the message. */ get data(): T; /** * Gets the time in milliseconds in which the message was generated. */ get timestamp(): number; /** * Generates UUID version 4. The solution above uses Math.random() for brevity, however, Math.random() is not * guaranteed to be a high-quality RNG. * * @return UUID version 4. */ private uuid; } /** * Main library class. * * @author Cristiam Mercado * @since 2.0.0 * @version 11.0.0 */ declare class NgEventBus { /** * Main observable to multicast to all observers. */ private eventBus; /** * Key message separator. */ private separator; /** * Constructor for this class: Initializes event bus. */ constructor(); /** * Validates key matching. * * @param key Key to identify the message/event. * @param wildcard Wildcard received from on method. * * @return true if key matches, false otherwise. */ keyMatch(key: string, wildcard: string): boolean; /** * Publish a message/event to event bus. * * @param key Key to identify the message/event. * @param [data] Optional payload sent with the message/event. * @throws {Error} key parameter must be a string and must not be empty. */ cast(key: string): void; cast<T>(key: string, data: T): void; /** * Returns an observable you can subscribe to listen messages/events. * * @typeParam T Expected payload type. Defaults to `undefined` when omitted. * @param key Key or wildcard pattern used to identify messages/events. * * @return Observable you can subscribe to listen messages/events. */ on<T = undefined>(key: string): Observable<MetaData<T>>; } /** * Interface of the messages sent through the events bus. * * @typeParam T Message payload type. * @author Cristiam Mercado * @since 2.0.0 * @version 11.0.0 */ interface IEventBusMessage<T = unknown> { /** * Key to identify a message. */ key: string; /** * Full message metadata with its payload type preserved. */ metadata: MetaData<T>; } export { MetaData, NgEventBus }; export type { IEventBusMessage };