mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-16 13:01:14 +00:00
rollback: use legacy event wrapper
This commit is contained in:
parent
946f12cf6a
commit
588ea7978e
@ -8,21 +8,27 @@ interface Internal_MapKey {
|
|||||||
checker: ((...args: any[]) => boolean) | undefined;
|
checker: ((...args: any[]) => boolean) | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ListenerClassBase {
|
export type ListenerClassBase = Record<string, string>;
|
||||||
[key: string]: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ListenerIBase {
|
export interface ListenerIBase {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-new
|
// eslint-disable-next-line @typescript-eslint/no-misused-new
|
||||||
new (listener: any): ListenerClassBase;
|
new(listener: any): ListenerClassBase;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NTEventWrapper {
|
export class LegacyNTEventWrapper {
|
||||||
private ListenerMap: { [key: string]: ListenerIBase } | undefined; //ListenerName-Unique -> Listener构造函数
|
private listenerMapping: Record<string, ListenerIBase>; //ListenerName-Unique -> Listener构造函数
|
||||||
private WrapperSession: NodeIQQNTWrapperSession | undefined; //WrapperSession
|
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}
|
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) {
|
createProxyDispatch(ListenerMainName: string) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||||
const current = this;
|
const current = this;
|
||||||
@ -34,7 +40,7 @@ export class NTEventWrapper {
|
|||||||
if (typeof target[prop] === "undefined") {
|
if (typeof target[prop] === "undefined") {
|
||||||
// 如果方法不存在,返回一个函数,这个函数调用existentMethod
|
// 如果方法不存在,返回一个函数,这个函数调用existentMethod
|
||||||
return (...args: any[]) => {
|
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,
|
createEventFunction<T extends (...args: any) => any>(eventName: string): T | undefined {
|
||||||
WrapperSession,
|
|
||||||
}: {
|
|
||||||
ListenerMap: { [key: string]: typeof ListenerClassBase };
|
|
||||||
WrapperSession: NodeIQQNTWrapperSession;
|
|
||||||
}) {
|
|
||||||
this.ListenerMap = ListenerMap;
|
|
||||||
this.WrapperSession = WrapperSession;
|
|
||||||
}
|
|
||||||
CreatEventFunction<T extends (...args: any) => any>(eventName: string): T | undefined {
|
|
||||||
const eventNameArr = eventName.split("/");
|
const eventNameArr = eventName.split("/");
|
||||||
type eventType = {
|
type eventType = {
|
||||||
[key: string]: () => { [key: string]: (...params: Parameters<T>) => Promise<ReturnType<T>> };
|
[key: string]: () => { [key: string]: (...params: Parameters<T>) => Promise<ReturnType<T>> };
|
||||||
@ -73,22 +70,24 @@ export class NTEventWrapper {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CreatListenerFunction<T>(listenerMainName: string, uniqueCode: string = ""): T {
|
|
||||||
const ListenerType = this.ListenerMap![listenerMainName];
|
createListenerFunction<T>(listenerMainName: string, uniqueCode: string = ""): T {
|
||||||
let Listener = this.ListenerManger.get(listenerMainName + uniqueCode);
|
const ListenerType = this.listenerMapping![listenerMainName];
|
||||||
|
let Listener = this.listenerManager.get(listenerMainName + uniqueCode);
|
||||||
if (!Listener && ListenerType) {
|
if (!Listener && ListenerType) {
|
||||||
Listener = new ListenerType(this.createProxyDispatch(listenerMainName));
|
Listener = new ListenerType(this.createProxyDispatch(listenerMainName));
|
||||||
const ServiceSubName = listenerMainName.match(/^NodeIKernel(.*?)Listener$/)![1];
|
const ServiceSubName = listenerMainName.match(/^NodeIKernel(.*?)Listener$/)![1];
|
||||||
const Service = "NodeIKernel" + ServiceSubName + "Service/addKernel" + ServiceSubName + "Listener";
|
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);
|
addfunc!(Listener as T);
|
||||||
//console.log(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;
|
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);
|
//console.log("[EventDispatcher]",ListenerMainName, ListenerSubName, ...args);
|
||||||
this.EventTask.get(ListenerMainName)
|
this.EventTask.get(ListenerMainName)
|
||||||
?.get(ListenerSubName)
|
?.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 = "",
|
EventName = "",
|
||||||
timeout: number = 3000,
|
timeout: number = 3000,
|
||||||
...args: Parameters<EventType>
|
...args: Parameters<EventType>
|
||||||
) {
|
) {
|
||||||
return new Promise<Awaited<ReturnType<EventType>>>(async (resolve, reject) => {
|
return new Promise<Awaited<ReturnType<EventType>>>(async (resolve, reject) => {
|
||||||
const EventFunc = this.CreatEventFunction<EventType>(EventName);
|
const EventFunc = this.createEventFunction<EventType>(EventName);
|
||||||
let complete = false;
|
let complete = false;
|
||||||
const Timeouter = setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!complete) {
|
if (!complete) {
|
||||||
reject(new Error("NTEvent EventName:" + EventName + " timeout"));
|
reject(new Error("NTEvent EventName:" + EventName + " timeout"));
|
||||||
}
|
}
|
||||||
@ -121,6 +121,7 @@ export class NTEventWrapper {
|
|||||||
resolve(retData);
|
resolve(retData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async RegisterListen<ListenerType extends (...args: any[]) => void>(
|
async RegisterListen<ListenerType extends (...args: any[]) => void>(
|
||||||
ListenerName = "",
|
ListenerName = "",
|
||||||
waitTimes = 1,
|
waitTimes = 1,
|
||||||
@ -141,7 +142,7 @@ export class NTEventWrapper {
|
|||||||
resolve(retData!);
|
resolve(retData!);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const Timeouter = setTimeout(databack, timeout);
|
const timeoutRef = setTimeout(databack, timeout);
|
||||||
const eventCallbak = {
|
const eventCallbak = {
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
createtime: Date.now(),
|
createtime: Date.now(),
|
||||||
@ -150,7 +151,7 @@ export class NTEventWrapper {
|
|||||||
complete++;
|
complete++;
|
||||||
retData = args;
|
retData = args;
|
||||||
if (complete >= waitTimes) {
|
if (complete >= waitTimes) {
|
||||||
clearTimeout(Timeouter);
|
clearTimeout(timeoutRef);
|
||||||
databack();
|
databack();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -162,7 +163,7 @@ export class NTEventWrapper {
|
|||||||
this.EventTask.get(ListenerMainName)?.set(ListenerSubName, new Map());
|
this.EventTask.get(ListenerMainName)?.set(ListenerSubName, new Map());
|
||||||
}
|
}
|
||||||
this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallbak);
|
this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallbak);
|
||||||
this.CreatListenerFunction(ListenerMainName);
|
this.createListenerFunction(ListenerMainName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async CallNormalEvent<
|
async CallNormalEvent<
|
||||||
@ -227,14 +228,13 @@ export class NTEventWrapper {
|
|||||||
this.EventTask.get(ListenerMainName)?.set(ListenerSubName, new Map());
|
this.EventTask.get(ListenerMainName)?.set(ListenerSubName, new Map());
|
||||||
}
|
}
|
||||||
this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallbak);
|
this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallbak);
|
||||||
this.CreatListenerFunction(ListenerMainName);
|
this.createListenerFunction(ListenerMainName);
|
||||||
const EventFunc = this.CreatEventFunction<EventType>(EventName);
|
const EventFunc = this.createEventFunction<EventType>(EventName);
|
||||||
retEvent = await EventFunc!(...(args as any[]));
|
retEvent = await EventFunc!(...(args as any[]));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export const NTEventDispatch = new NTEventWrapper();
|
|
||||||
|
|
||||||
// 示例代码 快速创建事件
|
// 示例代码 快速创建事件
|
||||||
// let NTEvent = new NTEventWrapper();
|
// let NTEvent = new NTEventWrapper();
|
||||||
|
@ -7,6 +7,7 @@ import { proxiedListenerOf } from "@/common/utils/proxy-handler";
|
|||||||
import { MsgListener, ProfileListener } from "./listeners";
|
import { MsgListener, ProfileListener } from "./listeners";
|
||||||
import { sleep } from "@/common/utils/helper";
|
import { sleep } from "@/common/utils/helper";
|
||||||
import { SelfInfo, LineDevice, SelfStatusInfo } from "./entities";
|
import { SelfInfo, LineDevice, SelfStatusInfo } from "./entities";
|
||||||
|
import {LegacyNTEventWrapper} from "@/common/framework/event-legacy";
|
||||||
|
|
||||||
export enum NapCatCoreWorkingEnv {
|
export enum NapCatCoreWorkingEnv {
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
@ -26,7 +27,8 @@ export function loadQQWrapper(QQVersion: string): WrapperNodeApi {
|
|||||||
|
|
||||||
export class NapCatCore {
|
export class NapCatCore {
|
||||||
readonly context: InstanceContext;
|
readonly context: InstanceContext;
|
||||||
readonly eventChannel: NTEventChannel;
|
readonly eventWrapper: LegacyNTEventWrapper;
|
||||||
|
// readonly eventChannel: NTEventChannel;
|
||||||
|
|
||||||
// runtime info, not readonly
|
// runtime info, not readonly
|
||||||
selfInfo: SelfInfo;
|
selfInfo: SelfInfo;
|
||||||
@ -34,7 +36,7 @@ export class NapCatCore {
|
|||||||
constructor(context: InstanceContext, selfInfo: SelfInfo) {
|
constructor(context: InstanceContext, selfInfo: SelfInfo) {
|
||||||
this.selfInfo = selfInfo;
|
this.selfInfo = selfInfo;
|
||||||
this.context = context;
|
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);
|
this.initNapCatCoreListeners().then().catch(console.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user