UNPKG

@donation-alerts/events

Version:
136 lines 6.09 kB
import { EventEmitter } from '@d-fischer/typed-event-emitter'; import { type ApiClient } from '@donation-alerts/api'; import { type UserIdResolvable } from '@donation-alerts/common'; import { type LoggerOptions } from '@stimulcross/logger'; import { DonationAlertsDonationEvent } from './events/donations/donation-alerts-donation-event'; import { DonationAlertsGoalUpdateEvent } from './events/goals/donation-alerts-goal-update-event'; import { DonationAlertsPollUpdateEvent } from './events/polls/donation-alerts-poll-update-event'; import { EventsListener } from './events-listener'; /** * Configuration for {@link UserEventsClient}. * * @remarks * Defines the settings required to initialize an instance of `UserEventsClient`. * Includes user identification, an API client for server interaction, and optional logger options. */ export interface UserEventsClientConfig { user: UserIdResolvable; apiClient: ApiClient; logger?: Partial<LoggerOptions>; } /** * Client for managing and subscribing to Donation Alerts events. * * This class provides a WebSocket-based interface for real-time interaction with the Donation Alerts platform. * It connects to its Centrifugo WebSocket server, enabling receiving notifications * about donations, goal updates, and poll updates. * * Designed for single-user use cases, it supports managing connection states (connect, disconnect, reconnect) * and subscribing to or unsubscribing from specific event types. */ export declare class UserEventsClient extends EventEmitter { private readonly _logger; private readonly _userId; private readonly _apiClient; private readonly _listeners; private readonly _centrifuge; private _client; private readonly _subscriptionListeners; /** * Fires when the client establishes a connection with Centrifugo server. */ readonly onConnect: import("@d-fischer/typed-event-emitter").EventBinder<any[]>; /** * Fires when the client disconnects from the Centrifugo server. */ readonly onDisconnect: import("@d-fischer/typed-event-emitter").EventBinder<[reason: string, reconnect: boolean]>; /** * Initializes a client for listening to various Donation Alerts events. * * @remarks This client is designed for single-user scenarios, with event subscriptions managed * through Centrifugo WebSocket connections. * * @param config Configuration required for setting up the client, including user information, * an API client for server communication, and optional logger options. * */ constructor(config: UserEventsClientConfig); /** * Unique identifier of the user associated with this client. */ get userId(): number; /** * Client ID assigned by the Centrifugo server. * * Returns `null` if the client is not connected. */ get clientId(): string | null; /** * Indicates whether the client is connected to the Centrifugo server. * * @returns `true` if the client is connected; otherwise `false`. */ get isConnected(): boolean; /** * Establishes a connection to the Centrifugo server. * * @param restoreExistingListeners Specifies whether existing listeners should be restored after connection. * Defaults to `true`. */ connect(restoreExistingListeners?: boolean): Promise<void>; /** * Closes the connection to the Centrifugo server. * * @param removeListeners Indicates whether all active listeners should be removed on disconnect. * If set to `false`, the listeners will be restored on the next connection. * Default to `false`. */ disconnect(removeListeners?: boolean): Promise<void>; /** * Re-establishes the connection to the Centrifugo server. * * @param removeListeners Indicates whether all listeners should be removed during reconnection. * If `false`, all listeners will be restored automatically after reconnection. * Defaults to `false`. */ reconnect(removeListeners?: boolean): Promise<void>; /** * Subscribes to donation events from Donation Alerts. * * @param callback A function invoked whenever a donation event is received. * The callback receives an instance of {@link DonationAlertsDonationEvent}. * @returns An {@link EventsListener} instance that manages the subscription. */ onDonation(callback: (event: DonationAlertsDonationEvent) => void): Promise<EventsListener>; /** * Subscribes to goal update events from Donation Alerts. * * @param callback A function invoked whenever a goal update event is received. * The callback receives an instance of {@link DonationAlertsGoalUpdateEvent}. * @returns An {@link EventsListener} instance that manages the subscription. */ onGoalUpdate(callback: (event: DonationAlertsGoalUpdateEvent) => void): Promise<EventsListener>; /** * Subscribes to poll update events from Donation Alerts. * * @param callback A function invoked whenever a poll update event is received. * The callback receives an instance of {@link DonationAlertsPollUpdateEvent}. * @returns An {@link EventsListener} instance that manages the subscription. */ onPollUpdate(callback: (event: DonationAlertsPollUpdateEvent) => void): Promise<EventsListener>; /** * Unsubscribes and removes a listener for a specific channel. * * @remarks * If this is the last listener, the WebSocket connection is also closed. * * @param listener The {@link EventsListener} instance to be removed. */ removeEventsListener(listener: EventsListener): Promise<void>; private _connect; private _disconnect; private _createListener; private _subscribe; private _unsubscribe; } //# sourceMappingURL=user-events-client.d.ts.map