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.docVectors.clear();
this.idf.clear(); this.idf.clear();
this.docContents.clear(); this.docContents.clear();
try {
const files = this.app.vault.getMarkdownFiles(); const files = this.app.vault.getMarkdownFiles();
const total = files.length; const total = files.length;
const df = /* @__PURE__ */ new Map(); const df = /* @__PURE__ */ new Map();
@@ -561,9 +562,11 @@ var VaultSearch = class {
this.docVectors.set(path, vec); this.docVectors.set(path, vec);
} }
this.indexed = true; this.indexed = true;
this.indexing = false;
if (this.onProgress) if (this.onProgress)
this.onProgress(total, total); this.onProgress(total, total);
} finally {
this.indexing = false;
}
} }
isIndexed() { isIndexed() {
return this.indexed; return this.indexed;
@@ -865,11 +868,11 @@ var MemexChatPlugin = class extends import_obsidian4.Plugin {
} }
}); });
this.addSettingTab(new MemexChatSettingsTab(this.app, this)); this.addSettingTab(new MemexChatSettingsTab(this.app, this));
setTimeout(() => { this.app.workspace.onLayoutReady(() => {
if (!this.search.isIndexed()) { if (!this.search.isIndexed()) {
this.search.buildIndex().catch(console.error); this.search.buildIndex().catch(console.error);
} }
}, 3e3); });
console.log("[Memex Chat] Plugin geladen"); console.log("[Memex Chat] Plugin geladen");
} }
onunload() { onunload() {
+5 -1
View File
@@ -59,6 +59,7 @@ export class VaultSearch {
this.idf.clear(); this.idf.clear();
this.docContents.clear(); this.docContents.clear();
try {
const files = this.app.vault.getMarkdownFiles(); const files = this.app.vault.getMarkdownFiles();
const total = files.length; const total = files.length;
const df: Map<string, number> = new Map(); // term -> doc count const df: Map<string, number> = new Map(); // term -> doc count
@@ -121,8 +122,11 @@ export class VaultSearch {
} }
this.indexed = true; this.indexed = true;
this.indexing = false;
if (this.onProgress) this.onProgress(total, total); if (this.onProgress) this.onProgress(total, total);
} finally {
// Always reset indexing so retries are possible if an error occurred
this.indexing = false;
}
} }
isIndexed(): boolean { isIndexed(): boolean {
+3 -3
View File
@@ -73,12 +73,12 @@ export default class MemexChatPlugin extends Plugin {
// Settings tab // Settings tab
this.addSettingTab(new MemexChatSettingsTab(this.app, this)); this.addSettingTab(new MemexChatSettingsTab(this.app, this));
// Build index in background after startup // Build index once the workspace layout (and vault cache) is fully ready
setTimeout(() => { this.app.workspace.onLayoutReady(() => {
if (!this.search.isIndexed()) { if (!this.search.isIndexed()) {
this.search.buildIndex().catch(console.error); this.search.buildIndex().catch(console.error);
} }
}, 3000); });
console.log("[Memex Chat] Plugin geladen"); console.log("[Memex Chat] Plugin geladen");
} }