rollback: use legacy event wrapper

This commit is contained in:
Wesley F. Young 2024-08-09 11:34:18 +08:00
parent 946f12cf6a
commit 588ea7978e
2 changed files with 39 additions and 37 deletions

View File

@ -8,21 +8,27 @@ interface Internal_MapKey {
checker: ((...args: any[]) => boolean) | undefined;
}
export class ListenerClassBase {
[key: string]: string;
}
export type ListenerClassBase = Record<string, string>;
export interface ListenerIBase {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new(listener: any): ListenerClassBase;
}
export class NTEventWrapper {
private ListenerMap: { [key: string]: ListenerIBase } | undefined; //ListenerName-Unique -> Listener构造函数
export class LegacyNTEventWrapper {
private listenerMapping: Record<string, ListenerIBase>; //ListenerName-Unique -> Listener构造函数
private WrapperSession: NodeIQQNTWrapperSession | undefined; //WrapperSession
private ListenerManger: Map<string, ListenerClassBase> = new Map<string, ListenerClassBase>(); //ListenerName-Unique -> Listener实例
private listenerManager: Map<string, ListenerClassBase> = new Map<string, ListenerClassBase>(); //ListenerName-Unique -> Listener实例
private EventTask = new Map<string, Map<string, Map<string, Internal_MapKey>>>(); //tasks ListenerMainName -> ListenerSubName-> uuid -> {timeout,createtime,func}
constructor() {}
constructor(
listenerMapping: Record<string, ListenerIBase>,
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<T extends (...args: any) => any>(eventName: string): T | undefined {
createEventFunction<T extends (...args: any) => any>(eventName: string): T | undefined {
const eventNameArr = eventName.split("/");
type eventType = {
[key: string]: () => { [key: string]: (...params: Parameters<T>) => Promise<ReturnType<T>> };
@ -73,22 +70,24 @@ export class NTEventWrapper {
return undefined;
}
}
CreatListenerFunction<T>(listenerMainName: string, uniqueCode: string = ""): T {
const ListenerType = this.ListenerMap![listenerMainName];
let Listener = this.ListenerManger.get(listenerMainName + uniqueCode);
createListenerFunction<T>(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<EventType extends (...args: any[]) => Promise<any> | any>(
async callNoListenerEvent<EventType extends (...args: any[]) => Promise<any> | any>(
EventName = "",
timeout: number = 3000,
...args: Parameters<EventType>
) {
return new Promise<Awaited<ReturnType<EventType>>>(async (resolve, reject) => {
const EventFunc = this.CreatEventFunction<EventType>(EventName);
const EventFunc = this.createEventFunction<EventType>(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<ListenerType extends (...args: any[]) => 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<EventType>(EventName);
this.createListenerFunction(ListenerMainName);
const EventFunc = this.createEventFunction<EventType>(EventName);
retEvent = await EventFunc!(...(args as any[]));
}
);
}
}
export const NTEventDispatch = new NTEventWrapper();
// 示例代码 快速创建事件
// let NTEvent = new NTEventWrapper();

View File

@ -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);
}