UNPKG

brusc

Version:

IOC Container for Effective JS development

87 lines (72 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InstanceProvider = void 0; var _utils = require("../common/utils"); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var InstanceProvider = /*#__PURE__*/function () { function InstanceProvider(_ref) { var key = _ref.key, provider = _ref.provider, _ref$isSingleton = _ref.isSingleton, isSingleton = _ref$isSingleton === void 0 ? true : _ref$isSingleton, _ref$isEager = _ref.isEager, isEager = _ref$isEager === void 0 ? false : _ref$isEager; _classCallCheck(this, InstanceProvider); this._key = key; this._provider = provider; this._isSingleton = isSingleton; this._isEager = isEager; this._guardKeyHasValue(); this._guardProviderIsFunction(); } _createClass(InstanceProvider, [{ key: "provide", value: function provide() { var instance = this._provider(); this._guardInstanceIsGenerated(instance); return instance; } }, { key: "key", get: function get() { return this._key; } }, { key: "isSingleton", get: function get() { return this._isSingleton; } }, { key: "isEager", get: function get() { return this._isEager; } }, { key: "_guardKeyHasValue", value: function _guardKeyHasValue() { if (!this._key) { throw new Error('Key has no value'); } } }, { key: "_guardProviderIsFunction", value: function _guardProviderIsFunction() { if (typeof this._provider !== 'function') { throw new Error("Instance provider for [".concat((0, _utils.stringKey)(this._key), "] must be a function")); } } }, { key: "_guardInstanceIsGenerated", value: function _guardInstanceIsGenerated(instance) { if (!instance) { throw new Error("Instance provider for [".concat((0, _utils.stringKey)(this._key), "] did not generate an instance")); } } }]); return InstanceProvider; }(); exports.InstanceProvider = InstanceProvider;