Search optimizations and fixes. Prevent re-renders on hash change (redux). Clear previous search results at the start of a new search.
This commit is contained in:
@@ -52,20 +52,30 @@ const closeSearch = (ev: Event) => {
|
|||||||
updateSearch(ev)
|
updateSearch(ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track pending route update
|
||||||
|
let pendingRouteUpdate: number | null = null
|
||||||
|
|
||||||
const updateSearch = (ev: Event) => {
|
const updateSearch = (ev: Event) => {
|
||||||
const q = (ev.target as HTMLInputElement).value
|
const q = (ev.target as HTMLInputElement).value
|
||||||
let p = props.path.join('/')
|
const loc = props.path.join('/')
|
||||||
p = p ? `/${p}` : ''
|
|
||||||
const url = q ? `${p}//${q}` : (p || '/')
|
|
||||||
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
|
||||||
|
|
||||||
// Start search immediately via store (worker handles it async)
|
// Start search immediately via store (worker handles it async)
|
||||||
store.search(q, props.path.join('/'))
|
store.search(q, loc)
|
||||||
|
|
||||||
// Update route in next frame to keep typing responsive
|
// Cancel any pending route update
|
||||||
requestAnimationFrame(() => {
|
if (pendingRouteUpdate !== null) {
|
||||||
if (!props.query && q) router.push(u)
|
cancelAnimationFrame(pendingRouteUpdate)
|
||||||
else router.replace(u)
|
}
|
||||||
|
|
||||||
|
// Schedule route update - will be cancelled if user types again
|
||||||
|
pendingRouteUpdate = requestAnimationFrame(() => {
|
||||||
|
pendingRouteUpdate = null
|
||||||
|
let p = loc
|
||||||
|
p = p ? `/${p}` : ''
|
||||||
|
const url = q ? `${p}//${q}` : (p || '/')
|
||||||
|
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
||||||
|
// Use replace to avoid building up history for each keystroke
|
||||||
|
router.replace(u)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const toggleSearchInput = (ev: Event) => {
|
const toggleSearchInput = (ev: Event) => {
|
||||||
|
|||||||
@@ -135,8 +135,13 @@ export const useMainStore = defineStore('main', {
|
|||||||
const id = ++searchId
|
const id = ++searchId
|
||||||
searchStore = this // Store reference for worker callback
|
searchStore = this // Store reference for worker callback
|
||||||
|
|
||||||
|
// Update query immediately so watchers know we're handling this
|
||||||
|
this.query = query
|
||||||
|
|
||||||
|
// Clear old results immediately - don't show stale data
|
||||||
|
this.searchResults = []
|
||||||
|
|
||||||
if (!query) {
|
if (!query) {
|
||||||
this.searchResults = []
|
|
||||||
this.searchLoading = false
|
this.searchLoading = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,17 +33,14 @@ const props = defineProps<{
|
|||||||
|
|
||||||
// Folder path for component keys - only recreate component when folder changes, not search
|
// Folder path for component keys - only recreate component when folder changes, not search
|
||||||
const folderPath = computed(() => props.path.join('/'))
|
const folderPath = computed(() => props.path.join('/'))
|
||||||
// Trigger search when query changes (from route, e.g., page load or back button)
|
|
||||||
// Note: Direct typing triggers search immediately via HeaderMain, this is for route-based changes
|
// Handle route-based search changes (back/forward navigation, direct URL)
|
||||||
|
// Skip if store.query already matches (means we triggered this via typing)
|
||||||
watch(
|
watch(
|
||||||
() => [props.query, props.path.join('/')] as const,
|
() => [props.query, props.path.join('/')] as const,
|
||||||
([query, loc]) => {
|
([query, loc]) => {
|
||||||
// Only trigger if results don't match current query (avoid duplicate searches)
|
if (store.query === query) return // Already searching this query
|
||||||
if (query && store.searchResults.length === 0) {
|
store.search(query, loc)
|
||||||
store.search(query, loc)
|
|
||||||
} else if (!query) {
|
|
||||||
store.search('', loc) // Clear search results
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
)
|
)
|
||||||
@@ -71,7 +68,6 @@ const documents = computed(() => {
|
|||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
store.fileExplorer = fileExplorer.value
|
store.fileExplorer = fileExplorer.value
|
||||||
store.query = props.query
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Only auto-switch gallery mode when entering a new folder or on initial file list load
|
// Only auto-switch gallery mode when entering a new folder or on initial file list load
|
||||||
|
|||||||
@@ -36,58 +36,118 @@ interface ResultMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Worker state
|
// Worker state
|
||||||
let documents: WorkerDoc[] = []
|
|
||||||
let recentDocuments: WorkerDoc[] = [] // Sorted by mtime descending
|
let recentDocuments: WorkerDoc[] = [] // Sorted by mtime descending
|
||||||
let currentSearchId = 0
|
let currentSearchId = 0
|
||||||
|
|
||||||
// Haystack formatting (same as main thread utils)
|
// Search result cache - cleared when documents change
|
||||||
function haystackFormat(str: string): string {
|
interface CacheEntry {
|
||||||
const based = str.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
query: string // Normalized query string
|
||||||
return '^' + based + '$'
|
results: WorkerDoc[] // Matched results (up to limit)
|
||||||
|
complete: boolean // True if search scanned all documents
|
||||||
|
}
|
||||||
|
const searchCache: CacheEntry[] = []
|
||||||
|
const MAX_CACHE_SIZE = 10
|
||||||
|
const RESULT_LIMIT = 100
|
||||||
|
|
||||||
|
// Normalize string for search (remove diacritics, lowercase)
|
||||||
|
// Haystack adds ^ and $ markers to allow matching start/end of name
|
||||||
|
function normalizeHaystack(str: string): string {
|
||||||
|
return '^' + str.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase() + '$'
|
||||||
}
|
}
|
||||||
|
|
||||||
// Needle formatting
|
function normalizeQuery(str: string): string {
|
||||||
function needleFormat(query: string) {
|
return str.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
||||||
const based = query.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
|
||||||
return { based, words: based.split(/\s+/) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if haystack includes needle
|
// Test if document matches search query
|
||||||
function localeIncludes(haystack: string, filter: { based: string; words: string[] }): boolean {
|
function matches(haystack: string, query: string, words: string[]): boolean {
|
||||||
const { based, words } = filter
|
return haystack.includes(query) || words.every(word => haystack.includes(word))
|
||||||
return haystack.includes(based) || (words && words.every(word => haystack.includes(word)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collator for sorting
|
// Collator for sorting
|
||||||
const collator = new Intl.Collator('en', { sensitivity: 'base', numeric: true, usage: 'search' })
|
const collator = new Intl.Collator('en', { sensitivity: 'base', numeric: true })
|
||||||
|
|
||||||
// Sort by mtime descending
|
// Yield control to allow new messages to be processed
|
||||||
function sortByRecent(docs: WorkerDoc[]): WorkerDoc[] {
|
const yieldControl = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 0))
|
||||||
return [...docs].sort((a, b) => b.mtime - a.mtime)
|
|
||||||
|
// Find best cache entry to filter from
|
||||||
|
// Returns entry if new query's results are guaranteed to be a subset of cached results
|
||||||
|
function findCacheSubset(query: string): CacheEntry | null {
|
||||||
|
// Look for a cached query that the new query starts with
|
||||||
|
// e.g., cached "foo" can be used for "foobar" or "foo bar"
|
||||||
|
// The longer the prefix, the better (fewer items to filter)
|
||||||
|
let best: CacheEntry | null = null
|
||||||
|
for (const entry of searchCache) {
|
||||||
|
if (query.startsWith(entry.query)) {
|
||||||
|
if (!best || entry.query.length > best.query.length) {
|
||||||
|
best = entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return best
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield control to check for new messages
|
// Add result to cache
|
||||||
function yieldControl(): Promise<void> {
|
function addToCache(query: string, results: WorkerDoc[], complete: boolean) {
|
||||||
return new Promise(resolve => setTimeout(resolve, 0))
|
// Remove existing entry for same query if any
|
||||||
|
const idx = searchCache.findIndex(e => e.query === query)
|
||||||
|
if (idx !== -1) searchCache.splice(idx, 1)
|
||||||
|
// Add to front (most recent)
|
||||||
|
searchCache.unshift({ query, results, complete })
|
||||||
|
// Trim cache
|
||||||
|
if (searchCache.length > MAX_CACHE_SIZE) searchCache.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache (called when documents change)
|
||||||
|
function clearCache() {
|
||||||
|
searchCache.length = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform search with incremental results
|
// Perform search with incremental results
|
||||||
async function performSearch(query: string, loc: string, searchId: number) {
|
async function performSearch(rawQuery: string, loc: string, searchId: number) {
|
||||||
const needle = needleFormat(query)
|
const query = normalizeQuery(rawQuery)
|
||||||
const limit = 100
|
const words = query.split(/\s+/)
|
||||||
const batchSize = 500 // Smaller batches for faster incremental feedback
|
|
||||||
const results: WorkerDoc[] = []
|
const results: WorkerDoc[] = []
|
||||||
let lastResultCount = 0
|
let lastResultCount = 0
|
||||||
|
|
||||||
for (let i = 0; i < recentDocuments.length && results.length < limit; i += batchSize) {
|
// Check cache for exact match
|
||||||
// Check if search was superseded
|
const exactMatch = searchCache.find(e => e.query === query)
|
||||||
if (currentSearchId !== searchId) return
|
if (exactMatch) {
|
||||||
|
if (currentSearchId === searchId) {
|
||||||
|
postResults(exactMatch.results, rawQuery, loc, searchId, true)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we can filter from a cached superset
|
||||||
|
const cacheEntry = findCacheSubset(query)
|
||||||
|
if (cacheEntry) {
|
||||||
|
// Fast path: filter from cached results
|
||||||
|
for (const doc of cacheEntry.results) {
|
||||||
|
if (matches(doc.haystack, query, words)) {
|
||||||
|
results.push(doc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Complete if cache was complete, or we found fewer than limit
|
||||||
|
const complete = cacheEntry.complete || results.length < RESULT_LIMIT
|
||||||
|
addToCache(query, results, complete)
|
||||||
|
|
||||||
|
if (currentSearchId === searchId) {
|
||||||
|
postResults(results, rawQuery, loc, searchId, true)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slow path: scan all documents
|
||||||
|
const batchSize = 500
|
||||||
|
for (let i = 0; i < recentDocuments.length && results.length < RESULT_LIMIT; i += batchSize) {
|
||||||
|
if (currentSearchId !== searchId) return // Superseded
|
||||||
|
|
||||||
// Process batch
|
// Process batch
|
||||||
const end = Math.min(i + batchSize, recentDocuments.length)
|
const end = Math.min(i + batchSize, recentDocuments.length)
|
||||||
for (let j = i; j < end && results.length < limit; j++) {
|
for (let j = i; j < end && results.length < RESULT_LIMIT; j++) {
|
||||||
const doc = recentDocuments[j]!
|
const doc = recentDocuments[j]!
|
||||||
if (localeIncludes(doc.haystack, needle)) {
|
if (matches(doc.haystack, query, words)) {
|
||||||
results.push(doc)
|
results.push(doc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,85 +155,69 @@ async function performSearch(query: string, loc: string, searchId: number) {
|
|||||||
// Post incremental results if we found new matches
|
// Post incremental results if we found new matches
|
||||||
if (results.length > lastResultCount && currentSearchId === searchId) {
|
if (results.length > lastResultCount && currentSearchId === searchId) {
|
||||||
lastResultCount = results.length
|
lastResultCount = results.length
|
||||||
const sortedResults = sortResults(results, query, loc)
|
postResults(results, rawQuery, loc, searchId, false)
|
||||||
postMessage({
|
|
||||||
type: 'results',
|
|
||||||
docs: sortedResults.map(stripHaystack),
|
|
||||||
id: searchId,
|
|
||||||
done: false
|
|
||||||
} as ResultMessage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield control between batches to allow new search requests to interrupt
|
// Yield control between batches
|
||||||
if (i + batchSize < recentDocuments.length && results.length < limit) {
|
if (i + batchSize < recentDocuments.length && results.length < RESULT_LIMIT) {
|
||||||
await yieldControl()
|
await yieldControl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post final results
|
// Cache and post final results
|
||||||
|
addToCache(query, results, results.length < RESULT_LIMIT)
|
||||||
if (currentSearchId === searchId) {
|
if (currentSearchId === searchId) {
|
||||||
const sortedResults = sortResults(results, query, loc)
|
postResults(results, rawQuery, loc, searchId, true)
|
||||||
postMessage({
|
|
||||||
type: 'results',
|
|
||||||
docs: sortedResults.map(stripHaystack),
|
|
||||||
id: searchId,
|
|
||||||
done: true
|
|
||||||
} as ResultMessage)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Post results to main thread
|
||||||
|
function postResults(docs: WorkerDoc[], query: string, loc: string, id: number, done: boolean) {
|
||||||
|
const sorted = sortResults(docs, query, loc)
|
||||||
|
postMessage({
|
||||||
|
type: 'results',
|
||||||
|
docs: sorted.map(({ haystack, ...rest }) => rest),
|
||||||
|
id,
|
||||||
|
done
|
||||||
|
} as ResultMessage)
|
||||||
|
}
|
||||||
|
|
||||||
// Sort results by relevance
|
// Sort results by relevance
|
||||||
function sortResults(docs: WorkerDoc[], query: string, loc: string): WorkerDoc[] {
|
function sortResults(docs: WorkerDoc[], query: string, loc: string): WorkerDoc[] {
|
||||||
const locsub = loc + '/'
|
const locsub = loc + '/'
|
||||||
return [...docs].sort((a, b) => (
|
return [...docs].sort((a, b) => (
|
||||||
// Current folder first
|
// Current folder first
|
||||||
// @ts-ignore
|
Number(b.loc === loc) - Number(a.loc === loc) ||
|
||||||
(b.loc === loc) - (a.loc === loc) ||
|
|
||||||
// Then subfolders
|
// Then subfolders
|
||||||
// @ts-ignore
|
Number(b.loc.startsWith(locsub)) - Number(a.loc.startsWith(locsub)) ||
|
||||||
(b.loc.slice(0, locsub.length) === locsub) - (a.loc.slice(0, locsub.length) === locsub) ||
|
|
||||||
// Then by location
|
// Then by location
|
||||||
collator.compare(a.loc, b.loc) ||
|
collator.compare(a.loc, b.loc) ||
|
||||||
// Files after folders
|
// Folders before files
|
||||||
// @ts-ignore
|
Number(b.dir) - Number(a.dir) ||
|
||||||
(a.dir === false) - (b.dir === false) ||
|
|
||||||
// Exact name match first
|
// Exact name match first
|
||||||
// @ts-ignore
|
Number(b.name.includes(query)) - Number(a.name.includes(query)) ||
|
||||||
b.name.includes(query) - a.name.includes(query) ||
|
|
||||||
// Finally by name
|
// Finally by name
|
||||||
collator.compare(a.name, b.name)
|
collator.compare(a.name, b.name)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip haystack before sending back to main thread
|
|
||||||
function stripHaystack(doc: WorkerDoc): DocData {
|
|
||||||
const { haystack, ...rest } = doc
|
|
||||||
return rest
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle incoming messages
|
// Handle incoming messages
|
||||||
self.onmessage = async (e: MessageEvent<IncomingMessage>) => {
|
self.onmessage = async (e: MessageEvent<IncomingMessage>) => {
|
||||||
const msg = e.data
|
const msg = e.data
|
||||||
|
|
||||||
if (msg.type === 'update') {
|
if (msg.type === 'update') {
|
||||||
// Update document list with haystacks
|
// Update document list with haystacks, sorted by mtime descending
|
||||||
documents = msg.documents.map(doc => ({
|
recentDocuments = msg.documents
|
||||||
...doc,
|
.map(doc => ({ ...doc, haystack: normalizeHaystack(doc.name) }))
|
||||||
haystack: haystackFormat(doc.name)
|
.sort((a, b) => b.mtime - a.mtime)
|
||||||
}))
|
clearCache()
|
||||||
recentDocuments = sortByRecent(documents)
|
|
||||||
} else if (msg.type === 'search') {
|
} else if (msg.type === 'search') {
|
||||||
currentSearchId = msg.id
|
currentSearchId = msg.id
|
||||||
if (msg.query) {
|
if (msg.query) {
|
||||||
await performSearch(msg.query, msg.loc, msg.id)
|
await performSearch(msg.query, msg.loc, msg.id)
|
||||||
} else {
|
} else {
|
||||||
// Empty query - no results needed (main thread handles folder listing)
|
// Empty query - no results needed
|
||||||
postMessage({
|
postMessage({ type: 'results', docs: [], id: msg.id, done: true } as ResultMessage)
|
||||||
type: 'results',
|
|
||||||
docs: [],
|
|
||||||
id: msg.id,
|
|
||||||
done: true
|
|
||||||
} as ResultMessage)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user