From ca8dffecffd679fb11e1b1f317f42d4561f106f4 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sun, 9 May 2021 15:14:16 +0200 Subject: [PATCH] Modify debounce --- package.json | 2 +- src/filewriter.ts | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 4e05ce3..af1a6b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/nodelogging", - "version": "3.0.6", + "version": "3.0.9", "description": "", "main": "out/index.js", "types": "out/index.d.ts", diff --git a/src/filewriter.ts b/src/filewriter.ts index e24f2f0..b5c5426 100644 --- a/src/filewriter.ts +++ b/src/filewriter.ts @@ -33,14 +33,24 @@ export class LoggingFiles implements Adapter { //TODO: Optimise write path -const Debounce = (callback: () => void, iv = 500) => { +const Debounce = (callback: () => void, iv = 500, max = 100) => { let to: any; + let curr = 0; return { trigger: () => { - if (!to) { + curr++; + if (curr >= max) { + if (to) { + clearTimeout(to); + to = undefined; + } + curr = 0; + callback(); + } else if (!to) { to = setTimeout(() => { to = undefined; + curr = 0; callback(); }, iv); } @@ -67,7 +77,7 @@ export class Files { private size: number = 0; private stream: fs.WriteStream = undefined; private lock = new Lock(); - private debounce = Debounce(() => this.checkQueue); + private debounce = Debounce(this.checkQueue.bind(this)); #initialized = false;