mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
fix: #733
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
export type TaskExecutor<T> = (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void, onCancel: (callback: () => void) => void) => void;
|
export type TaskExecutor<T> = (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void, onCancel: (callback: () => void) => void) => void | Promise<void>;
|
||||||
|
|
||||||
export class CancelableTask<T> {
|
export class CancelableTask<T> {
|
||||||
private promise: Promise<T>;
|
private promise: Promise<T>;
|
||||||
@@ -7,24 +7,30 @@ export class CancelableTask<T> {
|
|||||||
private cancelListeners: Array<() => void> = [];
|
private cancelListeners: Array<() => void> = [];
|
||||||
|
|
||||||
constructor(executor: TaskExecutor<T>) {
|
constructor(executor: TaskExecutor<T>) {
|
||||||
this.promise = new Promise<T>((resolve, reject) => {
|
this.promise = new Promise<T>(async (resolve, reject) => {
|
||||||
const onCancel = (callback: () => void) => {
|
const onCancel = (callback: () => void) => {
|
||||||
this.cancelCallback = callback;
|
this.cancelCallback = callback;
|
||||||
};
|
};
|
||||||
|
|
||||||
executor(
|
try {
|
||||||
(value) => {
|
await executor(
|
||||||
if (!this.isCanceled) {
|
(value) => {
|
||||||
resolve(value);
|
if (!this.isCanceled) {
|
||||||
}
|
resolve(value);
|
||||||
},
|
}
|
||||||
(reason) => {
|
},
|
||||||
if (!this.isCanceled) {
|
(reason) => {
|
||||||
reject(reason);
|
if (!this.isCanceled) {
|
||||||
}
|
reject(reason);
|
||||||
},
|
}
|
||||||
onCancel
|
},
|
||||||
);
|
onCancel
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (!this.isCanceled) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,9 +74,8 @@ export class CancelableTask<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function demoAwait() {
|
async function demoAwait() {
|
||||||
const executor: TaskExecutor<number> = (resolve, reject, onCancel) => {
|
const executor: TaskExecutor<number> = async (resolve, reject, onCancel) => {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
const intervalId = setInterval(() => {
|
const intervalId = setInterval(() => {
|
||||||
count++;
|
count++;
|
||||||
|
@@ -9,10 +9,12 @@ import {
|
|||||||
NapCatCore,
|
NapCatCore,
|
||||||
GroupNotify,
|
GroupNotify,
|
||||||
GroupInfoSource,
|
GroupInfoSource,
|
||||||
|
ShutUpGroupMember,
|
||||||
} from '@/core';
|
} from '@/core';
|
||||||
import { isNumeric, solveAsyncProblem } from '@/common/helper';
|
import { isNumeric, solveAsyncProblem } from '@/common/helper';
|
||||||
import { LimitedHashTable } from '@/common/message-unique';
|
import { LimitedHashTable } from '@/common/message-unique';
|
||||||
import { NTEventWrapper } from '@/common/event';
|
import { NTEventWrapper } from '@/common/event';
|
||||||
|
import { CancelableTask, TaskExecutor } from '@/common/cancel-task';
|
||||||
|
|
||||||
export class NTQQGroupApi {
|
export class NTQQGroupApi {
|
||||||
context: InstanceContext;
|
context: InstanceContext;
|
||||||
@@ -58,9 +60,28 @@ export class NTQQGroupApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getGroupShutUpMemberList(groupCode: string) {
|
async getGroupShutUpMemberList(groupCode: string) {
|
||||||
const data = this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onShutUpMemberListChanged', (group_id) => group_id === groupCode, 1, 1000);
|
const executor: TaskExecutor<ShutUpGroupMember[]> = async (resolve, reject, onCancel) => {
|
||||||
this.context.session.getGroupService().getGroupShutUpMemberList(groupCode);
|
this.core.eventWrapper.registerListen(
|
||||||
return (await data)[1];
|
'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) {
|
async clearGroupNotifiesUnreadCount(doubt: boolean) {
|
||||||
|
@@ -251,7 +251,7 @@ export interface NodeIKernelGroupService {
|
|||||||
|
|
||||||
setGroupShutUp(groupCode: string, shutUp: boolean): void;
|
setGroupShutUp(groupCode: string, shutUp: boolean): void;
|
||||||
|
|
||||||
getGroupShutUpMemberList(groupCode: string): Promise<any>;
|
getGroupShutUpMemberList(groupCode: string): Promise<GeneralCallResult>;
|
||||||
|
|
||||||
setMemberShutUp(groupCode: string, memberTimes: { uid: string, timeStamp: number }[]): Promise<void>;
|
setMemberShutUp(groupCode: string, memberTimes: { uid: string, timeStamp: number }[]): Promise<void>;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user