@glandjs/events
Version:
A fast, zero‑dependency event broker and message bus for building scalable, event‑driven applications.
45 lines (44 loc) • 3.43 kB
TypeScript
import type { Broker, BrokerId, BrokerOptions, Channel, ChannelEvents, ConnectionOptions, EmitOptions, EventOptions, EventPayload, EventRecord, EventReturn, Events, EventType, Listener } from './common';
export declare class EventBroker<TEvents extends EventRecord> implements Broker<TEvents> {
options: BrokerOptions;
private readonly _id;
private readonly _emitter;
private readonly _connections;
private readonly _channels;
private readonly _eventTraces;
constructor(options: BrokerOptions);
private trackEvent;
private hasProcessedEvent;
private normalizeOptions;
get id(): string;
on<K extends Events<TEvents>>(event: K, listener: Listener<EventPayload<TEvents, K>, void>, options?: EventOptions): this;
on<K extends Events<TEvents>>(event: K, listener: null | Listener<EventPayload<TEvents, K>, void>, options: EventOptions & {
watch: true;
}): Promise<EventPayload<TEvents, K>>;
off<K extends Events<TEvents>>(event: K, listener?: Listener<EventPayload<TEvents, K>, void>): this;
emit<K extends Events<TEvents>>(event: K, payload: EventPayload<TEvents, K>, options?: EmitOptions): this;
once<K extends Events<TEvents>>(event: K, listener: Listener<EventPayload<TEvents, K>, void>): this;
once<K extends Events<TEvents>>(event: K, listener: Listener<EventPayload<TEvents, K>, void> | null, options: EventOptions & {
watch: true;
}): Promise<EventPayload<TEvents, K>>;
watch<K extends Events<TEvents>>(event: K, timeoutMs?: number): Promise<EventPayload<TEvents, K>>;
getListener<K extends Events<TEvents>>(event: K): Listener<EventPayload<TEvents, K>, EventReturn<TEvents, K>>[];
call<K extends Events<TEvents>>(event: K, data: EventPayload<TEvents, K>): EventReturn<TEvents, K>;
call<K extends Events<TEvents>>(event: K, data: EventPayload<TEvents, K>, strategy: 'all'): EventReturn<TEvents, K>[];
channel<TPrefix extends EventType, TChannelEvents extends ChannelEvents<TPrefix, TEvents> = ChannelEvents<TPrefix, TEvents>>(name: TPrefix): Channel<TChannelEvents>;
broadcast<K extends Events<TEvents>>(event: K, payload: EventPayload<TEvents, K>, options?: EventOptions): this;
send<K extends Events<TEvents>>(event: K, target: EventBroker<TEvents>, options?: EventOptions): this;
connectTo<TOtherEvents extends EventRecord>(broker: EventBroker<TOtherEvents>, options?: ConnectionOptions): this;
disconnect(brokerId: BrokerId): boolean;
isConnected(brokerId: BrokerId): boolean;
emitTo<K extends Events<TEvents>>(brokerId: BrokerId, event: K, payload: TEvents[K], options?: EventOptions): boolean;
createConnections<TOtherEvents extends EventRecord>(brokers: Array<EventBroker<TOtherEvents>>, options?: ConnectionOptions): this;
getConnections(): BrokerId[];
getConnection(brokerId: BrokerId): EventBroker<any> | undefined;
disconnectAll(): this;
callTo<K extends Events<TEvents>>(brokerId: BrokerId, event: K, data: EventPayload<TEvents, K>): EventReturn<TEvents, K>;
callTo<K extends Events<TEvents>>(brokerId: BrokerId, event: K, data: EventPayload<TEvents, K>, strategy: 'all'): EventReturn<TEvents, K>[];
broadcastTo<K extends Events<TEvents>>(brokerIds: BrokerId[], event: K, payload: EventPayload<TEvents, K>, options?: EventOptions): this;
findBroker(brokerId: BrokerId, maxDepth?: number): EventBroker<any> | undefined;
shutdown(): void;
}