@donation-alerts/events
Version:
Listen to various Donation Alerts events.
150 lines (149 loc) • 5.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventsClient = void 0;
const tslib_1 = require("tslib");
const typed_event_emitter_1 = require("@d-fischer/typed-event-emitter");
const common_1 = require("@donation-alerts/common");
const shared_utils_1 = require("@stimulcross/shared-utils");
const user_events_client_1 = require("./user-events-client");
/**
* 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 typed_event_emitter_1.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 = (0, common_1.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((0, common_1.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 = (0, common_1.extractUserId)(user);
if (this._userClients.has(userId)) {
throw new Error(`User "${userId}" is already registered`);
}
const userEventsClient = new user_events_client_1.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 = (0, common_1.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);
}
};
exports.EventsClient = EventsClient;
tslib_1.__decorate([
shared_utils_1.nonenumerable
], EventsClient.prototype, "_config", void 0);
tslib_1.__decorate([
shared_utils_1.nonenumerable
], EventsClient.prototype, "_apiClient", void 0);
tslib_1.__decorate([
shared_utils_1.nonenumerable
], EventsClient.prototype, "_userClients", void 0);
exports.EventsClient = EventsClient = tslib_1.__decorate([
(0, common_1.ReadDocumentation)('events')
], EventsClient);