Skip to content

Commit

Permalink
fix: echoMap 没有被清理
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Jul 16, 2024
1 parent a00e72f commit 340384a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions main/src/client/NapCatClient/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export class NapCatClient extends QQClient {
});
}

private readonly echoMap: { [key: string]: { resolve: (result: any) => void; reject: (result: any) => void } } = {};
private readonly echoMap = new Map<string, { resolve: (result: any) => void; reject: (result: any) => void }>();

public async callApi<T extends keyof WSSendReturn>(action: T, params?: WSSendParam[T]): Promise<WSSendReturn[T]> {
return new Promise<WSSendReturn[T]>((resolve, reject) => {
const echo = `${new Date().getTime()}${random.int(100000, 999999)}`;
this.echoMap[echo] = { resolve, reject };
this.echoMap.set(echo, { resolve, reject });
this.ws.send(JSON.stringify({ action, params, echo }));
this.logger.debug('send', JSON.stringify({ action, params, echo }));
});
Expand All @@ -55,8 +55,9 @@ export class NapCatClient extends QQClient {
this.logger.debug('receive', message);
const data = JSON.parse(message) as WSReceiveHandler[keyof WSReceiveHandler] & { echo: string; status: 'ok' | 'error'; data: any; message: string };
if (data.echo) {
const promise = this.echoMap[data.echo];
const promise = this.echoMap.get(data.echo);
if (!promise) return;
this.echoMap.delete(data.echo);
if (data.status === 'ok') {
promise.resolve(data.data);
}
Expand Down
2 changes: 1 addition & 1 deletion main/src/client/NapCatClient/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const messageElemToNapCatSendable = async (elem: SendableElem): Promise<{
type: elem.type,
data: {
file: elem.file,
summary: elem.type,
summary: 'Q2TG ' + elem.type,
name: elem.type,
},
} as any,
Expand Down

0 comments on commit 340384a

Please sign in to comment.