From 9fd4ad73701ba8f03ededc598150e6ca849e072e Mon Sep 17 00:00:00 2001 From: Clansty Date: Fri, 10 Feb 2023 13:28:14 +0800 Subject: [PATCH] =?UTF-8?q?perf:=2060=20=E7=A7=92=E5=86=85=E5=8F=AA?= =?UTF-8?q?=E5=8F=91=E9=80=81=E4=B8=80=E6=AC=A1=E6=8E=89=E7=BA=BF=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OicqErrorNotifyController.ts | 7 ++++++- src/utils/highLevelFunces.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/controllers/OicqErrorNotifyController.ts b/src/controllers/OicqErrorNotifyController.ts index f0eca88..e0f0889 100644 --- a/src/controllers/OicqErrorNotifyController.ts +++ b/src/controllers/OicqErrorNotifyController.ts @@ -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(`QQ 机器人掉线\n${message}`); + await this.sendMessage(`QQ 机器人掉线\n${message}`); }); } } diff --git a/src/utils/highLevelFunces.ts b/src/utils/highLevelFunces.ts index 4a9ee18..38a5145 100644 --- a/src/utils/highLevelFunces.ts +++ b/src/utils/highLevelFunces.ts @@ -9,6 +9,18 @@ export function debounce(fn: (...originArgs: TArgs) = }; } +export function throttle(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(fn: (...originArgs: TArgs) => TRet, time = 100) { const tasks: Function[] = []; let timer: NodeJS.Timeout;