brusc
Version:
IOC Container for Effective JS development
127 lines (101 loc) • 3.97 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Brusc = void 0;
var _Container = require("./container/Container");
var _InstanceProvider = require("./container/InstanceProvider");
var _InstanceAdapter = require("./container/InstanceAdapter");
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; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var Brusc = /*#__PURE__*/function () {
function Brusc(inject) {
var _this = this;
_classCallCheck(this, Brusc);
_defineProperty(this, "_errorContainerNotCreated", function (key) {
throw new Error("Cannot provide [".concat((0, _utils.stringKey)(key), "], call the Brusc#create method first"));
});
this._guardInjectIsFunction(inject);
this._inject = inject;
this._defaults = inject.defaults || {};
this._instanceProviders = new Map();
this._adapters = [];
inject.provide = function (key) {
return _this._errorContainerNotCreated(key);
};
}
_createClass(Brusc, [{
key: "singleton",
value: function singleton(key, provider, isEager) {
this._guardKeyNotDefinedYet(key);
this._instanceProviders.set(key, new _InstanceProvider.InstanceProvider({
key: key,
provider: this._defaults[key] || provider,
isEager: isEager
}));
return this;
}
}, {
key: "prototype",
value: function prototype(key, provider) {
this._guardKeyNotDefinedYet(key);
this._instanceProviders.set(key, new _InstanceProvider.InstanceProvider({
key: key,
provider: this._defaults[key] || provider,
isSingleton: false
}));
return this;
}
}, {
key: "adapter",
value: function adapter() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
name = _ref.name,
match = _ref.match,
adapt = _ref.adapt;
this._adapters.push(new _InstanceAdapter.InstanceAdapter({
name: name,
match: match,
adapt: adapt
}));
return this;
}
}, {
key: "create",
value: function create() {
var container = new _Container.Container({
instanceProviders: this._instanceProviders,
adapters: this._adapters
});
this._inject.defaults = undefined;
this._inject.provide = function (key) {
return container.provide(key);
};
container.loadEagerInstances();
}
}, {
key: "_guardInjectIsFunction",
value: function _guardInjectIsFunction(inject) {
if (typeof inject !== 'function') {
throw new Error('To enable injection, inject to define must be a function');
}
}
}, {
key: "_guardKeyNotDefinedYet",
value: function _guardKeyNotDefinedYet(key) {
if (this._instanceProviders.has(key)) {
throw new Error("[".concat(key, "] already defined, check singleton/prototype declaration key duplications"));
}
}
}], [{
key: "define",
value: function define(inject) {
return new Brusc(inject);
}
}]);
return Brusc;
}();
exports.Brusc = Brusc;
;