Skip to content

Commit

Permalink
refactor AsyncQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
linyuchen committed May 1, 2024
1 parent a68349c commit 4a76afb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
33 changes: 33 additions & 0 deletions src/common/utils/AsyncQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { sleep } from '@/common/utils/helper';

type AsyncQueueTask = (() => void) | Promise<void> ;


export class AsyncQueue {
private tasks: (AsyncQueueTask)[] = [];

public addTask(task: AsyncQueueTask) {
this.tasks.push(task);
if (this.tasks.length === 1) {
this.runQueue().then().catch(()=>{});
}
}

private async runQueue() {
while (this.tasks.length > 0) {
const task = this.tasks[0];
try {
if (task instanceof Promise) {
await task;
}
else{
task();
}
} catch (e) {
console.error(e);
}
this.tasks.shift();
await sleep(100);
}
}
}
37 changes: 0 additions & 37 deletions src/common/utils/asyncQuene.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { postLoginStatus } from '@/common/utils/umami';
import { checkVersion } from '@/common/utils/version';
import { log, logDebug, logError, LogLevel, setLogLevel } from '@/common/utils/log';
import { NapCatOnebot11 } from '@/onebot11/main';
import { hookApi } from '@/core/external/hook';

program
.option('-q, --qq <type>', 'QQ号')
Expand All @@ -15,6 +16,11 @@ program
const cmdOptions = program.opts();
// console.log(process.argv);

for(let k=0; k<30; k++) {
new Promise((r, j) => {
hookApi.getRKey();
}).then();
}
checkVersion().then((remoteVersion: string) => {
const localVersion = require('./package.json').version;
const localVersionList = localVersion.split('.');
Expand Down
6 changes: 3 additions & 3 deletions src/onebot11/action/group/GetGroupMemberInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GetGroupMemberInfo extends BaseAction<PayloadType, OB11GroupMember> {
actionName = ActionName.GetGroupMemberInfo;

protected async _handle(payload: PayloadType) {
let WebGroupMember = await WebApi.getGroupMembers(payload.group_id.toString());
const WebGroupMember = await WebApi.getGroupMembers(payload.group_id.toString());
const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString());
// log(member);
if (member) {
Expand All @@ -30,7 +30,7 @@ class GetGroupMemberInfo extends BaseAction<PayloadType, OB11GroupMember> {
} catch (e) {
logDebug('获取群成员详细信息失败, 只能返回基础信息', e);
}
let retMember = OB11Constructor.groupMember(payload.group_id.toString(), member);
const retMember = OB11Constructor.groupMember(payload.group_id.toString(), member);
for (let i = 0, len = WebGroupMember.length; i < len; i++) {
if (WebGroupMember[i]?.uin && WebGroupMember[i].uin === retMember.user_id) {
retMember.join_time = WebGroupMember[i]?.join_time;
Expand All @@ -42,7 +42,7 @@ class GetGroupMemberInfo extends BaseAction<PayloadType, OB11GroupMember> {
}
return retMember;
} else {
throw (`群成员${payload.user_id}不存在`);
throw (`群(${payload.group_id})成员${payload.user_id}不存在`);
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/onebot11/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ import { deleteGroup, getFriend, getGroupMember, groupMembers, selfInfo, tempGro
import { NTQQFileApi, NTQQGroupApi, NTQQUserApi } from '../core/src/apis';
import http from 'http';
import { OB11GroupMsgEmojiLikeEvent } from '@/onebot11/event/notice/OB11MsgEmojiLikeEvent';
import { ImageQuene } from '@/common/utils/asyncQuene';
import { AsyncQueue } from '@/common/utils/AsyncQueue';

const imageQueue = new AsyncQueue();

export class OB11Constructor {
static async message(msg: RawMessage): Promise<OB11Message> {
Expand Down Expand Up @@ -144,18 +145,18 @@ export class OB11Constructor {
message_data['data']['file'] = element.picElement.fileName;
// message_data["data"]["path"] = element.picElement.sourcePath
// let currentRKey = "CAQSKAB6JWENi5LMk0kc62l8Pm3Jn1dsLZHyRLAnNmHGoZ3y_gDZPqZt-64"
ImageQuene.addTask(NTQQFileApi.getImageUrl, [msg], (result: string) => {
message_data['data']['url'] = result;
await new Promise<void>((resolve, reject) => {
const task = new Promise<void>((taskResolve, taskReject) => {
log('开始获取图片url');
NTQQFileApi.getImageUrl(msg).then((url) => {
message_data['data']['url'] = url;
log('获取图片url结果:', url);
taskResolve();
resolve();
});
});
imageQueue.addTask(task);
});
await ImageQuene.runQueue();
// 缓解获取失败
try {
if (!message_data['data']['url']) {
message_data['data']['url'] = "";
}
} catch (e) {
message_data['data']['url'] = "";
}

// message_data["data"]["file_id"] = element.picElement.fileUuid
message_data['data']['file_size'] = element.picElement.fileSize;
Expand Down

0 comments on commit 4a76afb

Please sign in to comment.