mana-syringe
Version:
IoC library for mana, easily to use.
165 lines (142 loc) • 5.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.register = exports.GlobalContainer = exports.Container = void 0;
var _inversify = require("inversify");
var _inversify2 = require("./inversify");
var _core = require("./core");
var _register = require("./register");
var _module = require("./module");
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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var ContainerMap = new Map();
/* eslint-disable @typescript-eslint/no-explicit-any */
var Container = /*#__PURE__*/function () {
function Container(inversifyContainer) {
_classCallCheck(this, Container);
this.loadedModules = [];
this.container = void 0;
this.inversify = true;
this.parent = void 0;
if (inversifyContainer) {
this.container = inversifyContainer;
} else {
this.container = new _inversify.Container();
}
Container.setContainer(this.container, this);
}
_createClass(Container, [{
key: "load",
value: function load(module, force) {
var _this = this;
if (force || !this.loadedModules.includes(module.id)) {
if ((0, _module.isSyringeModule)(module)) {
this.container.load(module.inversifyModule);
} else {
console.warn('Unsupported module.', module);
}
this.loadedModules.push(module.id);
return {
dispose: function dispose() {
_this.unload(module);
}
};
}
return {
dispose: function dispose() {}
};
}
}, {
key: "unload",
value: function unload(module) {
if ((0, _module.isSyringeModule)(module)) {
this.container.unload(module.inversifyModule);
this.loadedModules = this.loadedModules.filter(function (id) {
return id !== module.id;
});
}
}
}, {
key: "remove",
value: function remove(token) {
return this.container.unbind((0, _inversify2.tokenToIdentifier)(token));
}
}, {
key: "get",
value: function get(token) {
return this.container.get((0, _inversify2.tokenToIdentifier)(token));
}
}, {
key: "getNamed",
value: function getNamed(token, named) {
return this.container.getNamed((0, _inversify2.tokenToIdentifier)(token), (0, _inversify2.namedToIdentifier)(named));
}
}, {
key: "getAll",
value: function getAll(token) {
return this.container.getAll((0, _inversify2.tokenToIdentifier)(token));
}
}, {
key: "getAllNamed",
value: function getAllNamed(token, named) {
return this.container.getAllNamed((0, _inversify2.tokenToIdentifier)(token), (0, _inversify2.namedToIdentifier)(named));
}
}, {
key: "isBound",
value: function isBound(token) {
return this.container.isBound((0, _inversify2.tokenToIdentifier)(token));
}
}, {
key: "isBoundNamed",
value: function isBoundNamed(token, named) {
return this.container.isBoundNamed((0, _inversify2.tokenToIdentifier)(token), (0, _inversify2.namedToIdentifier)(named));
}
}, {
key: "createChild",
value: function createChild() {
var childContainer = this.container.createChild();
var child = new Container(childContainer);
child.parent = this;
return child;
}
}, {
key: "register",
value: function register(token) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (_core.Utils.isInjectOption(token)) {
_register.Register.resolveOption(this.container, token);
} else {
_register.Register.resolveTarget(this.container, token, options);
}
}
}], [{
key: "setContainer",
value: function setContainer(key, value) {
return ContainerMap.set(key.id, value);
}
}, {
key: "getContainer",
value: function getContainer(key) {
var exist = ContainerMap.get(key.id);
if (!exist) {
var container = new Container(key);
Container.setContainer(key, container);
return container;
}
return exist;
}
}, {
key: "config",
value: function config(option) {
_register.Register.globalConfig = option;
}
}]);
return Container;
}();
exports.Container = Container;
var GlobalContainer = new Container(_inversify2.GlobalContainer);
exports.GlobalContainer = GlobalContainer;
var register = GlobalContainer.register.bind(GlobalContainer);
exports.register = register;