UNPKG

mana-syringe

Version:

IoC library for mana, easily to use.

149 lines (134 loc) 4.77 kB
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; } import { Container as InversifyContainer } from 'inversify'; import { GlobalContainer as InversifyGlobalContainer, namedToIdentifier, tokenToIdentifier } from './inversify'; import { Utils } from './core'; import { Register } from './register'; import { isSyringeModule } from './module'; var ContainerMap = new Map(); /* eslint-disable @typescript-eslint/no-explicit-any */ export 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 InversifyContainer(); } 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 (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 (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(tokenToIdentifier(token)); } }, { key: "get", value: function get(token) { return this.container.get(tokenToIdentifier(token)); } }, { key: "getNamed", value: function getNamed(token, named) { return this.container.getNamed(tokenToIdentifier(token), namedToIdentifier(named)); } }, { key: "getAll", value: function getAll(token) { return this.container.getAll(tokenToIdentifier(token)); } }, { key: "getAllNamed", value: function getAllNamed(token, named) { return this.container.getAllNamed(tokenToIdentifier(token), namedToIdentifier(named)); } }, { key: "isBound", value: function isBound(token) { return this.container.isBound(tokenToIdentifier(token)); } }, { key: "isBoundNamed", value: function isBoundNamed(token, named) { return this.container.isBoundNamed(tokenToIdentifier(token), 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 (Utils.isInjectOption(token)) { Register.resolveOption(this.container, token); } else { 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.globalConfig = option; } }]); return Container; }(); export var GlobalContainer = new Container(InversifyGlobalContainer); export var register = GlobalContainer.register.bind(GlobalContainer);