UNPKG

@donation-alerts/events

Version:
147 lines (146 loc) 5.54 kB
import { __decorate } from "tslib"; import { EventEmitter } from '@d-fischer/typed-event-emitter'; import { extractUserId, ReadDocumentation } from '@donation-alerts/common'; import { nonenumerable } from '@stimulcross/shared-utils'; import { UserEventsClient } from "./user-events-client.mjs"; /** * 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. */ let EventsClient = class EventsClient extends EventEmitter { /** * Initializes the Donation Alerts events client. * * @param config Configuration for creating the `EventsClient`. */ constructor(config) { super(); this._userClients = new Map(); /** * Fires when a user's client successfully connects to the Centrifugo server. */ this.onConnect = this.registerEvent(); /** * Fires when a user's client disconnected from the Centrifugo server. */ this.onDisconnect = this.registerEvent(); this._config = config; this._apiClient = config.apiClient; } /** * 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) { const userId = extractUserId(user); if (!this._userClients.has(userId)) { throw new Error(`User "${userId}" is not registered. Use "addUser" method to register the user before listening to events.`); } return this._userClients.get(userId); } /** * 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) { return this._userClients.has(extractUserId(user)); } /** * 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) { const userId = extractUserId(user); if (this._userClients.has(userId)) { throw new Error(`User "${userId}" is already registered`); } const userEventsClient = new UserEventsClient({ user: userId, apiClient: this._apiClient, logger: this._config.logger, }); userEventsClient.onConnect(() => this.emit(this.onConnect, userId)); userEventsClient.onDisconnect((reason, reconnect) => this.emit(this.onDisconnect, userId, reason, reconnect)); this._userClients.set(userId, userEventsClient); return 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. */ async removeUser(user) { const userId = extractUserId(user); if (!this._userClients.has(userId)) { return; } const client = this._userClients.get(userId); await client.disconnect(true); client.removeListener(); this._userClients.delete(userId); } /** * 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. */ async onDonation(user, callback) { return await this.getUserClient(user).onDonation(callback); } /** * 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. */ async onGoalUpdate(user, callback) { return await this.getUserClient(user).onGoalUpdate(callback); } /** * 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. */ async onPollUpdate(user, callback) { return await this.getUserClient(user).onPollUpdate(callback); } }; __decorate([ nonenumerable ], EventsClient.prototype, "_config", void 0); __decorate([ nonenumerable ], EventsClient.prototype, "_apiClient", void 0); __decorate([ nonenumerable ], EventsClient.prototype, "_userClients", void 0); EventsClient = __decorate([ ReadDocumentation('events') ], EventsClient); export { EventsClient };