From b2f92a86bbe0c9af2bd44ab19dcf60e0681de5e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 12 Sep 2021 16:37:45 +0200 Subject: [PATCH] send postponed logs. --- src/component/internal/logger.ts | 82 +++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/src/component/internal/logger.ts b/src/component/internal/logger.ts index 610c32ef..5c655077 100644 --- a/src/component/internal/logger.ts +++ b/src/component/internal/logger.ts @@ -3,11 +3,14 @@ import * as EVENT from '../types/events' import * as message from '../types/messages' import { Logger } from '../utils/logger' -const RETRY_INTERVAL = 2500 +const MAX_LOG_MESSAGES = 25 +const FLUSH_TIMEOUT_MS = 250 +const RETRY_INTERVAL_MS = 2500 export class NekoLogger extends Logger { private _ws: NekoWebSocket private _logs: message.SystemLog[] = [] + private _timeout: number | null = null private _interval: number | null = null constructor(websocket: NekoWebSocket, scope?: string) { @@ -16,6 +19,13 @@ export class NekoLogger extends Logger { this._ws = websocket } + private _flush() { + if (this._logs.length > 0) { + this._ws.send(EVENT.SYSTEM_LOGS, this._logs) + this._logs = [] + } + } + protected _send(level: string, message: string, fields?: Record) { if (!fields) { fields = { scope: this._scope } @@ -24,36 +34,38 @@ export class NekoLogger extends Logger { } const payload = { level, message, fields } as message.SystemLog - if (!this._ws.connected) { - this._logs.push(payload) + this._logs.push(payload) - // postpone logs sending - if (this._interval == null) { - this._interval = window.setInterval(() => { - if (!this._ws.connected || !this._interval) { - return - } - - if (this._logs.length > 0) { - this._ws.send(EVENT.SYSTEM_LOGS, this._logs) - } - - window.clearInterval(this._interval) - this._interval = null - }, RETRY_INTERVAL) - } - - return + // rotate if exceeded maximum + if (this._logs.length > MAX_LOG_MESSAGES) { + this._logs.shift() } - // abort postponed logs sending - if (this._interval != null) { - window.clearInterval(this._interval) - this._interval = null - } + // postpone logs sending + if (!this._timeout && !this._interval) { + this._timeout = window.setTimeout(() => { + if (!this._timeout) { + return + } - this._ws.send(EVENT.SYSTEM_LOGS, [...this._logs, payload]) - this._logs = [] + if (this._ws.connected) { + this._flush() + } else { + this._interval = window.setInterval(() => { + if (!this._ws.connected || !this._interval) { + return + } + + this._flush() + window.clearInterval(this._interval) + this._interval = null + }, RETRY_INTERVAL_MS) + } + + window.clearTimeout(this._timeout) + this._timeout = null + }, FLUSH_TIMEOUT_MS) + } } public error(message: string, fields?: Record) { @@ -75,4 +87,20 @@ export class NekoLogger extends Logger { this._console('debug', message, fields) this._send('debug', message, fields) } + + public destroy() { + if (this._ws.connected) { + this._flush() + } + + if (this._interval) { + window.clearInterval(this._interval) + this._interval = null + } + + if (this._timeout) { + window.clearTimeout(this._timeout) + this._timeout = null + } + } }