UNPKG

@malagu/core

Version:
76 lines 2.95 kB
"use strict"; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyComponentDecorator = exports.parseComponentOption = exports.Component = exports.Scope = exports.COMPONENT_TAG = void 0; const inversify_1 = require("inversify"); const constants_1 = require("../constants"); const config_util_1 = require("../config/config-util"); const utils_1 = require("../utils"); exports.COMPONENT_TAG = 'Component'; var Scope; (function (Scope) { Scope[Scope["Request"] = 0] = "Request"; Scope[Scope["Singleton"] = 1] = "Singleton"; Scope[Scope["Transient"] = 2] = "Transient"; })(Scope = exports.Scope || (exports.Scope = {})); exports.Component = function (...idOrOption) { return (t) => { const option = parseComponentOption(t, idOrOption); applyComponentDecorator(option, t); }; }; const defaultComponentOption = Object.assign({ scope: Scope.Singleton, rebind: false, proxy: false }, (_b = (_a = config_util_1.ConfigUtil.getRaw().malagu) === null || _a === void 0 ? void 0 : _a.annotation) === null || _b === void 0 ? void 0 : _b.Component); function parseComponentOption(target, idOrOption) { if (Array.isArray(idOrOption)) { if (idOrOption.length === 1) { idOrOption = idOrOption[0]; } else if (idOrOption.length === 0) { idOrOption = undefined; } } const option = utils_1.AnnotationUtil.getValueOrOption(idOrOption); const parsed = Object.assign(Object.assign({}, defaultComponentOption), option); let ids; if (Array.isArray(parsed.id)) { ids = parsed.id; } else if (parsed.id && parsed.id !== target) { ids = [parsed.id, target]; } else { ids = [target]; } parsed.id = ids; parsed.sysTags = [...new Set([exports.COMPONENT_TAG, ...parsed.sysTags || []])]; return parsed; } exports.parseComponentOption = parseComponentOption; function applyComponentDecorator(option, target) { const isAlreadyDecorated = Reflect.hasOwnMetadata(inversify_1.METADATA_KEY.PARAM_TYPES, target); if (!isAlreadyDecorated) { inversify_1.decorate(inversify_1.injectable(), target); } const metadata = { target, ids: Array.isArray(option.id) ? option.id : [option.id || target], sysTags: option.sysTags, rebind: option.rebind, proxy: option.proxy, scope: option.scope, name: option.name, tag: option.tag, default: option.default, when: option.when, onActivation: option.onActivation }; let metadatas = Reflect.getMetadata(constants_1.METADATA_KEY.component, Reflect); if (!metadatas) { metadatas = []; Reflect.defineMetadata(constants_1.METADATA_KEY.component, metadatas, Reflect); } metadatas.push(metadata); return metadata; } exports.applyComponentDecorator = applyComponentDecorator; //# sourceMappingURL=component.js.map