From 588ea7978ee4fd8b948055476abb435675b721bc Mon Sep 17 00:00:00 2001 From: "Wesley F. Young" Date: Fri, 9 Aug 2024 11:34:18 +0800 Subject: [PATCH] rollback: use legacy event wrapper --- src/common/framework/event-legacy.ts | 70 ++++++++++++++-------------- src/core/core.ts | 6 ++- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/common/framework/event-legacy.ts b/src/common/framework/event-legacy.ts index 27be1010..a9e3e7ff 100644 --- a/src/common/framework/event-legacy.ts +++ b/src/common/framework/event-legacy.ts @@ -8,21 +8,27 @@ interface Internal_MapKey { checker: ((...args: any[]) => boolean) | undefined; } -export class ListenerClassBase { - [key: string]: string; -} +export type ListenerClassBase = Record; export interface ListenerIBase { // eslint-disable-next-line @typescript-eslint/no-misused-new - new (listener: any): ListenerClassBase; + new(listener: any): ListenerClassBase; } -export class NTEventWrapper { - private ListenerMap: { [key: string]: ListenerIBase } | undefined; //ListenerName-Unique -> Listener构造函数 +export class LegacyNTEventWrapper { + private listenerMapping: Record; //ListenerName-Unique -> Listener构造函数 private WrapperSession: NodeIQQNTWrapperSession | undefined; //WrapperSession - private ListenerManger: Map = new Map(); //ListenerName-Unique -> Listener实例 + private listenerManager: Map = new Map(); //ListenerName-Unique -> Listener实例 private EventTask = new Map>>(); //tasks ListenerMainName -> ListenerSubName-> uuid -> {timeout,createtime,func} - constructor() {} + + constructor( + listenerMapping: Record, + wrapperSession: NodeIQQNTWrapperSession + ) { + this.listenerMapping = listenerMapping; + this.WrapperSession = wrapperSession; + } + createProxyDispatch(ListenerMainName: string) { // eslint-disable-next-line @typescript-eslint/no-this-alias const current = this; @@ -34,7 +40,7 @@ export class NTEventWrapper { if (typeof target[prop] === "undefined") { // 如果方法不存在,返回一个函数,这个函数调用existentMethod return (...args: any[]) => { - current.DispatcherListener.apply(current, [ListenerMainName, prop, ...args]).then(); + current.dispatcherListener.apply(current, [ListenerMainName, prop, ...args]).then(); }; } // 如果方法存在,正常返回 @@ -43,17 +49,8 @@ export class NTEventWrapper { } ); } - init({ - ListenerMap, - WrapperSession, - }: { - ListenerMap: { [key: string]: typeof ListenerClassBase }; - WrapperSession: NodeIQQNTWrapperSession; - }) { - this.ListenerMap = ListenerMap; - this.WrapperSession = WrapperSession; - } - CreatEventFunction any>(eventName: string): T | undefined { + + createEventFunction any>(eventName: string): T | undefined { const eventNameArr = eventName.split("/"); type eventType = { [key: string]: () => { [key: string]: (...params: Parameters) => Promise> }; @@ -73,22 +70,24 @@ export class NTEventWrapper { return undefined; } } - CreatListenerFunction(listenerMainName: string, uniqueCode: string = ""): T { - const ListenerType = this.ListenerMap![listenerMainName]; - let Listener = this.ListenerManger.get(listenerMainName + uniqueCode); + + createListenerFunction(listenerMainName: string, uniqueCode: string = ""): T { + const ListenerType = this.listenerMapping![listenerMainName]; + let Listener = this.listenerManager.get(listenerMainName + uniqueCode); if (!Listener && ListenerType) { Listener = new ListenerType(this.createProxyDispatch(listenerMainName)); const ServiceSubName = listenerMainName.match(/^NodeIKernel(.*?)Listener$/)![1]; const Service = "NodeIKernel" + ServiceSubName + "Service/addKernel" + ServiceSubName + "Listener"; - const addfunc = this.CreatEventFunction<(listener: T) => number>(Service); + const addfunc = this.createEventFunction<(listener: T) => number>(Service); addfunc!(Listener as T); //console.log(addfunc!(Listener as T)); - this.ListenerManger.set(listenerMainName + uniqueCode, Listener); + this.listenerManager.set(listenerMainName + uniqueCode, Listener); } return Listener as T; } + //统一回调清理事件 - async DispatcherListener(ListenerMainName: string, ListenerSubName: string, ...args: any[]) { + async dispatcherListener(ListenerMainName: string, ListenerSubName: string, ...args: any[]) { //console.log("[EventDispatcher]",ListenerMainName, ListenerSubName, ...args); this.EventTask.get(ListenerMainName) ?.get(ListenerSubName) @@ -103,15 +102,16 @@ export class NTEventWrapper { } }); } - async CallNoListenerEvent Promise | any>( + + async callNoListenerEvent Promise | any>( EventName = "", timeout: number = 3000, ...args: Parameters ) { return new Promise>>(async (resolve, reject) => { - const EventFunc = this.CreatEventFunction(EventName); + const EventFunc = this.createEventFunction(EventName); let complete = false; - const Timeouter = setTimeout(() => { + setTimeout(() => { if (!complete) { reject(new Error("NTEvent EventName:" + EventName + " timeout")); } @@ -121,6 +121,7 @@ export class NTEventWrapper { resolve(retData); }); } + async RegisterListen void>( ListenerName = "", waitTimes = 1, @@ -141,7 +142,7 @@ export class NTEventWrapper { resolve(retData!); } }; - const Timeouter = setTimeout(databack, timeout); + const timeoutRef = setTimeout(databack, timeout); const eventCallbak = { timeout: timeout, createtime: Date.now(), @@ -150,7 +151,7 @@ export class NTEventWrapper { complete++; retData = args; if (complete >= waitTimes) { - clearTimeout(Timeouter); + clearTimeout(timeoutRef); databack(); } }, @@ -162,7 +163,7 @@ export class NTEventWrapper { this.EventTask.get(ListenerMainName)?.set(ListenerSubName, new Map()); } this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallbak); - this.CreatListenerFunction(ListenerMainName); + this.createListenerFunction(ListenerMainName); }); } async CallNormalEvent< @@ -227,14 +228,13 @@ export class NTEventWrapper { this.EventTask.get(ListenerMainName)?.set(ListenerSubName, new Map()); } this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallbak); - this.CreatListenerFunction(ListenerMainName); - const EventFunc = this.CreatEventFunction(EventName); + this.createListenerFunction(ListenerMainName); + const EventFunc = this.createEventFunction(EventName); retEvent = await EventFunc!(...(args as any[])); } ); } } -export const NTEventDispatch = new NTEventWrapper(); // 示例代码 快速创建事件 // let NTEvent = new NTEventWrapper(); diff --git a/src/core/core.ts b/src/core/core.ts index be71fda4..c67937d2 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -7,6 +7,7 @@ import { proxiedListenerOf } from "@/common/utils/proxy-handler"; import { MsgListener, ProfileListener } from "./listeners"; import { sleep } from "@/common/utils/helper"; import { SelfInfo, LineDevice, SelfStatusInfo } from "./entities"; +import {LegacyNTEventWrapper} from "@/common/framework/event-legacy"; export enum NapCatCoreWorkingEnv { Unknown = 0, @@ -26,7 +27,8 @@ export function loadQQWrapper(QQVersion: string): WrapperNodeApi { export class NapCatCore { readonly context: InstanceContext; - readonly eventChannel: NTEventChannel; + readonly eventWrapper: LegacyNTEventWrapper; + // readonly eventChannel: NTEventChannel; // runtime info, not readonly selfInfo: SelfInfo; @@ -34,7 +36,7 @@ export class NapCatCore { constructor(context: InstanceContext, selfInfo: SelfInfo) { this.selfInfo = selfInfo; this.context = context; - this.eventChannel = new NTEventChannel(context.wrapper, context.session); + this.eventWrapper = new LegacyNTEventWrapper(context.wrapper, context.session); this.initNapCatCoreListeners().then().catch(console.error); }