Skip to content

Commit 857ed14

Browse files
fix: fix several UI bugs
1 parent 0e494d4 commit 857ed14

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/webview/hooks/useSearch.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@ let grouped = [] as [string, DisplayResult[]][]
1313
let queryInFlight = ''
1414
let searching = true
1515
let notify = () => {}
16+
// we will not immediately drop previous result
17+
// instead, use a stale flag and update it on streaming or end
18+
let hasStaleResult = false
1619

1720
function postSearch(inputValue: string) {
1821
id = (id + 1) % MOD
1922
childPort.postMessage('search', { id, inputValue })
2023
searching = true
21-
grouped = []
24+
hasStaleResult = true
2225
notify()
2326
}
2427

2528
childPort.onMessage('searchResultStreaming', event => {
2629
if (event.id !== id) {
2730
return
2831
}
32+
if (hasStaleResult) {
33+
// empty previous result
34+
hasStaleResult = false
35+
grouped = []
36+
}
2937
queryInFlight = event.inputValue
3038
grouped = merge(groupBy(event.searchResult))
3139
notify()
@@ -36,6 +44,10 @@ childPort.onMessage('searchEnd', event => {
3644
return
3745
}
3846
searching = false
47+
if (hasStaleResult) {
48+
grouped = []
49+
}
50+
hasStaleResult = false
3951
queryInFlight = event.inputValue
4052
notify()
4153
})
@@ -65,8 +77,9 @@ function merge(newEntries: Map<string, DisplayResult[]>) {
6577
let version = 114514
6678
function subscribe(onChange: () => void): () => void {
6779
notify = () => {
68-
onChange()
80+
// snapshot should precede onChange
6981
version = (version + 1) % MOD
82+
onChange()
7083
}
7184
return () => {
7285
// TODO: cleanup is not correct

0 commit comments

Comments
 (0)