@dangao/bun-server 1.8.3 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/core/application.d.ts +5 -0
- package/dist/core/application.d.ts.map +1 -1
- package/dist/events/event-module.d.ts +4 -0
- package/dist/events/event-module.d.ts.map +1 -1
- package/dist/events/types.d.ts +16 -0
- package/dist/events/types.d.ts.map +1 -1
- package/dist/index.js +4944 -4856
- package/package.json +1 -1
- package/src/core/application.ts +29 -0
- package/src/events/event-module.ts +108 -0
- package/src/events/types.ts +19 -0
package/package.json
CHANGED
package/src/core/application.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { ConfigService } from '../config/service';
|
|
|
18
18
|
import { ConfigModule } from '../config/config-module';
|
|
19
19
|
import { CacheModule, CACHE_POST_PROCESSOR_TOKEN } from '../cache';
|
|
20
20
|
import { LoggerManager } from '@dangao/logsmith';
|
|
21
|
+
import { EventModule } from '../events/event-module';
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* 应用配置选项
|
|
@@ -102,6 +103,9 @@ export class Application {
|
|
|
102
103
|
// 初始化配置中心集成(在所有模块注册完成后)
|
|
103
104
|
await this.initializeConfigCenter();
|
|
104
105
|
|
|
106
|
+
// 自动初始化事件监听器(如果 EventModule 已注册且启用了 autoScan)
|
|
107
|
+
this.initializeEventListeners();
|
|
108
|
+
|
|
105
109
|
const finalPort = port ?? this.options.port ?? 3000;
|
|
106
110
|
const finalHostname = hostname ?? this.options.hostname;
|
|
107
111
|
|
|
@@ -178,6 +182,31 @@ export class Application {
|
|
|
178
182
|
}
|
|
179
183
|
}
|
|
180
184
|
|
|
185
|
+
/**
|
|
186
|
+
* 初始化事件监听器
|
|
187
|
+
* 在所有模块注册完成后调用,自动扫描并注册使用 @OnEvent 装饰器的类
|
|
188
|
+
*/
|
|
189
|
+
private initializeEventListeners(): void {
|
|
190
|
+
const container = this.getContainer();
|
|
191
|
+
|
|
192
|
+
// 检查 EventModule 是否已注册
|
|
193
|
+
const registry = ModuleRegistry.getInstance();
|
|
194
|
+
const eventModuleRef = registry.getModuleRef(EventModule);
|
|
195
|
+
|
|
196
|
+
if (!eventModuleRef) {
|
|
197
|
+
// EventModule 未注册,跳过
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 调用自动初始化
|
|
202
|
+
const initialized = EventModule.autoInitialize(container);
|
|
203
|
+
|
|
204
|
+
if (initialized) {
|
|
205
|
+
const logger = LoggerManager.getLogger();
|
|
206
|
+
logger.debug('[Application] Event listeners initialized automatically');
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
181
210
|
/**
|
|
182
211
|
* 停止应用(立即停止,不等待请求完成)
|
|
183
212
|
*/
|
|
@@ -102,6 +102,11 @@ export class EventModule {
|
|
|
102
102
|
*/
|
|
103
103
|
private static listenerClasses: Function[] = [];
|
|
104
104
|
|
|
105
|
+
/**
|
|
106
|
+
* 模块选项
|
|
107
|
+
*/
|
|
108
|
+
private static options: EventModuleOptions & { autoScan?: boolean } = {};
|
|
109
|
+
|
|
105
110
|
/**
|
|
106
111
|
* 创建事件模块
|
|
107
112
|
* @param options - 模块配置
|
|
@@ -121,6 +126,12 @@ export class EventModule {
|
|
|
121
126
|
* ```
|
|
122
127
|
*/
|
|
123
128
|
public static forRoot(options: EventModuleOptions = {}): typeof EventModule {
|
|
129
|
+
// 保存选项(默认启用自动扫描)
|
|
130
|
+
EventModule.options = {
|
|
131
|
+
...options,
|
|
132
|
+
autoScan: options.autoScan ?? true,
|
|
133
|
+
};
|
|
134
|
+
|
|
124
135
|
const providers: ModuleProvider[] = [];
|
|
125
136
|
|
|
126
137
|
// 创建事件发射器服务
|
|
@@ -269,4 +280,101 @@ export class EventModule {
|
|
|
269
280
|
|
|
270
281
|
return undefined;
|
|
271
282
|
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 自动初始化事件监听器
|
|
286
|
+
* 由框架在应用启动时自动调用,扫描所有模块中使用 @OnEvent 装饰器的类并注册
|
|
287
|
+
*
|
|
288
|
+
* @param rootContainer - 根容器
|
|
289
|
+
* @returns 是否成功初始化
|
|
290
|
+
*
|
|
291
|
+
* @internal 此方法由框架内部调用,用户通常不需要手动调用
|
|
292
|
+
*/
|
|
293
|
+
public static autoInitialize(rootContainer: Container): boolean {
|
|
294
|
+
// 检查是否启用自动扫描
|
|
295
|
+
if (EventModule.options.autoScan === false) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// 获取 EventEmitter
|
|
300
|
+
const registry = ModuleRegistry.getInstance();
|
|
301
|
+
const eventModuleRef = registry.getModuleRef(EventModule);
|
|
302
|
+
|
|
303
|
+
if (!eventModuleRef) {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let eventEmitter: EventEmitter | undefined;
|
|
308
|
+
try {
|
|
309
|
+
eventEmitter = eventModuleRef.container.resolve<EventEmitter>(EVENT_EMITTER_TOKEN);
|
|
310
|
+
} catch {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (!eventEmitter) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// 收集所有监听器类
|
|
319
|
+
const listenerClasses = new Set<Function>();
|
|
320
|
+
|
|
321
|
+
// 1. 添加通过 registerListeners 注册的类
|
|
322
|
+
for (const listenerClass of EventModule.listenerClasses) {
|
|
323
|
+
listenerClasses.add(listenerClass);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// 2. 添加通过选项配置的强制包含类
|
|
327
|
+
if (EventModule.options.includeListeners) {
|
|
328
|
+
for (const listenerClass of EventModule.options.includeListeners) {
|
|
329
|
+
listenerClasses.add(listenerClass);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// 3. 自动扫描所有模块的 providers,并从对应的模块容器中解析实例
|
|
334
|
+
const shouldAutoScan = EventModule.options.autoScan ?? true;
|
|
335
|
+
if (shouldAutoScan === true) {
|
|
336
|
+
const allModuleRefs = Array.from(registry['moduleRefs'].values());
|
|
337
|
+
|
|
338
|
+
for (const moduleRef of allModuleRefs) {
|
|
339
|
+
for (const provider of moduleRef.metadata.providers) {
|
|
340
|
+
// 提取提供者类
|
|
341
|
+
let providerClass: Function | undefined;
|
|
342
|
+
|
|
343
|
+
if (typeof provider === 'function') {
|
|
344
|
+
providerClass = provider;
|
|
345
|
+
} else if ('useClass' in provider && provider.useClass) {
|
|
346
|
+
providerClass = provider.useClass;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// 检查是否是事件监听器类
|
|
350
|
+
if (providerClass && isEventListenerClass(providerClass)) {
|
|
351
|
+
// 检查是否被排除
|
|
352
|
+
const isExcluded = EventModule.options.excludeListeners?.includes(providerClass);
|
|
353
|
+
if (!isExcluded) {
|
|
354
|
+
// 使用模块容器创建 scanner 并注册监听器
|
|
355
|
+
// 这样可以确保解析的实例和控制器中注入的是同一个
|
|
356
|
+
const scanner = new EventListenerScanner(eventEmitter, moduleRef.container);
|
|
357
|
+
scanner.registerListenerClass(providerClass);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// 4. 注册通过选项配置的强制包含类(使用根容器)
|
|
365
|
+
if (EventModule.options.includeListeners) {
|
|
366
|
+
const scanner = new EventListenerScanner(eventEmitter, rootContainer);
|
|
367
|
+
for (const listenerClass of EventModule.options.includeListeners) {
|
|
368
|
+
scanner.registerListenerClass(listenerClass);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// 5. 注册通过 registerListeners 注册的类(使用根容器)
|
|
373
|
+
if (EventModule.listenerClasses.length > 0) {
|
|
374
|
+
const scanner = new EventListenerScanner(eventEmitter, rootContainer);
|
|
375
|
+
scanner.scanAndRegister(EventModule.listenerClasses);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
272
380
|
}
|
package/src/events/types.ts
CHANGED
|
@@ -173,6 +173,25 @@ export interface EventModuleOptions {
|
|
|
173
173
|
* @param payload - 事件负载
|
|
174
174
|
*/
|
|
175
175
|
onError?: (error: Error, event: string | symbol, payload: unknown) => void;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 是否自动扫描和注册事件监听器
|
|
179
|
+
* 当设置为 true 时,框架会在应用启动时自动扫描所有模块中使用 @OnEvent 装饰器的类
|
|
180
|
+
* @default true
|
|
181
|
+
*/
|
|
182
|
+
autoScan?: boolean;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 需要排除的监听器类(不自动注册)
|
|
186
|
+
* 用于在自动扫描时排除某些类
|
|
187
|
+
*/
|
|
188
|
+
excludeListeners?: Function[];
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 额外的监听器类(强制注册)
|
|
192
|
+
* 即使 autoScan 为 false,这些类也会被注册
|
|
193
|
+
*/
|
|
194
|
+
includeListeners?: Function[];
|
|
176
195
|
}
|
|
177
196
|
|
|
178
197
|
/**
|