UNPKG

pandora

Version:
151 lines 4.63 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 assert = require("assert"); const ServiceLogger_1 = require("./ServiceLogger"); const const_1 = require("../const"); const debug = require('debug')('pandora:SimpleServiceCore'); /** * Class SimpleServiceCore */ class ServiceCore { get context() { return this.options.context; } get deps() { const ret = {}; for (const id in this.options.depInstances) { if (this.options.depInstances.hasOwnProperty(id)) { ret[id] = this.options.depInstances[id].getService(); } } return ret; } get representation() { return this.options.representation; } get serviceName() { return this.representation.serviceName; } get config() { return this.representation.config || {}; } constructor(options, ImplClass) { this.options = options; this.ImplClass = ImplClass; this.logger = new ServiceLogger_1.default(this); } ; /** * Get Class of implementation * @return {any} */ getImplClass() { return this.ImplClass; } /** * Instantiate service * @return {Service} */ instantiate() { if (!this.impl) { this.impl = new (this.getImplClass())(this); this.impl.core = this; if (this.impl.handleSubscribe) { this.subscribeHandler = this.impl.handleSubscribe.bind(this.impl); } if (this.impl.handleUnsubscribe) { this.unsubscribeHandler = this.impl.handleUnsubscribe.bind(this.impl); } } 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.ipcHub.publish(this.impl, { name: objectNameInHub }); }); } /** * Subscribe a event * @param reg * @param listener * @return {Promise<any>} */ subscribe(reg, listener) { return __awaiter(this, void 0, void 0, function* () { assert(this.subscribeHandler, 'could not found subscribeHandler when subscribe event'); debug('subscribe() %j', reg); return yield this.subscribeHandler(reg, listener); }); } /** * Cancel subscribe a event * @param reg * @param listener * @return {Promise<any>} */ unsubscribe(reg, listener) { return __awaiter(this, void 0, void 0, function* () { assert(this.unsubscribeHandler, 'could not found unsubscribeHandler when unsubscribe event'); debug('unsubscribe() %j', reg); return yield this.unsubscribeHandler(reg, listener); }); } /** * 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.deps[name]; } } exports.ServiceCore = ServiceCore; //# sourceMappingURL=ServiceCore.js.map