Fixed a promises race bug + disabled showing previous messages

This commit is contained in:
Simon Cambier
2023-01-27 23:03:34 +01:00
parent c896fd4094
commit 6d1fba1429
6 changed files with 71 additions and 15 deletions

View File

@@ -39,7 +39,7 @@
// the next time we open the modal, the search field will be empty
cacheManager.addToSearchHistory('')
dispatch('input', value)
}, 500)
}, 250)
</script>
<div class="omnisearch-input-container">

View File

@@ -3,16 +3,32 @@
import { onDestroy, onMount, tick } from 'svelte'
import InputSearch from './InputSearch.svelte'
import ModalContainer from './ModalContainer.svelte'
import { eventBus, indexingStep, IndexingStepType, type ResultNote, } from 'src/globals'
import {
eventBus,
indexingStep,
IndexingStepType,
type ResultNote,
SPACE_OR_PUNCTUATION,
} from 'src/globals'
import { createNote, openNote } from 'src/tools/notes'
import { getCtrlKeyLabel, getExtension, isFilePDF, loopIndex, } from 'src/tools/utils'
import { OmnisearchInFileModal, type OmnisearchVaultModal, } from 'src/components/modals'
import {
getCtrlKeyLabel,
getExtension,
isFilePDF,
loopIndex,
} from 'src/tools/utils'
import {
OmnisearchInFileModal,
type OmnisearchVaultModal,
} from 'src/components/modals'
import ResultItemVault from './ResultItemVault.svelte'
import { Query } from 'src/search/query'
import { settings } from '../settings'
import * as NotesIndex from '../notes-index'
import { cacheManager } from '../cache-manager'
import { searchEngine } from 'src/search/omnisearch'
import CancelablePromise, { cancelable } from 'cancelable-promise'
import { update } from 'lodash-es'
export let modal: OmnisearchVaultModal
export let previousQuery: string | undefined
@@ -23,18 +39,30 @@
let query: Query
let indexingStepDesc = ''
let searching = true
let refInput: InputSearch|undefined
let refInput: InputSearch | undefined
let pWaitingResults: CancelablePromise | null = null
$: selectedNote = resultNotes[selectedIndex]
$: searchQuery = searchQuery ?? previousQuery
$: if (searchQuery) {
resultNotes = []
if (pWaitingResults) {
pWaitingResults.cancel()
pWaitingResults = null
}
searching = true
updateResults().then(() => {
searching = false
}).catch((e) => {
console.error(e)
})
pWaitingResults = cancelable(
new Promise((resolve, reject) => {
updateResults()
.then(() => {
searching = false
resolve(null)
})
.catch(e => {
reject(e)
})
})
)
} else {
searching = false
resultNotes = []
@@ -193,7 +221,10 @@
function switchToInFileModal(): void {
// Do nothing if the selectedNote is a PDF,
// or if there is 0 match (e.g indexing in progress)
if (selectedNote && (isFilePDF(selectedNote?.path) || !selectedNote?.matches.length)) {
if (
selectedNote &&
(isFilePDF(selectedNote?.path) || !selectedNote?.matches.length)
) {
return
}
@@ -232,8 +263,8 @@
</script>
<InputSearch
bind:this="{refInput}"
initialValue="{searchQuery}"
bind:this={refInput}
on:input="{e => (searchQuery = e.detail)}"
placeholder="Omnisearch - Vault">
{#if settings.showCreateButton}
@@ -258,6 +289,15 @@
<div style="text-align: center;">
{#if !resultNotes.length && searchQuery && !searching}
We found 0 result for your search here.
{#if settings.simpleSearch && searchQuery
.split(SPACE_OR_PUNCTUATION)
.some(w => w.length < 3)}
<br />
<span style="color: var(--text-accent); font-size: small">
You have enabled "Simpler Search" in the settings, try to type more
characters.
</span>
{/if}
{:else if searching}
Searching...
{/if}

View File

@@ -55,6 +55,7 @@ export class Omnisearch {
private minisearch: MiniSearch
private indexedDocuments: Map<string, number> = new Map()
private previousResults: SearchResult[] = []
private previousQuery: Query | null = null
constructor() {
this.minisearch = new MiniSearch(Omnisearch.options)
@@ -154,6 +155,7 @@ export class Omnisearch {
): Promise<SearchResult[]> {
if (query.isEmpty()) {
this.previousResults = []
this.previousQuery = null
return []
}
@@ -172,7 +174,12 @@ export class Omnisearch {
headings3: settings.weightH3,
},
})
if (!results.length) return this.previousResults
// If the query does not return any result,
// retry but with a shorter prefix limit
if (!results.length) {
return []
}
if (options.singleFilePath) {
return results.filter(r => r.id === options.singleFilePath)
@@ -249,6 +256,7 @@ export class Omnisearch {
(result, index, arr) => arr.findIndex(t => t.id === result.id) === index
)
this.previousQuery = query
this.previousResults = results
return results

View File

@@ -210,7 +210,8 @@ export class SettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Simpler search')
.setDesc(
`Enable this if Obsidian often freezes while making searches. This may return fewer results.`
`Enable this if Obsidian often freezes while making searches.
Words shorter than 3 characters won't be used as prefixes; this can reduce search delay but will return fewer results.`
)
.addToggle(toggle =>
toggle.setValue(settings.simpleSearch).onChange(async v => {