@bbc/sofie-server-core-integration
Version:
Library for connecting to Core
178 lines • 6.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreConnectionChild = void 0;
const events_1 = require("events");
const methodsAPI_1 = require("@bbc/sofie-shared-lib/dist/peripheralDevice/methodsAPI");
const methods_js_1 = require("./methods.js");
const ping_js_1 = require("./ping.js");
const subscriptions_js_1 = require("./subscriptions.js");
class CoreConnectionChild extends events_1.EventEmitter {
_parent;
_parentOptions;
_coreOptions;
_methodQueue;
_subscriptions;
_pinger;
_destroyed = false;
_peripheralDeviceApi;
_peripheralDeviceApiLowPriority;
constructor(coreOptions) {
super();
this._coreOptions = coreOptions;
this._peripheralDeviceApi = (0, methods_js_1.makeMethods)(this, methodsAPI_1.PeripheralDeviceAPIMethods);
this._peripheralDeviceApiLowPriority = (0, methods_js_1.makeMethodsLowPrio)(this, methodsAPI_1.PeripheralDeviceAPIMethods);
this._pinger = new ping_js_1.CorePinger((err) => this._emitError(err), async () => this.coreMethods.ping());
}
doTriggerPing = (connected) => {
this._pinger.setConnectedAndTriggerPing(connected);
};
async init(parent, parentOptions) {
this._destroyed = false;
parent.on('connected', () => this._subscriptions.renewAutoSubscriptions());
parent.on('connectionChanged', this.doTriggerPing);
this._parent = parent;
this._parentOptions = parentOptions;
this._methodQueue = new methods_js_1.ConnectionMethodsQueue(this._parent.ddp, {
deviceId: this._coreOptions.deviceId,
deviceToken: parentOptions.deviceToken,
});
this._subscriptions = new subscriptions_js_1.SubscriptionsHelper(this._emitError.bind(this), this._parent.ddp, parentOptions.deviceToken);
this._pinger.setConnectedAndTriggerPing(parent.connected);
return this._sendInit();
}
async destroy() {
this._destroyed = true;
this._subscriptions.unsubscribeAll();
if (this._parent) {
this._parent.off('connected', () => this._subscriptions.renewAutoSubscriptions());
this._parent.off('connectionChanged', this.doTriggerPing);
this._parent.removeChild(this);
this._parent = undefined;
}
this.removeAllListeners();
this._pinger.destroy();
}
get parent() {
if (!this._parent)
throw new Error('Connection has been destroyed');
return this._parent;
}
get ddp() {
if (!this._parent)
throw new Error('Connection has been destroyed');
return this._parent.ddp;
}
get connected() {
return this._parent?.connected ?? false;
}
get deviceId() {
return this._coreOptions.deviceId;
}
get coreMethods() {
return this._peripheralDeviceApi;
}
get coreMethodsLowPriority() {
return this._peripheralDeviceApiLowPriority;
}
async setStatus(status) {
return this.coreMethods.setStatus(status);
}
/**
* This should not be used directly, use the `coreMethods` wrapper instead.
* Call a meteor method
* @param methodName The name of the method to call
* @param attrs Parameters to the method
* @returns Resopnse, if any
*/
async callMethodRaw(methodName, attrs) {
if (this._destroyed) {
throw new Error('callMethod: CoreConnection has been destroyed');
}
return this._methodQueue.callMethodRaw(methodName, attrs);
}
async callMethodLowPrioRaw(methodName, attrs) {
return this._methodQueue.callMethodLowPrioRaw(methodName, attrs);
}
async unInitialize() {
return this.coreMethods.unInitialize();
}
async getPeripheralDevice() {
return this.coreMethods.getPeripheralDevice();
}
getCollection(collectionName) {
if (!this._parent)
throw new Error('Connection has been destroyed');
return this._parent.getCollection(collectionName);
}
// /**
// * Subscribe to a DDP publication
// * Upon reconnecting to Sofie, this publication will be terminated
// */
// async subscribeOnce(publicationName: string, ...params: Array<any>): Promise<SubscriptionId> {
// if (!this._subscriptions) throw new Error('Connection is not ready to handle subscriptions')
// return this._subscriptions.subscribeOnce(publicationName, ...params)
// }
/**
* Subscribe to a DDP publication
* Upon reconnecting to Sofie, this publication will be restarted
*/
async autoSubscribe(publicationName, ...params) {
if (!this._subscriptions)
throw new Error('Connection is not ready to handle subscriptions');
return this._subscriptions.autoSubscribe(publicationName, ...params);
}
/**
* Unsubscribe from subscription to a DDP publication
*/
unsubscribe(subscriptionId) {
if (!this._subscriptions)
throw new Error('Connection is not ready to handle subscriptions');
this._subscriptions.unsubscribe(subscriptionId);
}
/**
* Unsubscribe from all subscriptions to DDP publications
*/
unsubscribeAll() {
if (!this._subscriptions)
throw new Error('Connection is not ready to handle subscriptions');
this._subscriptions.unsubscribeAll();
}
observe(collectionName) {
if (!this._parent)
throw new Error('Connection has been destroyed');
return this._parent.observe(collectionName);
}
// getCurrentTime(): number {
// return this._timeSync?.currentTime() || 0
// }
// hasSyncedTime(): boolean {
// return this._timeSync?.isGood() || false
// }
// syncTimeQuality(): number | null {
// return this._timeSync?.quality || null
// }
_emitError(e) {
if (!this._destroyed) {
this.emit('error', e);
}
else {
console.log('destroyed error', e);
}
}
async _sendInit() {
if (!this.ddp || !this.ddp.connectionId || !this._parent)
throw Error('Not connected to Core');
const options = {
category: this._parentOptions.deviceCategory,
type: this._parentOptions.deviceType,
subType: this._coreOptions.deviceSubType,
name: this._coreOptions.deviceName,
connectionId: this.ddp.connectionId,
parentDeviceId: this._parent.deviceId,
documentationUrl: this._parentOptions.documentationUrl,
};
return this.coreMethods.initialize(options);
}
}
exports.CoreConnectionChild = CoreConnectionChild;
//# sourceMappingURL=CoreConnectionChild.js.map