From 1376cea282e983bcd27723bd817a09633cd5c8c8 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Thu, 20 Oct 2022 17:27:15 +0200 Subject: [PATCH] Small rework --- rollup.config.js | 2 +- src/components/ResultItemVault.svelte | 6 ++---- src/globals.ts | 2 ++ src/main.ts | 6 +++--- src/notes-index.ts | 19 ++++++++++++------- src/search.ts | 10 ++++------ 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index e21b160..8c69a5d 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -14,7 +14,7 @@ if you want to view the source visit the plugins github repository */ ` -const production = !process.env.ROLLUP_WATCH +const production = false//!process.env.ROLLUP_WATCH export default { input: './src/main.ts', diff --git a/src/components/ResultItemVault.svelte b/src/components/ResultItemVault.svelte index 104fe7e..22e3136 100644 --- a/src/components/ResultItemVault.svelte +++ b/src/components/ResultItemVault.svelte @@ -2,7 +2,6 @@ import { cacheManager } from 'src/cache-manager' import { settings, showExcerpt } from 'src/settings' import type { ResultNote } from '../globals' - import * as Search from '../search' import { highlighter, makeExcerpt, stringsToRegex } from '../utils' import ResultItemContainer from './ResultItemContainer.svelte' @@ -10,7 +9,6 @@ export let note: ResultNote $: reg = stringsToRegex(note.foundWords) - $: matches = Search.getMatches(note.content, reg) $: cleanedContent = makeExcerpt(note.content, note.matches[0]?.offset ?? -1) $: glyph = cacheManager.getNoteFromMemCache(note.path)?.doesNotExist $: title = settings.showShortName ? note.basename : note.path @@ -27,9 +25,9 @@ {@html title.replace(reg, highlighter)} - {#if matches.length > 0} + {#if note.matches.length > 0} - {matches.length} {matches.length > 1 ? 'matches' : 'match'} + {note.matches.length} {note.matches.length > 1 ? 'matches' : 'match'} {/if} diff --git a/src/globals.ts b/src/globals.ts index 4679cfa..6f46587 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -1,4 +1,6 @@ +import pLimit from 'p-limit' import { EventBus } from './event-bus' +import { settings } from './settings' export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms diff --git a/src/main.ts b/src/main.ts index 3a91805..e2db52c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -59,7 +59,7 @@ export default class OmnisearchPlugin extends Plugin { // Listeners to keep the search index up-to-date this.registerEvent( this.app.vault.on('create', file => { - NotesIndex.addToIndexAndCache(file) + NotesIndex.addToIndexAndMemCache(file) }) ) this.registerEvent( @@ -76,7 +76,7 @@ export default class OmnisearchPlugin extends Plugin { this.app.vault.on('rename', async (file, oldPath) => { if (file instanceof TFile && isFilePlaintext(file.path)) { NotesIndex.removeFromIndex(oldPath) - await NotesIndex.addToIndexAndCache(file) + await NotesIndex.addToIndexAndMemCache(file) } }) ) @@ -89,7 +89,7 @@ export default class OmnisearchPlugin extends Plugin { onunload(): void { console.log('Omnisearch - Interrupting PDF indexing') - NotesIndex.pdfQueue.clearQueue() + NotesIndex.processQueue.clearQueue() } addRibbonButton(): void { diff --git a/src/notes-index.ts b/src/notes-index.ts index 94eb9fe..48aada4 100644 --- a/src/notes-index.ts +++ b/src/notes-index.ts @@ -9,21 +9,26 @@ import { } from './utils' import { getNonExistingNotes, removeAnchors } from './notes' import { pdfManager } from './pdf-manager' -import type { IndexedDocument } from './globals' import { settings } from './settings' import * as Search from './search' // import PQueue from 'p-queue-compat' -import pLimit from 'p-limit' import { cacheManager } from './cache-manager' +import pLimit from 'p-limit' +import type { IndexedDocument } from './globals' -export const pdfQueue = pLimit(settings.backgroundProcesses) +/** + * Use this processing queue to handle all heavy work + */ +export const processQueue = pLimit(settings.backgroundProcesses) /** * Adds a file to the search index * @param file * @returns */ -export async function addToIndexAndCache(file: TAbstractFile): Promise { +export async function addToIndexAndMemCache( + file: TAbstractFile +): Promise { if (!(file instanceof TFile) || !isFileIndexable(file.path)) { return } @@ -154,7 +159,7 @@ export async function refreshIndex(): Promise { } for (const note of notesToReindex) { removeFromIndex(note.path) - await addToIndexAndCache(note) + await addToIndexAndMemCache(note) await wait(0) } notesToReindex.clear() @@ -173,8 +178,8 @@ export async function indexPDFs() { removeFromIndex(file.path) } input.push( - pdfQueue(async () => { - await addToIndexAndCache(file) + processQueue(async () => { + await addToIndexAndMemCache(file) await cacheManager.writeMinisearchIndex(Search.minisearchInstance) }) ) diff --git a/src/search.ts b/src/search.ts index 8bbd667..0782d2f 100644 --- a/src/search.ts +++ b/src/search.ts @@ -17,7 +17,6 @@ import { import type { Query } from './query' import { settings } from './settings' import * as NotesIndex from './notes-index' -import pLimit from 'p-limit' import { cacheManager } from './cache-manager' export let minisearchInstance: MiniSearch @@ -98,13 +97,14 @@ export async function initGlobalSearchIndex(): Promise { } // Read and index all the files into the search engine - const queue = pLimit(settings.backgroundProcesses) const input = [] for (const file of files) { if (cacheManager.getNoteFromMemCache(file.path)) { NotesIndex.removeFromIndex(file.path) } - input.push(queue(() => NotesIndex.addToIndexAndCache(file))) + input.push( + NotesIndex.processQueue(() => NotesIndex.addToIndexAndMemCache(file)) + ) } await Promise.all(input) @@ -148,7 +148,7 @@ async function search(query: Query): Promise { headings3: settings.weightH3, }, }) - + // Downrank files that are in Obsidian's excluded list if (settings.respectExcluded) { results.forEach(result => { @@ -218,9 +218,7 @@ export async function getSuggestions( options?: Partial<{ singleFilePath: string | null }> ): Promise { // Get the raw results - console.time('search') let results = await search(query) - console.timeEnd('search') if (!results.length) return [] // Extract tags from the query