File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -13,19 +13,27 @@ let grouped = [] as [string, DisplayResult[]][]
1313let queryInFlight = ''
1414let searching = true
1515let 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
1720function 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
2528childPort . 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[]>) {
6577let version = 114514
6678function 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
You can’t perform that action at this time.
0 commit comments