Merge pull request #21678 from Snuffleupagus/ViewHistory-findIndex

Shorten the `ViewHistory` class a little bit
This commit is contained in:
Tim van der Meij 2026-08-02 12:10:21 +02:00 committed by GitHub
commit 2475c4ec91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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