@tvkitchen/countertop
Version:
The entry point for developers who want to set up a TV Kitchen.
117 lines (88 loc) • 4.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _uuid = require("uuid");
var _constants = require("../constants");
var _loggers = require("../tools/loggers");
var _CountertopWorker = _interopRequireDefault(require("./CountertopWorker"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class CountertopStation {
/**
* Create a CountertopStation
*
* @param {Class} Appliance The IAppliance class that this station will manage.
* @param {Object} applianceSettings Settings for the appliance.
* @param {Logger} options.logger A logger with methods for all TV Kitchen logLevels.
*/
constructor(Appliance, applianceSettings = {}, {
logger = _loggers.consoleLogger,
kafka
} = {}) {
_defineProperty(this, "logger", null);
_defineProperty(this, "kafka", null);
_defineProperty(this, "id", null);
_defineProperty(this, "Appliance", null);
_defineProperty(this, "applianceSettings", null);
_defineProperty(this, "workers", []);
_defineProperty(this, "state", '');
_defineProperty(this, "invokeTopology", topology => {
this.logger.trace(`CountertopStation<${this.id}>: invokeTopology()`);
if (this.getState() !== _constants.countertopStates.STOPPED) {
throw new Error('CountertopStations must be stopped before invoking a topology.');
}
const stationStreams = topology.streams.filter(stream => stream.getMouth() === this);
this.workers = stationStreams.map(stream => new _CountertopWorker.default(this.Appliance, this.applianceSettings, {
stream,
logger: this.logger,
kafka: this.kafka
}));
return this.workers;
});
_defineProperty(this, "start", async () => {
this.logger.trace(`CountertopStation<${this.id}>: start()`);
this.state = _constants.countertopStates.STARTING;
const promises = this.workers.map(async worker => worker.start());
const started = (await Promise.all(promises)).every(result => result);
this.setState(started ? _constants.countertopStates.STARTED : _constants.countertopStates.ERRORED);
return this.getState() === _constants.countertopStates.STARTED;
});
_defineProperty(this, "stop", async () => {
this.logger.trace(`CountertopStation<${this.id}>: stop()`);
this.setState(_constants.countertopStates.STOPPING);
const promises = this.workers.map(async worker => worker.stop());
const stopped = (await Promise.all(promises)).every(result => result);
this.setState(stopped ? _constants.countertopStates.STOPPED : _constants.countertopStates.ERRORED);
return this.getState() === _constants.countertopStates.STOPPED;
});
_defineProperty(this, "getInputTypes", () => this.Appliance.getInputTypes(this.applianceSettings));
_defineProperty(this, "getOutputTypes", () => this.Appliance.getOutputTypes(this.applianceSettings));
_defineProperty(this, "getState", () => this.state);
_defineProperty(this, "setState", state => {
this.state = state;
});
_defineProperty(this, "on", (eventType, listener) => {
this.workers.forEach(worker => worker.on(eventType, listener));
return this;
});
if (Appliance === undefined) {
throw new Error('CountertopStation requires an Appliance.');
}
this.Appliance = Appliance;
this.applianceSettings = applianceSettings;
this.logger = logger;
this.kafka = kafka;
this.id = `${Appliance.name}::${(0, _uuid.v4)()}`;
this.setState(_constants.countertopStates.STOPPED);
}
/**
* Replace the station's workers in order to fit a specified topology.
*
* @param {CountertopToplogy} topology The topology to use.
* @return {CountertopWorker[]} The new set of workers.
*/
}
var _default = CountertopStation;
exports.default = _default;