From bd42c4c415cfd15d48a957fa8d64f6dd8c2227c5 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Tue, 19 Feb 2019 16:36:43 -0700 Subject: [PATCH] fix: don't truncate all history when max is hit truncate a smaller amount of the oldest entries --- src/lib/history.mjs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/history.mjs b/src/lib/history.mjs index 1b1f939..9def930 100644 --- a/src/lib/history.mjs +++ b/src/lib/history.mjs @@ -1,6 +1,9 @@ import fs from 'fs'; import path from 'path'; +const MAX_ITEMS = 1000; // max entries in history +const TRUNCATE_AMOUNT = 200; // count of entries to remove when max is hit + export default class History { constructor() { this.filePath = path.resolve('data', 'history.json'); @@ -20,9 +23,11 @@ export default class History { // prevent duplicate entries if (this.get(doc.id)) return; - // add doc to history, truncate as needed + // add doc to history this.db.push(doc); - if (this.db.length > 1000) this.db.splice(0, 1000); + + // truncate as needed, removing first TRUNCATE_AMOUNT entries from the array + if (this.db.length > MAX_ITEMS) this.db.splice(0, TRUNCATE_AMOUNT); // write the updated content to the file fs.writeFileSync(this.filePath, JSON.stringify(this.db), { encoding: 'utf8' });