From 253aae15fcac61910ae02f4af6215026b5ec3d67 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 26 Jun 2018 14:07:52 +0200 Subject: [PATCH] Ensure that all entries above the current `cacheSize` is removed when initializing a `ViewHistory` instance Note how, in the current code, only *one* old history entry would ever be removed. That would mean that if e.g. the `cacheSize` is reduced, it would potentially require loading of multiple files before the database would be correctly pruned. Furthermore, in the case where the database was empty on load there's no need to attempt to shrink it, since trying to reduce the size of an *empty* array won't do much :-) --- web/view_history.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/web/view_history.js b/web/view_history.js index 79ebf0e03..c50d8064c 100644 --- a/web/view_history.js +++ b/web/view_history.js @@ -33,11 +33,12 @@ class ViewHistory { let database = JSON.parse(databaseStr || '{}'); if (!('files' in database)) { database.files = []; + } else { + while (database.files.length >= this.cacheSize) { + database.files.shift(); + } } - if (database.files.length >= this.cacheSize) { - database.files.shift(); - } - let index; + let index = -1; for (let i = 0, length = database.files.length; i < length; i++) { let branch = database.files[i]; if (branch.fingerprint === this.fingerprint) { @@ -45,7 +46,7 @@ class ViewHistory { break; } } - if (typeof index !== 'number') { + if (index === -1) { index = database.files.push({ fingerprint: this.fingerprint, }) - 1; } this.file = database.files[index];