Warn when too many errors in a short time

This commit is contained in:
Simon Cambier
2024-10-13 18:44:22 +02:00
parent fc19f96dfd
commit 7d7902ec9d

View File

@@ -1,4 +1,4 @@
import { normalizePath, TFile } from 'obsidian' import { normalizePath, Notice, TFile } from 'obsidian'
import type { IndexedDocument } from '../globals' import type { IndexedDocument } from '../globals'
import { import {
extractHeadingsFromCache, extractHeadingsFromCache,
@@ -18,14 +18,21 @@ import type OmnisearchPlugin from '../main'
import { getNonExistingNotes } from '../tools/notes' import { getNonExistingNotes } from '../tools/notes'
export class DocumentsRepository { export class DocumentsRepository {
/** /**
* The "live cache", containing all indexed vault files * The "live cache", containing all indexed vault files
* in the form of IndexedDocuments * in the form of IndexedDocuments
*/ */
private documents: Map<string, IndexedDocument> = new Map() private documents: Map<string, IndexedDocument> = new Map()
private errorsCount = 0
private errorsWarned = false
constructor(private plugin: OmnisearchPlugin) {} constructor(private plugin: OmnisearchPlugin) {
setInterval(() => {
if (this.errorsCount > 0) {
--this.errorsCount
}
}, 1000)
}
/** /**
* Set or update the live cache with the content of the given file. * Set or update the live cache with the content of the given file.
@@ -46,7 +53,7 @@ export class DocumentsRepository {
console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e) console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e)
// Shouldn't be needed, but... // Shouldn't be needed, but...
this.removeDocument(path) this.removeDocument(path)
// TODO: increment errors counter this.countError()
} }
} }
@@ -233,4 +240,13 @@ export class DocumentsRepository {
: '', : '',
} }
} }
private countError(): void {
if (++this.errorsCount > 5 && !this.errorsWarned) {
this.errorsWarned = true
new Notice(
'Omnisearch ⚠️ There might be an issue with your cache. You should clean it in Omnisearch settings and restart Obsidian.'
)
}
}
} }