UNPKG

@thatcompany/ts-tool

Version:

基于TypeScript编写的工具库

79 lines 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FrameWorkManager = void 0; const IAutoManager_1 = require("../IAutoManager"); const annotation_1 = require("../../annotation"); const logger_1 = require("../../../logger"); const logger = logger_1.LoggerContext.getSubLogger('EventRegister', true); /** * 框架注解管理器 */ class FrameWorkManager extends IAutoManager_1.IAutoManager { clazz = annotation_1.IRegister; keys = [annotation_1.METADATA.FRAMEWORK.COMPONENT, annotation_1.METADATA.FRAMEWORK.SERVICE]; services = new Map(); // 收集的事件监听器 eventListeners = new Map(); async execute() { if (this.entities.size === 0) { return this.services; } for (const key of this.keys) { logger.silly('------------------------------'); logger.silly('🌟 扫描组件实体:全局组件'); const entities = this.entities.get(key); if (entities?.size === 0) continue; switch (key) { case annotation_1.METADATA.FRAMEWORK.COMPONENT: for (const entity of entities) { const instance = new entity(); this.services.set(annotation_1.METADATA.FRAMEWORK.COMPONENT, new Map([[entity.name, instance]])); const proto = entity.prototype; // 遍历 prototype 上的所有属性 for (const propertyKey of Object.getOwnPropertyNames(proto)) { const descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey); if (descriptor && typeof descriptor.value === 'function') { const isEventListener = Reflect.getMetadata(annotation_1.METADATA.EVENT.LISTENER, proto, propertyKey); const identifier = Reflect.getMetadata(annotation_1.METADATA.EVENT.IDENTIFIER, proto, propertyKey); const sort = Reflect.getMetadata(annotation_1.METADATA.EVENT.SORT, proto, propertyKey); if (isEventListener) { const paramTypes = Reflect.getMetadata('design:paramtypes', proto, propertyKey); if (paramTypes && paramTypes.length > 0) { // 使用参数类型的名称拼接作为事件类型标识符 const eventType = paramTypes.map((param) => param.name).join('::'); if (!this.eventListeners.has(eventType)) { this.eventListeners.set(eventType, []); } this.eventListeners .get(eventType) ?.push({ instance, method: descriptor.value, sort }); if (!this.eventListeners.has(identifier)) { this.eventListeners.set(identifier, []); } this.eventListeners .get(identifier) ?.push({ instance, method: descriptor.value, sort }); logger.info(`发现监听实体:: ${entity.name} -> (${identifier}::${propertyKey})`); } } } } } break; default: break; } logger.silly('🪶 扫描组件实体:全局组件实体扫描完成'); logger.silly('------------------------------\n'); } // 对监听器进行排序 for (const [eventType, listeners] of this.eventListeners.entries()) { this.eventListeners.set(eventType, listeners.sort((a, b) => a.sort - b.sort)); } this.services.set(annotation_1.METADATA.EVENT.LISTENER, this.eventListeners); return this.services; } } exports.FrameWorkManager = FrameWorkManager; //# sourceMappingURL=FrameWorkManager.js.map