mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-16 04:45:46 +00:00
refactor: event
This commit is contained in:
parent
31f8f884f1
commit
7f2d57ef62
@ -2,106 +2,17 @@ import { NodeIKernelMsgListener } from "@/core";
|
||||
import { NodeIQQNTWrapperSession } from "@/core/wrapper";
|
||||
|
||||
import { randomUUID } from "crypto";
|
||||
// export enum NTEventMode {
|
||||
// Once = 1,
|
||||
// Twice = 2
|
||||
// }
|
||||
export enum NTEventMode {
|
||||
Once = 1,
|
||||
Twice = 2
|
||||
}
|
||||
|
||||
// export interface NTEventType<U extends (...args: any[]) => Promise<any>> {
|
||||
// EventName: string,
|
||||
// EventFunction: U,
|
||||
// ListenerName: string,
|
||||
// ListenerFunction: Function
|
||||
// }
|
||||
|
||||
// interface Internal_MapKey {
|
||||
// mode: NTEventMode,
|
||||
// timeout: number,
|
||||
// createtime: number,
|
||||
// func: Function
|
||||
// }
|
||||
|
||||
// export class NTEvent<T extends (...args: any[]) => any, R = any> {
|
||||
// EventData: NTEventType<T>;
|
||||
// EventTask: Map<string, Internal_MapKey> = new Map<string, Internal_MapKey>();
|
||||
// constructor(params: NTEventType<T>) {
|
||||
// params.ListenerFunction = this.DispatcherListener.bind(this);
|
||||
// this.EventData = params;
|
||||
// this.EventData.EventFunction = params.EventFunction.bind(this) as any;
|
||||
// }
|
||||
// async DispatcherListener(...args: any[]) {
|
||||
// console.log(...args);
|
||||
// this.EventTask.forEach((task, uuid) => {
|
||||
// if (task.createtime + task.timeout > Date.now()) {
|
||||
// this.EventTask.delete(uuid);
|
||||
// return;
|
||||
// }
|
||||
// task.func(...args);
|
||||
// })
|
||||
// }
|
||||
// async CallTwiceEvent(timeout: number = 3000, ...args: Parameters<T>) {
|
||||
// return new Promise<R>((resolve, reject) => {
|
||||
// const id = randomUUID();
|
||||
// let complete = 0;
|
||||
// let retData: R | undefined = undefined;
|
||||
// let databack = () => {
|
||||
// if (!complete) {
|
||||
// this.EventTask.delete(id);
|
||||
// reject(new Error('NTEvent EventName:' + this.EventData.EventName + ' EventListener:' + this.EventData.ListenerName + ' timeout'));
|
||||
// } else {
|
||||
// resolve(retData as R);
|
||||
// }
|
||||
// }
|
||||
|
||||
// let Timeouter = setTimeout(databack, timeout);
|
||||
|
||||
// this.EventTask.set(id, {
|
||||
// mode: NTEventMode.Once,
|
||||
// timeout: timeout,
|
||||
// createtime: Date.now(),
|
||||
// func: (...args: any[]) => {
|
||||
// complete++;
|
||||
// retData = args as R;
|
||||
// if (complete == 2) {
|
||||
// clearTimeout(Timeouter);
|
||||
// databack();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// this.EventData.EventFunction(...args);
|
||||
// });
|
||||
// }
|
||||
|
||||
// async CallOnceEvent(timeout: number = 3000, ...args: Parameters<T>) {
|
||||
// return new Promise<R>((resolve, reject) => {
|
||||
// const id = randomUUID();
|
||||
// let complete = false;
|
||||
// let retData: R | undefined = undefined;
|
||||
// let databack = () => {
|
||||
// if (!complete) {
|
||||
// this.EventTask.delete(id);
|
||||
// reject(new Error('NTEvent EventName:' + this.EventData.EventName + ' EventListener:' + this.EventData.ListenerName + ' timeout'));
|
||||
// } else {
|
||||
// resolve(retData as R);
|
||||
// }
|
||||
// }
|
||||
// let Timeouter = setTimeout(databack, timeout);
|
||||
|
||||
// this.EventTask.set(id, {
|
||||
// mode: NTEventMode.Once,
|
||||
// timeout: timeout,
|
||||
// createtime: Date.now(),
|
||||
// func: (...args: any[]) => {
|
||||
// clearTimeout(Timeouter);
|
||||
// complete = true;
|
||||
// retData = args as R;
|
||||
// databack();
|
||||
// }
|
||||
// });
|
||||
// this.EventData.EventFunction(...args);
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
interface Internal_MapKey {
|
||||
mode: NTEventMode,
|
||||
timeout: number,
|
||||
createtime: number,
|
||||
func: Function
|
||||
}
|
||||
|
||||
export class ListenerClassBase {
|
||||
[key: string]: string;
|
||||
@ -111,6 +22,7 @@ export class NTEventWrapper {
|
||||
private ListenerMap: Map<string, typeof ListenerClassBase> | undefined;
|
||||
private WrapperSession: NodeIQQNTWrapperSession | undefined;
|
||||
private ListenerManger: Map<string, ListenerClassBase> = new Map<string, ListenerClassBase>();
|
||||
private EventTask: Map<string, Internal_MapKey> = new Map<string, Internal_MapKey>();
|
||||
constructor() {
|
||||
|
||||
}
|
||||
@ -119,7 +31,6 @@ export class NTEventWrapper {
|
||||
this.WrapperSession = WrapperSession;
|
||||
}
|
||||
CreatEventFunction<T extends (...args: any) => any>(eventName: string): T | undefined {
|
||||
// 将 eventName 'NodeIKernelProfileLikeService/GetTest' => 转换成 this.WrapperSession.getProfileLikeService().getTest
|
||||
let eventNameArr = eventName.split('/');
|
||||
type eventType = {
|
||||
[key: string]: () => { [key: string]: (...params: Parameters<T>) => Promise<ReturnType<T>> }
|
||||
@ -135,20 +46,107 @@ export class NTEventWrapper {
|
||||
return event as T;
|
||||
}
|
||||
return undefined;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
// 获取某个Listener
|
||||
// 获取某个Listener 存在返回 不存在创建
|
||||
CreatListenerFunction<T>(listenerName: string, uniqueCode: string = ""): T {
|
||||
let ListenerType = this.ListenerMap!.get(listenerName);
|
||||
let Listener = this.ListenerManger.get(listenerName + uniqueCode);
|
||||
if (!Listener && ListenerType) {
|
||||
Listener = new ListenerType();
|
||||
let ServiceSubName = listenerName.match(/^NodeIKernel(.*?)Listener$/);
|
||||
let Service = "NodeIKernel" + ServiceSubName + "Service/addKernel" + ServiceSubName + "Listener";
|
||||
let addfunc = this.CreatEventFunction<(listener: T) => number>(Service);
|
||||
addfunc!(Listener as T);
|
||||
this.ListenerManger.set(listenerName + uniqueCode, Listener);
|
||||
}
|
||||
return Listener as T;
|
||||
}
|
||||
// 如果存在覆盖注册 不存在则创建Listener
|
||||
RigisterListener<T extends { [key: string]: (...args: any) => any }>(listenerName: string, uniqueCode: string = "NTEvent", cb: (...args: any) => any) {
|
||||
let ListenerNameList = listenerName.split('/');
|
||||
let ListenerMain = ListenerNameList[0];
|
||||
let ListenerMethod = ListenerNameList[1];
|
||||
let Listener = this.CreatListenerFunction<T>(ListenerMain, uniqueCode); //uniqueCode NTEvent
|
||||
(Listener[ListenerMethod] as any) = cb;
|
||||
}
|
||||
//统一回调清理事件
|
||||
async DispatcherListener(...args: any[]) {
|
||||
this.EventTask.forEach((task, uuid) => {
|
||||
if (task.createtime + task.timeout > Date.now()) {
|
||||
this.EventTask.delete(uuid);
|
||||
return;
|
||||
}
|
||||
if (task.mode == NTEventMode.Once) {
|
||||
this.EventTask.delete(uuid);
|
||||
}
|
||||
task.func(...args);
|
||||
})
|
||||
}
|
||||
async CallOnceEvent<EventType extends (...args: any[]) => any, ListenerType extends (...args: any[]) => void>(EventName = '', ListenerName = '', timeout: number = 3000, ...args: Parameters<EventType>) {
|
||||
return new Promise<ArrayLike<Parameters<ListenerType>>>((resolve, reject) => {
|
||||
const id = randomUUID();
|
||||
let complete = false;
|
||||
let retData: ArrayLike<Parameters<ListenerType>> | undefined = undefined;
|
||||
let databack = () => {
|
||||
if (!complete) {
|
||||
this.EventTask.delete(id);
|
||||
reject(new Error('NTEvent EventName:' + EventName + ' ListenerName:' + ListenerName + ' timeout'));
|
||||
} else {
|
||||
resolve(retData as ArrayLike<Parameters<ListenerType>>);
|
||||
}
|
||||
}
|
||||
let Timeouter = setTimeout(databack, timeout);
|
||||
|
||||
this.EventTask.set(id, {
|
||||
mode: NTEventMode.Once,
|
||||
timeout: timeout,
|
||||
createtime: Date.now(),
|
||||
func: (...args: any[]) => {
|
||||
clearTimeout(Timeouter);
|
||||
complete = true;
|
||||
retData = args as ArrayLike<Parameters<ListenerType>>;
|
||||
databack();
|
||||
}
|
||||
});
|
||||
let EventFunc = this.CreatEventFunction<EventType>(EventName);
|
||||
EventFunc!(...args);
|
||||
});
|
||||
}
|
||||
async CallTwiceEvent<EventType extends (...args: any[]) => any, ListenerType extends (...args: any[]) => void>(EventName = '', ListenerName = '', timeout: number = 3000, ...args: Parameters<EventType>) {
|
||||
return new Promise<ArrayLike<Parameters<ListenerType>>>((resolve, reject) => {
|
||||
const id = randomUUID();
|
||||
let complete = 0;
|
||||
let retData: ArrayLike<Parameters<ListenerType>> | undefined = undefined;
|
||||
let databack = () => {
|
||||
if (complete < 2) {
|
||||
this.EventTask.delete(id);
|
||||
reject(new Error('NTEvent EventName:' + EventName + ' ListenerName:' + ListenerName + ' timeout'));
|
||||
} else {
|
||||
resolve(retData as ArrayLike<Parameters<ListenerType>>);
|
||||
}
|
||||
}
|
||||
let Timeouter = setTimeout(databack, timeout);
|
||||
|
||||
this.EventTask.set(id, {
|
||||
mode: NTEventMode.Once,
|
||||
timeout: timeout,
|
||||
createtime: Date.now(),
|
||||
func: (...args: any[]) => {
|
||||
complete++;
|
||||
retData = args as ArrayLike<Parameters<ListenerType>>;
|
||||
if (complete == 2) {
|
||||
clearTimeout(Timeouter);
|
||||
databack();
|
||||
}
|
||||
}
|
||||
});
|
||||
let EventFunc = this.CreatEventFunction<EventType>(EventName);
|
||||
EventFunc!(...args);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
// 示例代码 快速创建事件
|
||||
// let NTEvent = new NTEventWrapper();
|
||||
|
Loading…
Reference in New Issue
Block a user