UNPKG

@malagu/core

Version:
71 lines 2.81 kB
"use strict"; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyComponentDecorator = exports.parseComponentOption = exports.Component = 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"); const scope_1 = require("../container/scope"); exports.COMPONENT_TAG = 'Component'; exports.Component = function (...idOrOption) { return (t) => { const option = parseComponentOption(t, idOrOption); applyComponentDecorator(option, t); }; }; const defaultComponentOption = Object.assign({ scope: scope_1.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 = Array.from(new Set([target, ...parsed.id])); } else if (parsed.id && parsed.id !== target) { ids = [target, parsed.id]; } 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) { (0, inversify_1.decorate)((0, 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