UNPKG

framework4js

Version:

Framework-for-js is a better Javascript's framework

171 lines (162 loc) 4.72 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _Exception = _interopRequireDefault(require("../spanners/Exception")); /** * Class Container 容器类 */ var Container = /*#__PURE__*/function () { function Container() { (0, _classCallCheck2["default"])(this, Container); // 闭包[回调/函数] (0, _defineProperty2["default"])(this, "_binds", {}); // 实例 (0, _defineProperty2["default"])(this, "_instances", {}); // 别名 (0, _defineProperty2["default"])(this, "_aliases", {}); } /** * Method bind :: 绑定注册到容器 * @param {*} abstract * @param {*} concrete function(){} function(){return new Class()} * @param {*} shared 是否为共享 */ (0, _createClass2["default"])(Container, [{ key: "bind", value: function bind(_abstract, concrete) { var shared = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; if (!_abstract) { throw new _Exception["default"]("abstract not null", "FunctionParameterError"); } if (typeof concrete === "function") { this._binds[_abstract] = concrete; } else { this._instances[_abstract] = concrete; } } /** * Method make :: 实例化 new Class() * @param {*} abstract * @param {...any} args * @returns */ }, { key: "make", value: function make(_abstract2) { if (this._instances.hasOwnProperty(_abstract2)) { if (typeof this._instances[_abstract2] === "function") { var _this$_instances; for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return (_this$_instances = this._instances)[_abstract2].apply(_this$_instances, args); } return this._instances[_abstract2]; } else { throw new _Exception["default"]("instances is undefined", "InstancesError"); } } }, { key: "resolving", value: function resolving(_abstract3, concrete) {} /** * Method isAlias 确定字符串是否为别名 * @param {*} name * @returns */ }, { key: "isAlias", value: function isAlias(name) { return this._aliases[name] ? ture : false; } }, { key: "extend", value: function extend(_abstract4, concrete) {} /** * Method singleton 单例 * @param {*} abstract * @param {*} concrete */ }, { key: "singleton", value: function singleton(_abstract5, concrete) { // 绑定共享实例 this.bind(_abstract5, concrete, true); } /** * Method get 获取容器中的对象实例 * @param {*} name * @returns */ }, { key: "get", value: function get(name) { var binds = this._binds; var hasProperty = binds.hasOwnProperty(name); return hasProperty ? this._binds[name]() : this._binds; } }, { key: "warp", value: function warp() {} /** * Static method getInstance * @returns */ }, { key: "proxyInstance", value: /** * Method proxyInstance * @returns */ function proxyInstance() { return new Proxy(this, { get: function get(target, propKey) { return target[propKey]; } }); } /** * Method instance 注册共享实例 * @param {*} abstract * @param {*} instance * @returns */ }, { key: "instance", value: function instance(_abstract6, _instance) { // 未处理 this._instances[_abstract6] = _instance; // 未处理 return _instance; } }], [{ key: "getInstance", value: function getInstance() { var instance = Container._instance; if (!instance || !(instance instanceof Container)) { instance = Container._instance = new Container(); } return instance.proxyInstance(); } /** * Static method setInstance * @param {*} instance */ }, { key: "setInstance", value: function setInstance(instance) { Container._instance = instance; } }]); return Container; }(); exports["default"] = Container; // 静态实例 (0, _defineProperty2["default"])(Container, "_instance", null);