refactor: NTEvent Checker

This commit is contained in:
手瓜一十雪 2024-07-22 18:21:29 +08:00
parent c57497cd91
commit ab22f36b8a
3 changed files with 12 additions and 4 deletions

View File

@ -5,6 +5,7 @@ interface Internal_MapKey {
timeout: number,
createtime: number,
func: (...arg: any[]) => any,
checker: ((...args: any[]) => boolean) | undefined,
}
export class ListenerClassBase {
@ -90,10 +91,12 @@ export class NTEventWrapper {
this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.delete(uuid);
return;
}
if (task.checker && task.checker(...args)) {
task.func(...args);
}
});
}
async CallNoListenerEvent<EventType extends (...args: any[]) => Promise<any>,>(EventName = '', timeout: number = 3000, ...args: Parameters<EventType>) {
async CallNoListenerEvent<EventType extends (...args: any[]) => Promise<any>>(EventName = '', timeout: number = 3000, ...args: Parameters<EventType>) {
return new Promise<Awaited<ReturnType<EventType>>>(async (resolve, reject) => {
const EventFunc = this.CreatEventFunction<EventType>(EventName);
let complete = false;
@ -107,14 +110,15 @@ export class NTEventWrapper {
resolve(retData);
});
}
async CallNormalEvent<EventType extends (...args: any[]) => Promise<any>, ListenerType extends (...args: any[]) => void>(EventName = '', ListenerName = '', waitTimes = 1, timeout: number = 3000, ...args: Parameters<EventType>) {
async CallNormalEvent<EventType extends (...args: any[]) => Promise<any>, ListenerType extends (...args: any[]) => void>
(EventName = '', ListenerName = '', waitTimes = 1, timeout: number = 3000, checker: (...args: Parameters<ListenerType>) => boolean, ...args: Parameters<EventType>) {
return new Promise<[EventRet: Awaited<ReturnType<EventType>>, ...Parameters<ListenerType>]>(async (resolve, reject) => {
const id = randomUUID();
let complete = 0;
let retData: Parameters<ListenerType> | undefined = undefined;
let retEvent: any = {};
const databack = () => {
if (complete < waitTimes) {
if (complete == 0) {
reject(new Error('NTEvent EventName:' + EventName + ' ListenerName:' + ListenerName + ' timeout'));
} else {
resolve([retEvent as Awaited<ReturnType<EventType>>, ...retData!]);
@ -128,6 +132,7 @@ export class NTEventWrapper {
const eventCallbak = {
timeout: timeout,
createtime: Date.now(),
checker: checker,
func: (...args: any[]) => {
complete++;
//console.log('func', ...args);

View File

@ -14,6 +14,7 @@ export class NTQQFriendApi {
'NodeIKernelBuddyListener/onBuddyListChange',
1,
5000,
() => true,
forced
);
const friends: User[] = [];

View File

@ -15,6 +15,7 @@ export class NTQQGroupApi {
'NodeIKernelGroupListener/onGroupListUpdate',
1,
5000,
()=>true,
forced
);
return groupList;
@ -63,6 +64,7 @@ export class NTQQGroupApi {
'NodeIKernelGroupListener/onGroupSingleScreenNotifies',
1,
5000,
()=>true,
false,
'',
num