mirror of https://github.com/Nofated095/Q2TG.git
perf: 60 秒内只发送一次掉线消息
This commit is contained in:
parent
d51fe62801
commit
9fd4ad7370
|
@ -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}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue