perf: 60 秒内只发送一次掉线消息

This commit is contained in:
Clansty 2023-02-10 13:28:14 +08:00
parent d51fe62801
commit 9fd4ad7370
2 changed files with 18 additions and 1 deletions

View File

@ -1,11 +1,16 @@
import Instance from '../models/Instance';
import OicqClient from '../client/OicqClient';
import { throttle } from '../utils/highLevelFunces';
export default class OicqErrorNotifyController {
private sendMessage = throttle((message: string) => {
return this.instance.ownerChat.sendMessage(message);
}, 1000 * 60);
public constructor(private readonly instance: Instance,
private readonly oicq: OicqClient) {
oicq.on('system.offline', async ({ message }) => {
await instance.ownerChat.sendMessage(`<i>QQ 机器人掉线</i>\n${message}`);
await this.sendMessage(`<i>QQ 机器人掉线</i>\n${message}`);
});
}
}

View File

@ -9,6 +9,18 @@ export function debounce<TArgs extends any[], TRet>(fn: (...originArgs: TArgs) =
};
}
export function throttle<TArgs extends any[], TRet>(fn: (...originArgs: TArgs) => TRet, time = 500) {
let timer: NodeJS.Timeout;
return function (...args) {
if (timer == null) {
fn.apply(this, args);
timer = setTimeout(() => {
timer = null;
}, time);
}
};
}
export function consumer<TArgs extends any[], TRet>(fn: (...originArgs: TArgs) => TRet, time = 100) {
const tasks: Function[] = [];
let timer: NodeJS.Timeout;