UNPKG

srvoa

Version:

Infrastructure for service oriented architecture.

156 lines (128 loc) 4.98 kB
/** * srvoa - soa infrastructure for node js * * @copyright Copyright (c) 2015, Alrik Zachert * @license https://gitlab.com/kermit-js/kermit/blob/support/srvoa/LICENSE BSD-2-Clause */ "use strict"; var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var EventEmitter = require('events').EventEmitter, Config = require('./Config'); /** * Abstract class that defines the srvoa service interface. */ var Service = (function (_EventEmitter) { _inherits(Service, _EventEmitter); /** * The local reference to the service manager instance. * * @property serviceManager {ServiceManager} */ /** * The config passed to configure as config service.. * * @property serviceConfig {Config} */ /** * Create a new service instance. * * @constructor * @param {ServiceManager} [serviceManager] - The service manager instance. */ function Service(serviceManager) { _classCallCheck(this, Service); _get(Object.getPrototypeOf(Service.prototype), 'constructor', this).call(this); this.serviceManager = serviceManager || null; this.serviceConfig = null; } /** * Returns the service manager instance. * * @return {ServiceManager} */ _createClass(Service, [{ key: 'getServiceManager', value: function getServiceManager() { return this.serviceManager; } /** * Sets the service manager instance. * * @param {ServiceManager} serviceManager * @return {Service} */ }, { key: 'setServiceManager', value: function setServiceManager(serviceManager) { this.serviceManager = serviceManager; return this; } /** * Configures the service. * * @param {Object} [config] * @return {Service} */ }, { key: 'configure', value: function configure(config) { return this._applyConfig(config); } /** * Bootstrap the service logic. * * @return {Service} */ }, { key: 'bootstrap', value: function bootstrap() { return this; } /** * Launch the service logic. * * @return {Service} */ }, { key: 'launch', value: function launch() { return this; } /** * Return the default service config. * * @returns {Object|false} */ }, { key: 'getDefaultServiceConfig', value: function getDefaultServiceConfig() { return false; } /** * Take a config hash and turn it into a config instance that is stored as `serviceConfig` property. * Before that the default service config is applied. * * @param config * @returns {Service} * @private */ }, { key: '_applyConfig', value: function _applyConfig(config) { var defaultConfig = this.getDefaultServiceConfig(); this.serviceConfig = new Config(); if (defaultConfig !== false) { this.serviceConfig.setConfig(defaultConfig); } if (config) { this.serviceConfig.mergeConfig(config); } return this; } }]); return Service; })(EventEmitter); module.exports = Service;