From ab22f36b8aaaed06c79eff48f704ffaebb66a9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 22 Jul 2024 18:21:29 +0800 Subject: [PATCH] refactor: NTEvent Checker --- src/common/utils/EventTask.ts | 13 +++++++++---- src/core/src/apis/friend.ts | 1 + src/core/src/apis/group.ts | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/common/utils/EventTask.ts b/src/common/utils/EventTask.ts index 696973db..8bea2ba8 100644 --- a/src/common/utils/EventTask.ts +++ b/src/common/utils/EventTask.ts @@ -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; } - task.func(...args); + if (task.checker && task.checker(...args)) { + task.func(...args); + } }); } - async CallNoListenerEvent Promise,>(EventName = '', timeout: number = 3000, ...args: Parameters) { + async CallNoListenerEvent Promise>(EventName = '', timeout: number = 3000, ...args: Parameters) { return new Promise>>(async (resolve, reject) => { const EventFunc = this.CreatEventFunction(EventName); let complete = false; @@ -107,14 +110,15 @@ export class NTEventWrapper { resolve(retData); }); } - async CallNormalEvent Promise, ListenerType extends (...args: any[]) => void>(EventName = '', ListenerName = '', waitTimes = 1, timeout: number = 3000, ...args: Parameters) { + async CallNormalEvent Promise, ListenerType extends (...args: any[]) => void> + (EventName = '', ListenerName = '', waitTimes = 1, timeout: number = 3000, checker: (...args: Parameters) => boolean, ...args: Parameters) { return new Promise<[EventRet: Awaited>, ...Parameters]>(async (resolve, reject) => { const id = randomUUID(); let complete = 0; let retData: Parameters | 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>, ...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); diff --git a/src/core/src/apis/friend.ts b/src/core/src/apis/friend.ts index e6cb961e..01b9cb6a 100644 --- a/src/core/src/apis/friend.ts +++ b/src/core/src/apis/friend.ts @@ -14,6 +14,7 @@ export class NTQQFriendApi { 'NodeIKernelBuddyListener/onBuddyListChange', 1, 5000, + () => true, forced ); const friends: User[] = []; diff --git a/src/core/src/apis/group.ts b/src/core/src/apis/group.ts index 37614f26..459e98c5 100644 --- a/src/core/src/apis/group.ts +++ b/src/core/src/apis/group.ts @@ -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