export async function ensureIndexed(opts: { state: { indexed: boolean; indexPromise?: Promise | null }; confirmIndex: boolean; confirm: (title: string, msg: string) => Promise; index: () => Promise; /** If true, run indexing even if state.indexed is already true. */ force?: boolean; }): Promise { // If an indexing run is already in-flight, always await it (even if state.indexed is already true). // This matters for forced re-indexing (state.indexed=true, indexPromise!=null). if (opts.state.indexPromise) { await opts.state.indexPromise; return; } if (opts.state.indexed && !opts.force) return; const run = (async () => { if (opts.confirmIndex) { const ok = await opts.confirm( "Index repository?", "KotaDB indexing can take a while. Index this repository now?", ); if (!ok) throw new Error("Indexing cancelled by user"); } await opts.index(); opts.state.indexed = true; })(); opts.state.indexPromise = run; try { await run; } finally { // Always clear the in-flight promise. opts.state.indexPromise = null; } }