diff --git a/src/common/cancel-task.ts b/src/common/cancel-task.ts index 9d3de652..9fa8161a 100644 --- a/src/common/cancel-task.ts +++ b/src/common/cancel-task.ts @@ -1,4 +1,4 @@ -export type TaskExecutor = (resolve: (value: T | PromiseLike) => void, reject: (reason?: any) => void, onCancel: (callback: () => void) => void) => void; +export type TaskExecutor = (resolve: (value: T | PromiseLike) => void, reject: (reason?: any) => void, onCancel: (callback: () => void) => void) => void | Promise; export class CancelableTask { private promise: Promise; @@ -7,24 +7,30 @@ export class CancelableTask { private cancelListeners: Array<() => void> = []; constructor(executor: TaskExecutor) { - this.promise = new Promise((resolve, reject) => { + this.promise = new Promise(async (resolve, reject) => { const onCancel = (callback: () => void) => { this.cancelCallback = callback; }; - executor( - (value) => { - if (!this.isCanceled) { - resolve(value); - } - }, - (reason) => { - if (!this.isCanceled) { - reject(reason); - } - }, - onCancel - ); + try { + await executor( + (value) => { + if (!this.isCanceled) { + resolve(value); + } + }, + (reason) => { + if (!this.isCanceled) { + reject(reason); + } + }, + onCancel + ); + } catch (error) { + if (!this.isCanceled) { + reject(error); + } + } }); } @@ -68,9 +74,8 @@ export class CancelableTask { } } - async function demoAwait() { - const executor: TaskExecutor = (resolve, reject, onCancel) => { + const executor: TaskExecutor = async (resolve, reject, onCancel) => { let count = 0; const intervalId = setInterval(() => { count++; diff --git a/src/core/apis/group.ts b/src/core/apis/group.ts index ef58f651..a97d4bb1 100644 --- a/src/core/apis/group.ts +++ b/src/core/apis/group.ts @@ -9,10 +9,12 @@ import { NapCatCore, GroupNotify, GroupInfoSource, + ShutUpGroupMember, } from '@/core'; import { isNumeric, solveAsyncProblem } from '@/common/helper'; import { LimitedHashTable } from '@/common/message-unique'; import { NTEventWrapper } from '@/common/event'; +import { CancelableTask, TaskExecutor } from '@/common/cancel-task'; export class NTQQGroupApi { context: InstanceContext; @@ -58,9 +60,28 @@ export class NTQQGroupApi { } async getGroupShutUpMemberList(groupCode: string) { - const data = this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onShutUpMemberListChanged', (group_id) => group_id === groupCode, 1, 1000); - this.context.session.getGroupService().getGroupShutUpMemberList(groupCode); - return (await data)[1]; + const executor: TaskExecutor = async (resolve, reject, onCancel) => { + this.core.eventWrapper.registerListen( + 'NodeIKernelGroupListener/onShutUpMemberListChanged', + (group_id) => group_id === groupCode, + 1, + 1000 + ).then((data) => { + resolve(data[1]) + }).catch(reject); + + onCancel(() => { + reject(new Error('Task was canceled')); + }); + }; + + const task = new CancelableTask(executor); + this.context.session.getGroupService().getGroupShutUpMemberList(groupCode).then(e => { + if (e.result !== 0) { + task.cancel() + } + }) + return await task.catch(() => []); } async clearGroupNotifiesUnreadCount(doubt: boolean) { diff --git a/src/core/services/NodeIKernelGroupService.ts b/src/core/services/NodeIKernelGroupService.ts index 49d2f881..2a4ed5a0 100644 --- a/src/core/services/NodeIKernelGroupService.ts +++ b/src/core/services/NodeIKernelGroupService.ts @@ -251,7 +251,7 @@ export interface NodeIKernelGroupService { setGroupShutUp(groupCode: string, shutUp: boolean): void; - getGroupShutUpMemberList(groupCode: string): Promise; + getGroupShutUpMemberList(groupCode: string): Promise; setMemberShutUp(groupCode: string, memberTimes: { uid: string, timeStamp: number }[]): Promise;