UNPKG

@sofie-automation/server-core-integration

Version:
82 lines 4.91 kB
"use strict"; var _SubscriptionsHelper_ddp, _SubscriptionsHelper_deviceToken, _SubscriptionsHelper_autoSubscriptions, _SubscriptionsHelper_otherSubscriptions; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscriptionsHelper = void 0; const tslib_1 = require("tslib"); const protectedString_1 = require("@sofie-automation/shared-lib/dist/lib/protectedString"); class SubscriptionsHelper { constructor(emitError, ddp, deviceToken) { this.emitError = emitError; _SubscriptionsHelper_ddp.set(this, void 0); _SubscriptionsHelper_deviceToken.set(this, void 0); _SubscriptionsHelper_autoSubscriptions.set(this, new Map()); _SubscriptionsHelper_otherSubscriptions.set(this, new Set()); tslib_1.__classPrivateFieldSet(this, _SubscriptionsHelper_ddp, ddp, "f"); tslib_1.__classPrivateFieldSet(this, _SubscriptionsHelper_deviceToken, deviceToken, "f"); } async subscribeOnce(publicationName, ...params) { const subscriptionId = await this.subscribeWithId(undefined, String(publicationName), ...params); tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_otherSubscriptions, "f").add(subscriptionId); return subscriptionId; } async subscribeWithId(existingSubscriptionId, publicationName, ...params) { const orgError = new Error(); return new Promise((resolve, reject) => { if (!tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_ddp, "f").ddpClient) { reject('subscribe: DDP client is not initialized'); return; } try { const subscriptionId = tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_ddp, "f").ddpClient.subscribe(publicationName, // name of Meteor Publish function to subscribe to params.concat([tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_deviceToken, "f")]), // 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); } }); } async autoSubscribe(publicationName, ...params) { const subscriptionId = await this.subscribeWithId(undefined, String(publicationName), ...params); tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_autoSubscriptions, "f").set(subscriptionId, { publicationName: String(publicationName), params: params, }); return subscriptionId; } unsubscribe(subscriptionId) { tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_ddp, "f").ddpClient?.unsubscribe((0, protectedString_1.unprotectString)(subscriptionId)); tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_autoSubscriptions, "f").delete(subscriptionId); tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_otherSubscriptions, "f").delete(subscriptionId); } renewAutoSubscriptions() { // Forget the other presumed dead subscriptions tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_otherSubscriptions, "f").clear(); for (const [subId, sub] of tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_autoSubscriptions, "f").entries()) { this.subscribeWithId(subId, sub.publicationName, ...sub.params).catch((e) => this.emitError('renewSubscr ' + sub.publicationName + ': ' + e)); } } unsubscribeAll() { for (const subId of tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_otherSubscriptions, "f")) { this.unsubscribe(subId); } tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_otherSubscriptions, "f").clear(); for (const subId of tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_autoSubscriptions, "f").keys()) { this.unsubscribe(subId); } tslib_1.__classPrivateFieldGet(this, _SubscriptionsHelper_autoSubscriptions, "f").clear(); } } exports.SubscriptionsHelper = SubscriptionsHelper; _SubscriptionsHelper_ddp = new WeakMap(), _SubscriptionsHelper_deviceToken = new WeakMap(), _SubscriptionsHelper_autoSubscriptions = new WeakMap(), _SubscriptionsHelper_otherSubscriptions = new WeakMap(); //# sourceMappingURL=subscriptions.js.map