From 69283630d5a125f037b276e0850b95f15b15f955 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 10 Apr 2022 20:06:50 +0200 Subject: [PATCH] #7 - Tentative fix Added a 100ms pause every 100 indexed notes, to not block the main thread. --- src/main.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 573220f..a3e6d02 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { Plugin, SuggestModal, TAbstractFile, TFile } from 'obsidian' +import { Notice, Plugin, SuggestModal, TAbstractFile, TFile } from 'obsidian' import MiniSearch from 'minisearch' import removeMarkdown from 'remove-markdown' @@ -59,9 +59,18 @@ export default class OmnisearchPlugin extends Plugin { }) // Index files that are already present + const start = new Date().getTime() const files = this.app.vault.getMarkdownFiles() - for (const file of files) { - await this.addToIndex(file) + for (let i = 0; i < files.length; ++i) { + if (i % 100 === 0) await wait(100) + await this.addToIndex(files[i]) + } + if (files.length > 0) { + new Notice( + `Omnisearch - Loaded ${files.length} notes in ${ + new Date().getTime() - start + }ms`, + ) } } @@ -315,3 +324,9 @@ function removeFrontMatter(text: string): string { // Regex to recognize YAML Front Matter (at beginning of file, 3 hyphens, than any charecter, including newlines, then 3 hyphens). return text.replace(regexYaml, '') } + +function wait(ms: number): Promise { + return new Promise((resolve, reject) => { + setTimeout(resolve, ms) + }) +}