@tvkitchen/countertop
Version:
The entry point for developers who want to set up a TV Kitchen.
124 lines (97 loc) • 4.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _kafkajs = require("kafkajs");
var _constants = require("../constants");
var _loggers = require("../tools/loggers");
var _CountertopStation = _interopRequireDefault(require("./CountertopStation"));
var _CountertopTopology = _interopRequireDefault(require("./CountertopTopology"));
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; }
/**
* The Countertop sets up and monitors stations for registered appliances to ensure the proper
* flow and processing of data.
*
* It is also responsible for emitting information and serving as the entry and exit point of
* the countertop architecture.
*/
class Countertop {
/**
* @param {Logger} options.logger A logger with methods for all TV Kitchen logLevels.
* @param {Object} options.kafkaSettings Kafka settings as defined in the KafkaJS library.
*/
constructor({
logger = _loggers.silentLogger,
kafkaSettings = {}
} = {}) {
_defineProperty(this, "logger", null);
_defineProperty(this, "kafka", null);
_defineProperty(this, "stations", []);
_defineProperty(this, "state", '');
_defineProperty(this, "addAppliance", (Appliance, applianceSettings = {}) => {
this.logger.trace('CountertopCoordinator: addAppliance()');
if (this.getState() !== _constants.countertopStates.STOPPED) {
throw new Error('The Countertop must be stopped in order to add an Appliance.');
}
const station = new _CountertopStation.default(Appliance, applianceSettings, {
logger: this.logger,
kafka: this.kafka
});
this.stations.push(station);
this.updateTopology();
return station;
});
_defineProperty(this, "start", async () => {
this.logger.trace('CountertopCoordinator: start()');
this.setState(_constants.countertopStates.STARTING);
const promises = this.stations.map(station => station.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('CountertopCoordinator: stop()');
this.setState(_constants.countertopStates.STOPPING);
this.stations.map(station => station.stop());
const promises = this.stations.map(async station => station.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, "updateTopology", () => {
this.logger.trace('CountertopCoordinator: updateTopology()');
if (this.state !== _constants.countertopStates.STOPPED) {
throw new Error('The Countertop must be stopped in order to update topology.');
}
const topology = new _CountertopTopology.default(this.stations);
this.stations.map(station => station.invokeTopology(topology));
return topology;
});
_defineProperty(this, "getState", () => this.state);
_defineProperty(this, "setState", state => {
this.state = state;
});
_defineProperty(this, "on", (eventType, listener) => {
this.stations.forEach(station => station.on(eventType, listener));
return this;
});
this.logger = logger;
this.setState(_constants.countertopStates.STOPPED);
this.kafka = new _kafkajs.Kafka({
brokers: ['127.0.0.1:9092'],
...kafkaSettings
});
}
/**
* Register a new, configured appliance into the countertop.
* Appliances that ingest Payload streams may be cloned as new streams are created.
*
* @param {Class} Appliance The class of the IAppliance being registered.
* @param {Object} applianceSettings The settings to be passed to the appliance.
* @return {CountertopStation} The station created for this appliance.
*/
}
var _default = Countertop;
exports.default = _default;