#141 - Added callback when indexing is done
This commit is contained in:
@@ -17,6 +17,13 @@ export type SearchMatchApi = {
|
||||
offset: number
|
||||
}
|
||||
|
||||
let notified = false
|
||||
|
||||
/**
|
||||
* Callbacks to be called when the search index is ready
|
||||
*/
|
||||
let onIndexedCallbacks: Array<() => void> = []
|
||||
|
||||
function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
||||
return results.map(result => {
|
||||
const { score, path, basename, foundWords, matches, content } = result
|
||||
@@ -39,13 +46,27 @@ function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
||||
})
|
||||
}
|
||||
|
||||
async function search(
|
||||
q: string,
|
||||
options: Partial<{ excerpt: boolean }> = {}
|
||||
): Promise<ResultNoteApi[]> {
|
||||
async function search(q: string): Promise<ResultNoteApi[]> {
|
||||
const query = new Query(q)
|
||||
const raw = await searchEngine.getSuggestions(query)
|
||||
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 }
|
||||
|
||||
Reference in New Issue
Block a user