Bind TypedEventEmitter to DI container and update usage

Added TypedEventEmitter to the dependency injection container and exposed it in ServiceBase. Updated OlPushService to use the injected event emitter instead of directly importing appEvent. Also performed minor code style improvements for consistency.
This commit is contained in:
手瓜一十雪 2025-11-15 10:48:59 +08:00
parent 31bb1e5dee
commit 8e71dec63a
3 changed files with 14 additions and 9 deletions

View File

@ -33,6 +33,7 @@ import { NTQQPacketApi } from './apis/packet';
import { NativePacketHandler } from './packet/handler/client';
import { container, ReceiverServiceRegistry } from './packet/handler/serviceRegister';
import { appEvent } from './packet/handler/eventList';
import { TypedEventEmitter } from './packet/handler/typeEvent';
export * from './wrapper';
export * from './types/index';
export * from './services/index';
@ -44,7 +45,7 @@ export enum NapCatCoreWorkingEnv {
Framework = 2,
}
export function loadQQWrapper (QQVersion: string): WrapperNodeApi {
export function loadQQWrapper(QQVersion: string): WrapperNodeApi {
if (process.env['NAPCAT_WRAPPER_PATH']) {
const wrapperPath = process.env['NAPCAT_WRAPPER_PATH'];
const nativemodule: { exports: WrapperNodeApi; } = { exports: {} as WrapperNodeApi };
@ -71,7 +72,7 @@ export function loadQQWrapper (QQVersion: string): WrapperNodeApi {
process.dlopen(nativemodule, wrapperNodePath);
return nativemodule.exports;
}
export function getMajorPath (QQVersion: string): string {
export function getMajorPath(QQVersion: string): string {
// major.node
let appPath;
if (os.platform() === 'darwin') {
@ -104,7 +105,7 @@ export class NapCatCore {
configLoader: NapCatConfigLoader;
// 通过构造器递过去的 runtime info 应该尽量少
constructor (context: InstanceContext, selfInfo: SelfInfo) {
constructor(context: InstanceContext, selfInfo: SelfInfo) {
this.selfInfo = selfInfo;
this.context = context;
this.util = this.context.wrapper.NodeQQNTWrapperUtil;
@ -122,6 +123,7 @@ export class NapCatCore {
GroupApi: new NTQQGroupApi(this.context, this),
};
container.bind(NapCatCore).toConstantValue(this);
container.bind(TypedEventEmitter).toConstantValue(this.event);
ReceiverServiceRegistry.forEach((ServiceClass, serviceName) => {
container.bind(ServiceClass).toSelf();
console.log(`Registering service handler for: ${serviceName}`);
@ -132,7 +134,7 @@ export class NapCatCore {
});
}
async initCore () {
async initCore() {
this.NapCatDataPath = path.join(this.dataPath, 'NapCat');
fs.mkdirSync(this.NapCatDataPath, { recursive: true });
this.NapCatTempPath = path.join(this.NapCatDataPath, 'temp');
@ -161,7 +163,7 @@ export class NapCatCore {
);
}
get dataPath (): string {
get dataPath(): string {
let result = this.context.wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig();
if (!result) {
result = path.resolve(os.homedir(), './.config/QQ');
@ -171,7 +173,7 @@ export class NapCatCore {
}
// Renamed from 'InitDataListener'
async initNapCatCoreListeners () {
async initNapCatCoreListeners() {
const msgListener = new NodeIKernelMsgListener();
msgListener.onKickedOffLine = (Info: KickedOffLineInfo) => {
@ -209,7 +211,7 @@ export class NapCatCore {
}
}
export async function genSessionConfig (
export async function genSessionConfig(
guid: string,
QQVersionAppid: string,
QQVersion: string,

View File

@ -1,6 +1,7 @@
import "reflect-metadata";
import { Container, injectable } from "inversify";
import { NapCatCore } from "../..";
import { TypedEventEmitter } from "./typeEvent";
export const container = new Container();
@ -10,6 +11,9 @@ export abstract class ServiceBase {
get core(): NapCatCore {
return container.get(NapCatCore);
}
get event() {
return container.get(TypedEventEmitter);
}
abstract handler(seq: number, hex_data: string): Promise<void> | void;
}

View File

@ -1,5 +1,4 @@
import { NapProtoMsg } from "napcat-protobuf";
import { appEvent } from "../packet/handler/eventList";
import { ReceiveService, ServiceBase } from "../packet/handler/serviceRegister";
import { GroupReactNotify, PushMsg } from "../packet/transformer/proto";
@ -23,7 +22,7 @@ export class OlPushService extends ServiceBase {
const code = notify.groupReactionData?.data?.data?.groupReactionDataContent?.code ?? '';
const count = notify.groupReactionData?.data?.data?.groupReactionDataContent?.count ?? 0;
const senderUin = await this.core.apis.UserApi.getUinByUidV2(operatorUid);
appEvent.emit('event:emoji_like', {
this.event.emit('event:emoji_like', {
groupId: groupCode,
senderUin: senderUin,
emojiId: code,