#141 - Added callback when indexing is done

This commit is contained in:
Simon Cambier
2023-02-11 16:52:33 +01:00
parent 8c78c91cc3
commit c1d82dab54
3 changed files with 34 additions and 8 deletions

View File

@@ -74,9 +74,13 @@ object `omnisearch` (`window.omnisearch`)
```ts ```ts
// API: // API:
{ type OmnisearchApi = {
// Returns a promise that will contain the same results as the Vault modal // Returns a promise that will contain the same results as the Vault modal
search: (query: string) => Promise<ResultNoteApi[]> search: (query: string) => Promise<ResultNoteApi[]>,
// Register a callback that will be called when the indexing is done
registerOnIndexed: (callback: () => void) => void,
// Unregister a callback that was previously registered
unregisterOnIndexed: (callback: () => void) => void,
} }
type ResultNoteApi = { type ResultNoteApi = {

View File

@@ -17,7 +17,7 @@ import {
IndexingStepType, IndexingStepType,
isCacheEnabled, isCacheEnabled,
} from './globals' } from './globals'
import api from './tools/api' import api, { notifyOnIndexed } from './tools/api'
import { isFileIndexable } from './tools/utils' import { isFileIndexable } from './tools/utils'
import { database, OmnisearchCache } from './database' import { database, OmnisearchCache } from './database'
import * as NotesIndex from './notes-index' import * as NotesIndex from './notes-index'
@@ -210,6 +210,7 @@ export default class OmnisearchPlugin extends Plugin {
new Notice(`Omnisearch - Your files have been indexed.`) new Notice(`Omnisearch - Your files have been indexed.`)
} }
indexingStep.set(IndexingStepType.Done) indexingStep.set(IndexingStepType.Done)
notifyOnIndexed()
} }
} }

View File

@@ -17,6 +17,13 @@ export type SearchMatchApi = {
offset: number offset: number
} }
let notified = false
/**
* Callbacks to be called when the search index is ready
*/
let onIndexedCallbacks: Array<() => void> = []
function mapResults(results: ResultNote[]): ResultNoteApi[] { function mapResults(results: ResultNote[]): ResultNoteApi[] {
return results.map(result => { return results.map(result => {
const { score, path, basename, foundWords, matches, content } = result const { score, path, basename, foundWords, matches, content } = result
@@ -39,13 +46,27 @@ function mapResults(results: ResultNote[]): ResultNoteApi[] {
}) })
} }
async function search( async function search(q: string): Promise<ResultNoteApi[]> {
q: string,
options: Partial<{ excerpt: boolean }> = {}
): Promise<ResultNoteApi[]> {
const query = new Query(q) const query = new Query(q)
const raw = await searchEngine.getSuggestions(query) const raw = await searchEngine.getSuggestions(query)
return mapResults(raw) return mapResults(raw)
} }
export default { search } function registerOnIndexed(cb: () => void): void {
onIndexedCallbacks.push(cb)
// Immediately call the callback if the indexing is already ready done
if (notified) {
cb()
}
}
function unregisterOnIndexed(cb: () => void): void {
onIndexedCallbacks = onIndexedCallbacks.filter(o => o !== cb)
}
export function notifyOnIndexed(): void {
notified = true
onIndexedCallbacks.forEach(cb => cb())
}
export default { search, registerOnIndexed, unregisterOnIndexed }