Shorten the ViewHistory class a little bit

- Replace the manual loop, used to find an existing entry, with the native [`findIndex` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex).

 - Remove the `fingerprint`/`cacheSize` class fields, since they are completely unused.
This commit is contained in:
Jonas Jenwald 2026-08-01 12:19:20 +02:00
parent 7fc7072f9c
commit 664526ee65

View File

@ -26,29 +26,22 @@ const DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
*/ */
class ViewHistory { class ViewHistory {
constructor(fingerprint, cacheSize = DEFAULT_VIEW_HISTORY_CACHE_SIZE) { constructor(fingerprint, cacheSize = DEFAULT_VIEW_HISTORY_CACHE_SIZE) {
this.fingerprint = fingerprint;
this.cacheSize = cacheSize;
this._initializedPromise = this._readFromStorage().then(databaseStr => { this._initializedPromise = this._readFromStorage().then(databaseStr => {
const database = JSON.parse(databaseStr || "{}"); const database = JSON.parse(databaseStr || "{}");
let index = -1; let index = -1;
if (!Array.isArray(database.files)) { if (!Array.isArray(database.files)) {
database.files = []; database.files = [];
} else { } else {
while (database.files.length >= this.cacheSize) { while (database.files.length >= cacheSize) {
database.files.shift(); database.files.shift();
} }
for (let i = 0, ii = database.files.length; i < ii; i++) { index = database.files.findIndex(
const branch = database.files[i]; branch => branch.fingerprint === fingerprint
if (branch.fingerprint === this.fingerprint) { );
index = i;
break;
}
}
} }
if (index === -1) { if (index === -1) {
index = database.files.push({ fingerprint: this.fingerprint }) - 1; index = database.files.push({ fingerprint }) - 1;
} }
this.file = database.files[index]; this.file = database.files[index];
this.database = database; this.database = database;