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 :-)
This commit is contained in:
Jonas Jenwald 2018-06-26 14:07:52 +02:00
parent 66ffdc4c5b
commit 253aae15fc

View File

@ -33,11 +33,12 @@ class ViewHistory {
let database = JSON.parse(databaseStr || '{}');
if (!('files' in database)) {
database.files = [];
}
if (database.files.length >= this.cacheSize) {
} else {
while (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];