UNPKG

ngx-signal-hub

Version:

[![npm version](https://badge.fury.io/js/ngx-signal-hub.svg)](https://badge.fury.io/js/ngx-signal-hub) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

70 lines (69 loc) 3.17 kB
import { Injector, Signal, DestroyRef } from '@angular/core'; import * as i0 from "@angular/core"; /** Represents an event in the signal hub with a key, data, and timestamp. */ export interface HubEvent<T = unknown> { key: string; data: T; timestamp: number; } /** Represents the options for subscribing to a single signal hub event. */ export interface HubEventOptions<T> { /** The key to match events against (e.g., 'user:login'). */ key: string; /** The callback function to execute when an event matches the key. */ callback: (event: HubEvent<T>) => void | Promise<void>; /** Optional DestroyRef for auto-cleaning subscriptions. */ destroyRef?: DestroyRef; /** If true, replays the latest event for the key on subscription. */ replayLatest?: boolean; /** Optional error handler for callback errors. */ onError?: (error: unknown, event: HubEvent<T>) => void; /** Optional condition(s) to unsubscribe when true (signal) or when event is received (key). */ until?: (string | Signal<boolean>) | (string | Signal<boolean>)[]; } /** Represents the options for subscribing to multiple signal hub events with combineLatest behavior. */ export interface HubCombineLatestOptions<T> { /** Array of keys to match events against (e.g., ['user:login', 'user:logout']). */ keys: string[]; /** The callback function to execute with the latest events for all keys. */ callback: (events: HubEvent<T>[]) => void | Promise<void>; /** Optional DestroyRef for auto-cleaning subscriptions. */ destroyRef?: DestroyRef; /** If true, replays the latest events for the keys on subscription. */ replayLatest?: boolean; /** Optional error handler for callback errors. */ onError?: (error: unknown, events: HubEvent<T>[]) => void; /** Optional sorting for combined events (e.g., 'timestamp' or 'key'). */ sortBy?: 'timestamp' | 'key'; } /** Represents a subscription to the signal hub. */ export interface HubSubscription { unsubscribe: () => void; } export declare class SignalHubService { private readonly injector; private readonly eventRegistry; private readonly subscribers; constructor(injector: Injector); publish<T = unknown>(key: string, data?: T): void; on<T>(options: HubEventOptions<T>): HubSubscription; subscribe: <T>(options: HubEventOptions<T>) => HubSubscription; once<T>({ key, callback, replayLatest, onError, until }: HubEventOptions<T>): HubSubscription; onCombineLatest<T>(options: HubCombineLatestOptions<T>): HubSubscription; toSignal<T>(key: string): Signal<HubEvent<T> | null>; toSignalMultiple<T>(keys: string[], options?: { sortBy?: 'timestamp' | 'key'; }): Signal<HubEvent<T>[]>; clearEvent(key: string): void; reset(options?: { clearSubscribers?: boolean; }): void; private internalSubscribe; private notifySubscribers; private findLatestEventForKey; private handleError; private matchQuery; private buildWildcardRegex; static ɵfac: i0.ɵɵFactoryDeclaration<SignalHubService, never>; static ɵprov: i0.ɵɵInjectableDeclaration<SignalHubService>; }