@thatcompany/ts-tool
Version:
基于TypeScript编写的工具库
154 lines • 6.12 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventPublisher = exports.ApplicationContext = void 0;
require("reflect-metadata");
const file_1 = require("../../file");
const ConfigureManager_1 = require("./impl/ConfigureManager");
const TaskManager_1 = require("./impl/TaskManager");
const annotation_1 = require("../annotation");
const slot_1 = require("../slot");
const FrameWorkManager_1 = require("./impl/FrameWorkManager");
/**
* 自动装配管理器
*/
class ApplicationManager {
// 工厂模式, 用于动态添加装配器
factory = [];
// 装配器实例链路
managerChain = [];
/**
* 装配完成后的服务集合
* key: symbol 装配的服务类型
* value: Set<any> 装配的服务实例集合
*
* 例如: 获取所有调度任务注解的服务实例集合
* @example
* ```typescript
* import { ApplicationContext } from '@that-tool';
* // 获取所有调度任务注解的服务实例集合
* const cronTasks = ApplicationContext.services.get(METADATA.TASK.CRON);
* // 进一步获取名字为 CronTaskRegister 的服务实例
* const cronTaskRegister = cronTasks.get('CronTaskRegister');
* // 进一步获取每个调度任务实例ICronTaskService的线程池状态
* const register = ApplicationContext.getCronTasks();
* for (const [ id, task ] of register.get('CronTaskRegister')!.services){
* Logger.info("正在挂起的线程:", task._queuePending)
* Logger.info("正在执行的线程:", task._queueThread)
* }
* ```
*/
services = new Map();
eventPublisher = new slot_1.ApplicationEventPublisher();
add(china) {
this.factory.push(china);
return this;
}
async build(baseDir = '') {
this.factory = [ConfigureManager_1.ConfigureManager, FrameWorkManager_1.FrameWorkManager, TaskManager_1.TaskManager];
// this.managerChain =
// this.factory.length === 0
// ? new ManagerFactory(this.services).buildManagerChain()
// : this.factory.map((manager) => new manager());
this.managerChain = this.factory.map((manager) => new manager(this.services));
await this.scan(baseDir);
await this.execute();
// 初始化 eventPublisher
exports.EventPublisher = new slot_1.ApplicationEventPublisher(this.getEventListeners());
this.eventPublisher = exports.EventPublisher;
}
async loadModule(path) {
// 动态导入 TypeScript、JavaScript 文件
const modules = await Promise.resolve(`${path}`).then(s => __importStar(require(s)));
for (const entity of Object.values(modules)) {
// 过滤非类 和 不存在装饰器的类
if (typeof entity === 'function' && Reflect.getMetadataKeys(entity).length > 0) {
// 装配器识别收集
for (const manager of this.managerChain) {
manager.collection(entity);
}
}
}
}
/**
* 扫描并自动装配
* @param scanDir 基于目录扫描 默认 src
*/
async scan(scanDir) {
// 启动扫描
await file_1.Scanner.scan(this.loadModule.bind(this), scanDir);
}
async execute() {
// 装饰器执行链路
for (const manager of this.managerChain) {
const entities = await manager.execute();
if (entities.size > 0) {
for (const [key, entity] of entities) {
entity.size > 0 && this.services.has(key)
? entity.forEach((v, k) => this.services.get(key).set(k, v))
: this.services.set(key, entity);
}
}
}
}
getEventListeners() {
if (this.services.has(annotation_1.METADATA.EVENT.LISTENER)) {
return this.services.get(annotation_1.METADATA.EVENT.LISTENER);
}
return new Map();
}
/**
* 获取所有调度任务的服务实例集合
*/
getCronTasks() {
if (this.services.has(annotation_1.METADATA.TASK.CRON)) {
return this.services.get(annotation_1.METADATA.TASK.CRON);
}
return new Map();
}
}
/**
* 自动装配管理器上下文
*
* 用于在不同模块之间共享装配器实例
*
* 构建时add添加管理链路,build构建装配链路并执行装配
*
* 默认添加了 ConfigureManager、FrameWorkManager、TaskManager 三个管理器, 可直接build()
* @example
* ```typescript
* import { ApplicationContext } from '@that-tool';
*
* (async () => {
* // await ApplicationContext.add(ConfigureManager).build();
* await ApplicationContext.build();
* // 装配完成后可通过 ApplicationContext.services 获取装配的服务
* Logger.info(ApplicationContext.services);
* })();
* ```
*/
exports.ApplicationContext = new ApplicationManager();
exports.EventPublisher = new slot_1.ApplicationEventPublisher();
//# sourceMappingURL=ApplicationManager.js.map