Fix vault index: prevent stuck state and wait for layout ready

- VaultSearch.buildIndex(): wrap body in try/finally so this.indexing
  is always reset to false even if an unexpected error occurs. Previously
  any outer exception left indexing=true permanently, blocking all retries.
- main.ts: replace unreliable setTimeout(3000) with onLayoutReady() so
  the index is built only after Obsidian's vault cache is fully resolved.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
svemagie
2026-03-04 21:13:28 +01:00
parent fbb8545890
commit 5a27b998c3
3 changed files with 121 additions and 114 deletions
+6 -3
View File
@@ -510,6 +510,7 @@ var VaultSearch = class {
this.docVectors.clear();
this.idf.clear();
this.docContents.clear();
try {
const files = this.app.vault.getMarkdownFiles();
const total = files.length;
const df = /* @__PURE__ */ new Map();
@@ -561,9 +562,11 @@ var VaultSearch = class {
this.docVectors.set(path, vec);
}
this.indexed = true;
this.indexing = false;
if (this.onProgress)
this.onProgress(total, total);
} finally {
this.indexing = false;
}
}
isIndexed() {
return this.indexed;
@@ -865,11 +868,11 @@ var MemexChatPlugin = class extends import_obsidian4.Plugin {
}
});
this.addSettingTab(new MemexChatSettingsTab(this.app, this));
setTimeout(() => {
this.app.workspace.onLayoutReady(() => {
if (!this.search.isIndexed()) {
this.search.buildIndex().catch(console.error);
}
}, 3e3);
});
console.log("[Memex Chat] Plugin geladen");
}
onunload() {
+5 -1
View File
@@ -59,6 +59,7 @@ export class VaultSearch {
this.idf.clear();
this.docContents.clear();
try {
const files = this.app.vault.getMarkdownFiles();
const total = files.length;
const df: Map<string, number> = new Map(); // term -> doc count
@@ -121,8 +122,11 @@ export class VaultSearch {
}
this.indexed = true;
this.indexing = false;
if (this.onProgress) this.onProgress(total, total);
} finally {
// Always reset indexing so retries are possible if an error occurred
this.indexing = false;
}
}
isIndexed(): boolean {
+3 -3
View File
@@ -73,12 +73,12 @@ export default class MemexChatPlugin extends Plugin {
// Settings tab
this.addSettingTab(new MemexChatSettingsTab(this.app, this));
// Build index in background after startup
setTimeout(() => {
// Build index once the workspace layout (and vault cache) is fully ready
this.app.workspace.onLayoutReady(() => {
if (!this.search.isIndexed()) {
this.search.buildIndex().catch(console.error);
}
}, 3000);
});
console.log("[Memex Chat] Plugin geladen");
}