Fixed a promises race bug + disabled showing previous messages
This commit is contained in:
@@ -40,6 +40,7 @@
|
|||||||
"vite": "^3.2.5"
|
"vite": "^3.2.5"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cancelable-promise": "^4.3.1",
|
||||||
"dexie": "^3.2.2",
|
"dexie": "^3.2.2",
|
||||||
"lodash-es": "4.17.21",
|
"lodash-es": "4.17.21",
|
||||||
"minisearch": "6.0.0-beta.1",
|
"minisearch": "6.0.0-beta.1",
|
||||||
|
|||||||
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
@@ -14,6 +14,7 @@ specifiers:
|
|||||||
'@types/pako': ^2.0.0
|
'@types/pako': ^2.0.0
|
||||||
babel-jest: ^27.5.1
|
babel-jest: ^27.5.1
|
||||||
builtin-modules: ^3.3.0
|
builtin-modules: ^3.3.0
|
||||||
|
cancelable-promise: ^4.3.1
|
||||||
dexie: ^3.2.2
|
dexie: ^3.2.2
|
||||||
esbuild: 0.14.0
|
esbuild: 0.14.0
|
||||||
esbuild-plugin-copy: 1.3.0
|
esbuild-plugin-copy: 1.3.0
|
||||||
@@ -34,6 +35,7 @@ specifiers:
|
|||||||
vite: ^3.2.5
|
vite: ^3.2.5
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
|
cancelable-promise: 4.3.1
|
||||||
dexie: 3.2.2
|
dexie: 3.2.2
|
||||||
lodash-es: 4.17.21
|
lodash-es: 4.17.21
|
||||||
minisearch: 6.0.0-beta.1
|
minisearch: 6.0.0-beta.1
|
||||||
@@ -2112,6 +2114,10 @@ packages:
|
|||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/cancelable-promise/4.3.1:
|
||||||
|
resolution: {integrity: sha512-A/8PwLk/T7IJDfUdQ68NR24QHa8rIlnN/stiJEBo6dmVUkD4K14LswG0w3VwdeK/o7qOwRUR1k2MhK5Rpy2m7A==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/caniuse-lite/1.0.30001436:
|
/caniuse-lite/1.0.30001436:
|
||||||
resolution: {integrity: sha512-ZmWkKsnC2ifEPoWUvSAIGyOYwT+keAaaWPHiQ9DfMqS1t6tfuyFYoWR78TeZtznkEQ64+vGXH9cZrElwR2Mrxg==}
|
resolution: {integrity: sha512-ZmWkKsnC2ifEPoWUvSAIGyOYwT+keAaaWPHiQ9DfMqS1t6tfuyFYoWR78TeZtznkEQ64+vGXH9cZrElwR2Mrxg==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
// the next time we open the modal, the search field will be empty
|
// the next time we open the modal, the search field will be empty
|
||||||
cacheManager.addToSearchHistory('')
|
cacheManager.addToSearchHistory('')
|
||||||
dispatch('input', value)
|
dispatch('input', value)
|
||||||
}, 500)
|
}, 250)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="omnisearch-input-container">
|
<div class="omnisearch-input-container">
|
||||||
|
|||||||
@@ -3,16 +3,32 @@
|
|||||||
import { onDestroy, onMount, tick } from 'svelte'
|
import { onDestroy, onMount, tick } from 'svelte'
|
||||||
import InputSearch from './InputSearch.svelte'
|
import InputSearch from './InputSearch.svelte'
|
||||||
import ModalContainer from './ModalContainer.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 { createNote, openNote } from 'src/tools/notes'
|
||||||
import { getCtrlKeyLabel, getExtension, isFilePDF, loopIndex, } from 'src/tools/utils'
|
import {
|
||||||
import { OmnisearchInFileModal, type OmnisearchVaultModal, } from 'src/components/modals'
|
getCtrlKeyLabel,
|
||||||
|
getExtension,
|
||||||
|
isFilePDF,
|
||||||
|
loopIndex,
|
||||||
|
} from 'src/tools/utils'
|
||||||
|
import {
|
||||||
|
OmnisearchInFileModal,
|
||||||
|
type OmnisearchVaultModal,
|
||||||
|
} from 'src/components/modals'
|
||||||
import ResultItemVault from './ResultItemVault.svelte'
|
import ResultItemVault from './ResultItemVault.svelte'
|
||||||
import { Query } from 'src/search/query'
|
import { Query } from 'src/search/query'
|
||||||
import { settings } from '../settings'
|
import { settings } from '../settings'
|
||||||
import * as NotesIndex from '../notes-index'
|
import * as NotesIndex from '../notes-index'
|
||||||
import { cacheManager } from '../cache-manager'
|
import { cacheManager } from '../cache-manager'
|
||||||
import { searchEngine } from 'src/search/omnisearch'
|
import { searchEngine } from 'src/search/omnisearch'
|
||||||
|
import CancelablePromise, { cancelable } from 'cancelable-promise'
|
||||||
|
import { update } from 'lodash-es'
|
||||||
|
|
||||||
export let modal: OmnisearchVaultModal
|
export let modal: OmnisearchVaultModal
|
||||||
export let previousQuery: string | undefined
|
export let previousQuery: string | undefined
|
||||||
@@ -23,18 +39,30 @@
|
|||||||
let query: Query
|
let query: Query
|
||||||
let indexingStepDesc = ''
|
let indexingStepDesc = ''
|
||||||
let searching = true
|
let searching = true
|
||||||
let refInput: InputSearch|undefined
|
let refInput: InputSearch | undefined
|
||||||
|
|
||||||
|
let pWaitingResults: CancelablePromise | null = null
|
||||||
|
|
||||||
$: selectedNote = resultNotes[selectedIndex]
|
$: selectedNote = resultNotes[selectedIndex]
|
||||||
$: searchQuery = searchQuery ?? previousQuery
|
$: searchQuery = searchQuery ?? previousQuery
|
||||||
$: if (searchQuery) {
|
$: if (searchQuery) {
|
||||||
resultNotes = []
|
if (pWaitingResults) {
|
||||||
|
pWaitingResults.cancel()
|
||||||
|
pWaitingResults = null
|
||||||
|
}
|
||||||
searching = true
|
searching = true
|
||||||
updateResults().then(() => {
|
pWaitingResults = cancelable(
|
||||||
searching = false
|
new Promise((resolve, reject) => {
|
||||||
}).catch((e) => {
|
updateResults()
|
||||||
console.error(e)
|
.then(() => {
|
||||||
})
|
searching = false
|
||||||
|
resolve(null)
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
reject(e)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
searching = false
|
searching = false
|
||||||
resultNotes = []
|
resultNotes = []
|
||||||
@@ -193,7 +221,10 @@
|
|||||||
function switchToInFileModal(): void {
|
function switchToInFileModal(): void {
|
||||||
// Do nothing if the selectedNote is a PDF,
|
// Do nothing if the selectedNote is a PDF,
|
||||||
// or if there is 0 match (e.g indexing in progress)
|
// 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,8 +263,8 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<InputSearch
|
<InputSearch
|
||||||
|
bind:this="{refInput}"
|
||||||
initialValue="{searchQuery}"
|
initialValue="{searchQuery}"
|
||||||
bind:this={refInput}
|
|
||||||
on:input="{e => (searchQuery = e.detail)}"
|
on:input="{e => (searchQuery = e.detail)}"
|
||||||
placeholder="Omnisearch - Vault">
|
placeholder="Omnisearch - Vault">
|
||||||
{#if settings.showCreateButton}
|
{#if settings.showCreateButton}
|
||||||
@@ -258,6 +289,15 @@
|
|||||||
<div style="text-align: center;">
|
<div style="text-align: center;">
|
||||||
{#if !resultNotes.length && searchQuery && !searching}
|
{#if !resultNotes.length && searchQuery && !searching}
|
||||||
We found 0 result for your search here.
|
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}
|
{:else if searching}
|
||||||
Searching...
|
Searching...
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export class Omnisearch {
|
|||||||
private minisearch: MiniSearch
|
private minisearch: MiniSearch
|
||||||
private indexedDocuments: Map<string, number> = new Map()
|
private indexedDocuments: Map<string, number> = new Map()
|
||||||
private previousResults: SearchResult[] = []
|
private previousResults: SearchResult[] = []
|
||||||
|
private previousQuery: Query | null = null
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.minisearch = new MiniSearch(Omnisearch.options)
|
this.minisearch = new MiniSearch(Omnisearch.options)
|
||||||
@@ -154,6 +155,7 @@ export class Omnisearch {
|
|||||||
): Promise<SearchResult[]> {
|
): Promise<SearchResult[]> {
|
||||||
if (query.isEmpty()) {
|
if (query.isEmpty()) {
|
||||||
this.previousResults = []
|
this.previousResults = []
|
||||||
|
this.previousQuery = null
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +174,12 @@ export class Omnisearch {
|
|||||||
headings3: settings.weightH3,
|
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) {
|
if (options.singleFilePath) {
|
||||||
return results.filter(r => r.id === 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
|
(result, index, arr) => arr.findIndex(t => t.id === result.id) === index
|
||||||
)
|
)
|
||||||
|
|
||||||
|
this.previousQuery = query
|
||||||
this.previousResults = results
|
this.previousResults = results
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|||||||
@@ -210,7 +210,8 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName('Simpler search')
|
.setName('Simpler search')
|
||||||
.setDesc(
|
.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 =>
|
.addToggle(toggle =>
|
||||||
toggle.setValue(settings.simpleSearch).onChange(async v => {
|
toggle.setValue(settings.simpleSearch).onChange(async v => {
|
||||||
|
|||||||
Reference in New Issue
Block a user