UNPKG

ps2census

Version:

Client to connect to the PS2 Event Stream websocket.

109 lines 4.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscriptionManager = void 0; const stream_response_exception_1 = require("./exceptions/stream-response.exception"); const next_tick_1 = __importDefault(require("../utils/next-tick")); class SubscriptionManager { constructor(client, stream, commandHandler, subscription = {}) { this.client = client; this.stream = stream; this.commandHandler = commandHandler; this.characters = new Set(subscription.characters); this.worlds = new Set(subscription.worlds); this.eventNames = new Set(subscription.eventNames); this.logicalAndCharactersWithWorlds = subscription.logicalAndCharactersWithWorlds ?? false; this.registerClientEvents(); } registerClientEvents() { this.stream.on('ready', async () => { (0, next_tick_1.default)(() => this.client.emit('debug', `Subscribing to events`)); try { await this.commandHandler.subscribe(this.subscription); } catch (err) { if (!(err instanceof stream_response_exception_1.StreamResponseException)) throw err; } }); } subscribe({ characters, worlds, eventNames, logicalAndCharactersWithWorlds, }) { characters?.forEach(character => this.characters.add(character)); worlds?.forEach(world => this.worlds.add(world)); eventNames?.forEach(eventName => this.eventNames.add(eventName)); this.logicalAndCharactersWithWorlds = logicalAndCharactersWithWorlds ?? this.logicalAndCharactersWithWorlds; return this.commandHandler.subscribe({ characters, worlds, eventNames, logicalAndCharactersWithWorlds, }); } unsubscribe({ characters, worlds, eventNames, logicalAndCharactersWithWorlds, }) { characters?.forEach(character => this.characters.delete(character)); worlds?.forEach(world => this.worlds.delete(world)); eventNames?.forEach(eventName => this.eventNames.delete(eventName)); this.logicalAndCharactersWithWorlds = logicalAndCharactersWithWorlds ?? this.logicalAndCharactersWithWorlds; return this.commandHandler.clearSubscribe({ characters, worlds, eventNames, logicalAndCharactersWithWorlds, }); } async unsubscribeAll() { this.characters.clear(); this.worlds.clear(); this.eventNames.clear(); this.logicalAndCharactersWithWorlds = false; await this.commandHandler.clearSubscribeAll(); } async verifySubscription() { const subscription = await this.commandHandler.subscribe({}); if (this.logicalAndCharactersWithWorlds != subscription.logicalAndCharactersWithWorlds) return false; if (this.characters.has('all')) { if (!('characters' in subscription)) return false; } else { if (!('characterCount' in subscription) || subscription.characterCount != this.characters.size) return false; } for (const world of this.worlds) { if (!subscription.worlds.some(id => world == id)) return false; } for (const event of this.eventNames) { if (!subscription.eventNames.some(name => event == name)) return false; } return true; } async resubscribe(reset = false) { if (this.stream.isReady) { if (reset) await this.commandHandler.clearSubscribeAll(); await this.commandHandler.subscribe(this.subscription); return true; } return false; } get subscription() { return { characters: Array.from(this.characters), worlds: Array.from(this.worlds), eventNames: Array.from(this.eventNames), logicalAndCharactersWithWorlds: this.logicalAndCharactersWithWorlds, }; } } exports.SubscriptionManager = SubscriptionManager; //# sourceMappingURL=subscription.manager.js.map