| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-24 17:55:22 |
| 4 | * @LastEditTime: 2025-03-19 14:31:34 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import 'reflect-metadata'; |
| 9 | import { ipcMain } from 'electron'; |
| 10 | import { container } from './container'; |
| 11 | import { INJECT_METADATA_KEY } from './metadata'; |
| 12 | import { EtEvent } from '../../global/event'; |
| 13 | import { scheduleJob, scheduleJobMap } from '../../global/schedule'; |
| 14 | |
| 15 | // Module 装饰器 |
| 16 | export function Module(metadata: { |
| 17 | imports?: any[]; |
| 18 | controllers?: any[]; |
| 19 | providers?: any[]; |
| 20 | }) { |
| 21 | return function (target: any) { |
| 22 | // 只注册 providers 和 controllers,不立即初始化 |
| 23 | metadata.providers?.forEach((provider) => { |
| 24 | container.registerProvider(provider); |
| 25 | }); |
| 26 | |
| 27 | metadata.controllers?.forEach((controller) => { |
| 28 | const controllerName = controller.name; |
| 29 | if (!container.hasController(controllerName)) { |
| 30 | const instance = new controller(); |
| 31 | container.setController(controllerName, instance); |
| 32 | } |
| 33 | }); |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | // Inject 装饰器 |
| 38 | export function Inject(serviceType: any) { |
| 39 | return function (target: any, propertyKey: string) { |
| 40 | const injections = |
| 41 | Reflect.getMetadata(INJECT_METADATA_KEY, target.constructor) || []; |
| 42 | injections.push({ propertyKey, serviceType }); |
| 43 | Reflect.defineMetadata(INJECT_METADATA_KEY, injections, target.constructor); |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | // Controller 装饰器 |
| 48 | export function Controller() { |
| 49 | return function (target: any) { |
| 50 | Reflect.defineMetadata('isController', true, target); |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | // Injectable 装饰器 |
| 55 | export function Injectable() { |
| 56 | return function (target: any) { |
| 57 | Reflect.defineMetadata('isInjectable', true, target); |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | // IPC 方法装饰器 |
| 62 | export function Icp(channel: string) { |
| 63 | return function ( |
| 64 | target: any, |
| 65 | propertyKey: string, |
| 66 | descriptor: PropertyDescriptor, |
| 67 | ) { |
| 68 | const originalMethod = descriptor.value; |
| 69 | |
| 70 | ipcMain.handle(channel, async (event, ...args) => { |
| 71 | const controller = container.getController(target.constructor.name); |
| 72 | if (!controller) { |
| 73 | throw new Error(`Controller ${target.constructor.name} not found`); |
| 74 | } |
| 75 | return await originalMethod.bind(controller)(event, ...args); |
| 76 | }); |
| 77 | |
| 78 | return descriptor; |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | // Event事件监听装饰器 |
| 83 | export function Et(eventName: string) { |
| 84 | return function ( |
| 85 | target: any, |
| 86 | propertyKey: string, |
| 87 | descriptor: PropertyDescriptor, |
| 88 | ) { |
| 89 | const originalMethod = descriptor.value; |
| 90 | |
| 91 | EtEvent.on(eventName, async (...args) => { |
| 92 | const controller = container.getController(target.constructor.name); |
| 93 | if (!controller) { |
| 94 | throw new Error(`Controller ${target.constructor.name} not found`); |
| 95 | } |
| 96 | return await originalMethod.bind(controller)(...args); |
| 97 | }); |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * 定时任务装饰器 |
| 103 | * @param cron |
| 104 | * @param key |
| 105 | * @returns |
| 106 | */ |
| 107 | export function Scheduled(cron: string, key?: string) { |
| 108 | return function ( |
| 109 | target: any, |
| 110 | propertyKey: string, |
| 111 | descriptor: PropertyDescriptor, |
| 112 | ) { |
| 113 | const originalMethod = descriptor.value; |
| 114 | |
| 115 | // 使用 node-schedule 创建定时任务 |
| 116 | const job = scheduleJob.scheduleJob(cron, async () => { |
| 117 | const controller = container.getController(target.constructor.name); |
| 118 | if (!controller) { |
| 119 | throw new Error(`Controller ${target.constructor.name} not found`); |
| 120 | } |
| 121 | await originalMethod.bind(controller)(); |
| 122 | }); |
| 123 | |
| 124 | if (!!key) scheduleJobMap.set(key, job); |
| 125 | |
| 126 | return descriptor; |
| 127 | }; |
| 128 | } |
| 129 |