UNPKG

srvoa

Version:

Infrastructure for service oriented architecture.

159 lines (133 loc) 4.48 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"; /** * The srvoa service manager. * It is responsible for holding the service registry and giving shared access to the service instances. */ 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; }; })(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ServiceManager = (function () { _createClass(ServiceManager, null, [{ key: "STRICT_MODE_CONFIG_KEY", /** * The map of key => service. * * @property services {Object} */ /** * The strict mode flag of the service manager. * If set to true, the service manager will throw errors. * * @property {boolean} strictMode */ /** * The config key for setting the strict mode of the service manager from application/configuration context. * * @returns {string} */ get: function get() { return 'service-manager.strictMode'; } /** * Initialize services map. * * @constructor */ }]); function ServiceManager() { _classCallCheck(this, ServiceManager); this.services = {}; this.strictMode = false; } /** * Retrieves the strict mode flag of the service manager. * * @returns {boolean} */ _createClass(ServiceManager, [{ key: "getStrictMode", value: function getStrictMode() { return this.strictMode; } /** * Sets the strict mode flag of the service manager. * * @param {boolean} strictMode * @returns {ServiceManager} */ }, { key: "setStrictMode", value: function setStrictMode(strictMode) { this.strictMode = strictMode; return this; } /** * Lookup and return a service by its key. * * @param {String} key * @param {Boolean} strict (optional) default false * @throws {Error} if strict mode is enabled and there is no such service. * @return {Object}|{undefined} */ }, { key: "get", value: function get(key, strict) { if ((this.strictMode === true && strict !== false || strict === true) && !this.has(key)) { throw new Error("Cannot return unknown service for key: " + key + " in strict mode."); } return this.services[key]; } /** * Store a service for the given key. * * @param {String} key * @param {Object} service * @param {Boolean} strict (optional) default false * @throws {Error} if strict mode is enabled and a service for the given key already exists. * @return {ServiceManager} */ }, { key: "set", value: function set(key, service, strict) { if ((this.strictMode === true && strict !== false || strict === true) && this.has(key)) { throw new Error("Cannot re-register service for key: " + key + " in strict mode."); } this.services[key] = service; return this; } /** * Delete the service for the given key. * * @param {String} key * @param {Boolean} strict (optional) default false * @return {ServiceManager} */ }, { key: "remove", value: function remove(key, strict) { if ((this.strictMode === true && strict !== false || strict === true) && !this.has(key)) { throw new Error("Cannot remove unknown service for key: " + key + " in strict mode."); } delete this.services[key]; return this; } /** * Check for the existence of a service by its key. * * @param {String} key * @return {Boolean} */ }, { key: "has", value: function has(key) { return key in this.services; } }]); return ServiceManager; })(); module.exports = ServiceManager;