UNPKG

deep-kernel

Version:
190 lines (146 loc) 5.59 kB
/** * Created by AlexanderC on 6/10/15. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContainerAware = undefined; 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(object, property, receiver) { 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 { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _Injectable = require('./Microservice/Injectable'); var _InvalidDeepIdentifierException = require('./Exception/InvalidDeepIdentifierException'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } 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; } /** * @todo - rename it to AbstractService (ContainerAware doesn't make sense anymore) * * Container aware instance */ let ContainerAware = exports.ContainerAware = function (_MicroserviceInjectab) { _inherits(ContainerAware, _MicroserviceInjectab); function ContainerAware() { _classCallCheck(this, ContainerAware); var _this = _possibleConstructorReturn(this, (ContainerAware.__proto__ || Object.getPrototypeOf(ContainerAware)).call(this)); _this._kernel = null; _this._localBackend = false; return _this; } /** * @returns {Boolean} */ _createClass(ContainerAware, [{ key: 'boot', /** * Booting a certain service * * @param {Kernel} kernel * @param {Function} callback */ value: function boot(kernel, callback) { // @todo: override in child service callback(); } /** * Cleanup a certain service */ }, { key: 'cleanup', value: function cleanup() {} // @todo: override in child service /** * @param {Object|String} microservice * @returns {Injectable} */ }, { key: 'bind', value: function bind(microservice) { // @todo: find more smart way of doing this... if (typeof microservice === 'string') { microservice = this.kernel.microservice(microservice); } return _get(ContainerAware.prototype.__proto__ || Object.getPrototypeOf(ContainerAware.prototype), 'bind', this).call(this, microservice); } /** * @param {String} identifier (e.g. @microservice_identifier:resource[:action]) * @returns {String} * * @private */ }, { key: '_resolveIdentifier', value: function _resolveIdentifier(identifier) { let regExp = /^@\s*([^:]+)\s*:\s*([^\s]+)\s*$/; if (typeof identifier === 'string' && regExp.test(identifier)) { let parts = identifier.match(regExp); this.bind(parts[1]); // microservice identifier return parts[2]; // resource identifier } else { throw new _InvalidDeepIdentifierException.InvalidDeepIdentifierException(identifier); } } /** * @returns {DI|null} */ }, { key: 'get', /** * @param {Array} args * @returns {*} */ value: function get() { var _container; return (_container = this.container).get.apply(_container, arguments); } }, { key: 'localBackend', get: function get() { return this._localBackend; } /** * @param {Boolean} state */ , set: function set(state) { this._localBackend = state; } /** * @returns {String} */ }, { key: 'name', get: function get() { return this.constructor.name.toLowerCase(); } /** * @returns {Object} */ }, { key: 'service', get: function get() { return this; } }, { key: 'container', get: function get() { return this._kernel ? this._kernel.container : null; } /** * @param {Kernel} kernel */ }, { key: 'kernel', set: function set(kernel) { this._kernel = kernel; } /** * @returns {Kernel} */ , get: function get() { return this._kernel; } }]); return ContainerAware; }(_Injectable.Injectable);