From 2f6e25ce4731dd2d77373c2dfea1f7e8b1491248 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Fri, 7 Jun 2024 12:30:00 +0200 Subject: [PATCH] Don't needlessly refresh the live cache --- src/main.ts | 12 +++++++++--- src/notes-indexer.ts | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4924dd0..4817945 100644 --- a/src/main.ts +++ b/src/main.ts @@ -42,6 +42,7 @@ export default class OmnisearchPlugin extends Plugin { public readonly searchEngine = new SearchEngine(this) private ribbonButton?: HTMLElement + private refreshIndexCallback?: () => void constructor(app: App, manifest: PluginManifest) { super(app, manifest) @@ -106,7 +107,6 @@ export default class OmnisearchPlugin extends Plugin { this.app.vault.on('create', file => { if (this.notesIndexer.isFileIndexable(file.path)) { logDebug('Indexing new file', file.path) - // await cacheManager.addToLiveCache(file.path) searchEngine.addFromPaths([file.path]) } }) @@ -121,8 +121,6 @@ export default class OmnisearchPlugin extends Plugin { this.registerEvent( this.app.vault.on('modify', async file => { if (this.notesIndexer.isFileIndexable(file.path)) { - logDebug('Updating file', file.path) - await this.cacheManager.addToLiveCache(file.path) this.notesIndexer.flagNoteForReindex(file) } }) @@ -139,6 +137,10 @@ export default class OmnisearchPlugin extends Plugin { }) ) + this.refreshIndexCallback = this.notesIndexer.refreshIndex.bind(this.notesIndexer) + addEventListener('blur', this.refreshIndexCallback) + removeEventListener + await this.executeFirstLaunchTasks() await this.populateIndex() @@ -165,6 +167,10 @@ export default class OmnisearchPlugin extends Plugin { // @ts-ignore delete globalThis['omnisearch'] + if (this.refreshIndexCallback) { + removeEventListener('blur', this.refreshIndexCallback) + } + // Clear cache when disabling Omnisearch if (process.env.NODE_ENV === 'production') { await this.database.clearCache() diff --git a/src/notes-indexer.ts b/src/notes-indexer.ts index 611051b..89f51f7 100644 --- a/src/notes-indexer.ts +++ b/src/notes-indexer.ts @@ -7,6 +7,7 @@ import { isFileFromDataloomPlugin, isFileImage, isFilePDF, + logDebug, } from './tools/utils' export class NotesIndexer { @@ -23,6 +24,11 @@ export class NotesIndexer { } public async refreshIndex(): Promise { + for (const file of this.notesToReindex) { + logDebug('Updating file', file.path) + await this.plugin.cacheManager.addToLiveCache(file.path) + } + const paths = [...this.notesToReindex].map(n => n.path) if (paths.length) { this.plugin.searchEngine.removeFromPaths(paths)