@donation-alerts/events
Version:
Listen to various Donation Alerts events.
110 lines • 4.87 kB
TypeScript
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 { type DonationAlertsDonationEvent } from './events/donations/donation-alerts-donation-event';
import { type DonationAlertsGoalUpdateEvent } from './events/goals/donation-alerts-goal-update-event';
import { type DonationAlertsPollUpdateEvent } from './events/polls/donation-alerts-poll-update-event';
import { type EventsListener } from './events-listener';
import { UserEventsClient } from './user-events-client';
/**
* Configuration for {@link EventsClient}.
*
* @remarks
* Defines the settings required to initialize the `EventsClient`, including the API client
* for communication and optional logger configuration.
*/
export interface EventsClientConfig {
apiClient: ApiClient;
logger?: Partial<LoggerOptions>;
}
/**
* Donation Alerts events client that manages multiple users and allows listening to
* various events such as donations, goal updates, and poll updates.
*
* @remarks
* The `EventsClient` acts as a central manager for event subscriptions. Each user tracked
* by the client operates within its own {@link UserEventsClient} instance. This class
* simplifies handling multiple users while maintaining individual event subscription control.
*/
export declare class EventsClient extends EventEmitter {
private readonly _config;
private readonly _apiClient;
private readonly _userClients;
/**
* Fires when a user's client successfully connects to the Centrifugo server.
*/
readonly onConnect: import("@d-fischer/typed-event-emitter").EventBinder<[userId: number]>;
/**
* Fires when a user's client disconnected from the Centrifugo server.
*/
readonly onDisconnect: import("@d-fischer/typed-event-emitter").EventBinder<[userId: number, reason: string, reconnect: boolean]>;
/**
* Initializes the Donation Alerts events client.
*
* @param config Configuration for creating the `EventsClient`.
*/
constructor(config: EventsClientConfig);
/**
* Returns an instance of {@link UserEventsClient} for a specific user.
*
* @param user The ID of the user to retrieve the client instance for.
* @throws Error if the user has not been registered in the client.
*/
getUserClient(user: UserIdResolvable): UserEventsClient;
/**
* Checks if a user is registered in the client.
*
* @param user The user ID to check for registration.
* @returns `true` if the user is registered; otherwise, `false`.
*/
hasUser(user: UserIdResolvable): boolean;
/**
* Registers a user with the client and creates a new {@link UserEventsClient} for them.
*
* @param user The ID of the user to register.
*
* @returns A {@link UserEventsClient} instance for the registered user.
*
* @throws Error if the user is already registered.
*/
addUser(user: UserIdResolvable): UserEventsClient;
/**
* Unregisters a user and removes their listeners and event subscriptions.
*
* @remarks
* If the user's client is not actively subscribed to channels, the WebSocket connection
* will be closed.
*
* @param user The ID of the user to remove.
*/
removeUser(user: UserIdResolvable): Promise<void>;
/**
* Subscribes to donation events for a specific user.
*
* @param user The ID of the user whose donations are being monitored.
* @param callback A function invoked when a donation event is received.
*
* @returns An {@link EventsListener} instance for managing the subscription.
*/
onDonation(user: UserIdResolvable, callback: (event: DonationAlertsDonationEvent) => void): Promise<EventsListener>;
/**
* Subscribes to goal update events for a specific user.
*
* @param user The ID of the user whose goal updates are being monitored.
* @param callback A function invoked when a goal update event is received.
*
* @returns An {@link EventsListener} instance for managing the subscription.
*/
onGoalUpdate(user: UserIdResolvable, callback: (event: DonationAlertsGoalUpdateEvent) => void): Promise<EventsListener>;
/**
* Subscribes to poll update events for a specific user.
*
* @param user The ID of the user whose poll updates are being monitored.
* @param callback A function invoked when a poll update event is received.
*
* @returns An {@link EventsListener} instance for managing the subscription.
*/
onPollUpdate(user: UserIdResolvable, callback: (event: DonationAlertsPollUpdateEvent) => void): Promise<EventsListener>;
}
//# sourceMappingURL=events-client.d.ts.map