UNPKG

pandora

Version:

A powerful and lightweight application manager for Node.js applications powered by TypeScript.

128 lines 3.78 kB
'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ServiceLogger_1 = require("./ServiceLogger"); const const_1 = require("../const"); const ServiceContextAccessor_1 = require("./ServiceContextAccessor"); /** * Class SimpleServiceCore */ class ServiceCore { constructor(options, ImplClass) { this.options = options; this.ImplClass = ImplClass; } ; get logger() { if (!this._logger) { this._logger = new ServiceLogger_1.default(this); } return this._logger; } get context() { return this.options.context; } get dependencies() { if (this._dependencies) { return this._dependencies; } const ret = {}; for (const id in this.options.depInstances) { if (this.options.depInstances.hasOwnProperty(id)) { ret[id] = this.options.depInstances[id].getService(); } } this._dependencies = ret; return ret; } get representation() { return this.options.representation; } get serviceName() { return this.representation.serviceName; } get config() { return this.representation.config || {}; } /** * Get Class of implementation * @return {any} */ getImplClass() { return this.ImplClass; } /** * Instantiate service * @return {Service} */ instantiate() { if (!this.impl) { const serviceContextAccessor = new ServiceContextAccessor_1.ServiceContextAccessor(this); const Klass = this.getImplClass(); this.impl = new (Klass)(serviceContextAccessor); } return this.impl; } /** * Start service * @return {Promise<void>} */ start() { return __awaiter(this, void 0, void 0, function* () { const impl = this.instantiate(); impl.start && (yield impl.start()); }); } /** * Stop service * @return {Promise<void>} */ stop() { return __awaiter(this, void 0, void 0, function* () { this.impl.stop && (yield this.impl.stop()); }); } /** * Publish this service to IPC HUB * @return {Promise<void>} */ publish() { return __awaiter(this, void 0, void 0, function* () { const objectNameInHub = const_1.SERVICE_PREFIX_IN_HUB + this.getServiceId(); yield this.context.hub.publish(this.impl, { name: objectNameInHub }); }); } /** * Get service's implementation object * @return {Service} */ getService() { return this.impl; } /** * Get service's identify, equal service's name now * @return {string} */ getServiceId() { return this.serviceName; } /** * Get a dependency service's implementation object by service's ID (name) * @param name * @return {any} */ getDependency(name) { return this.dependencies[name]; } } exports.ServiceCore = ServiceCore; //# sourceMappingURL=ServiceCore.js.map