UNPKG

@bbc/sofie-server-core-integration

Version:
80 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscriptionsHelper = void 0; const protectedString_1 = require("@bbc/sofie-shared-lib/dist/lib/protectedString"); class SubscriptionsHelper { emitError; #ddp; #deviceToken; #autoSubscriptions = new Map(); #otherSubscriptions = new Set(); constructor(emitError, ddp, deviceToken) { this.emitError = emitError; this.#ddp = ddp; this.#deviceToken = deviceToken; } async subscribeOnce(publicationName, ...params) { const subscriptionId = await this.subscribeWithId(undefined, String(publicationName), ...params); this.#otherSubscriptions.add(subscriptionId); return subscriptionId; } async subscribeWithId(existingSubscriptionId, publicationName, ...params) { const orgError = new Error(); return new Promise((resolve, reject) => { if (!this.#ddp.ddpClient) { reject(new Error('subscribe: DDP client is not initialized')); return; } try { const subscriptionId = this.#ddp.ddpClient.subscribe(publicationName, // name of Meteor Publish function to subscribe to params.concat([this.#deviceToken]), // parameters used by the Publish function (error) => { if (error) { const newError = new Error(`Error from publication: ${error.errorType} [${error.error}] ${error.reason} ${error.message}`); newError.stack = `${newError.stack}\nOriginal stack:\n${orgError.stack}`; reject(newError); } else { // callback when the subscription is complete resolve((0, protectedString_1.protectString)(subscriptionId)); } }, (0, protectedString_1.unprotectString)(existingSubscriptionId)); } catch (e) { reject(e instanceof Error ? e : new Error('subscribe failed: ' + e)); } }); } async autoSubscribe(publicationName, ...params) { const subscriptionId = await this.subscribeWithId(undefined, String(publicationName), ...params); this.#autoSubscriptions.set(subscriptionId, { publicationName: String(publicationName), params: params, }); return subscriptionId; } unsubscribe(subscriptionId) { this.#ddp.ddpClient?.unsubscribe((0, protectedString_1.unprotectString)(subscriptionId)); this.#autoSubscriptions.delete(subscriptionId); this.#otherSubscriptions.delete(subscriptionId); } renewAutoSubscriptions() { // Forget the other presumed dead subscriptions this.#otherSubscriptions.clear(); for (const [subId, sub] of this.#autoSubscriptions.entries()) { this.subscribeWithId(subId, sub.publicationName, ...sub.params).catch((e) => this.emitError('renewSubscr ' + sub.publicationName + ': ' + e)); } } unsubscribeAll() { for (const subId of this.#otherSubscriptions) { this.unsubscribe(subId); } this.#otherSubscriptions.clear(); for (const subId of this.#autoSubscriptions.keys()) { this.unsubscribe(subId); } this.#autoSubscriptions.clear(); } } exports.SubscriptionsHelper = SubscriptionsHelper; //# sourceMappingURL=subscriptions.js.map