@@ -14,6 +14,13 @@ function debounce(wait, fn) {
14
14
}
15
15
}
16
16
17
+ function concat ( fn1 , fn2 ) {
18
+ return function ( ...args ) {
19
+ fn1 . apply ( this , args )
20
+ fn2 . apply ( this , args )
21
+ }
22
+ }
23
+
17
24
const appSelector = '#guides-app'
18
25
const appElement = document . querySelector ( appSelector ) ;
19
26
@@ -44,13 +51,21 @@ const app = createApp({
44
51
} ,
45
52
watch : {
46
53
'search.input.q' : {
47
- // "debounce" makes sure we only run search ~300ms after the user is done typing the text
48
- // WARNING: we really do want to debounce here, NOT in setters,
49
- // because debouncing in setters leads to data in input forms being refreshed after the timeout,
50
- // causing problems when typing text relatively fast.
51
- handler : debounce ( 300 , function ( newValue , oldValue ) {
52
- this . resetAndSearch ( )
53
- } )
54
+ handler : concat (
55
+ // Without debouncing, we want to cancel the previous search and mark the view as "loading",
56
+ // so that we don't mistakenly display "no results" while debouncing the initial search.
57
+ // See https://github.com/quarkusio/search.quarkus.io/issues/200
58
+ function ( newValue , oldValue ) {
59
+ this . resetAndMarkLoading ( )
60
+ } ,
61
+ // "debounce" makes sure we only run search ~300ms after the user is done typing the text
62
+ // WARNING: we really do want to debounce here, NOT in setters,
63
+ // because debouncing in setters leads to data in input forms being refreshed after the timeout,
64
+ // causing problems when typing text relatively fast.
65
+ debounce ( 300 , function ( newValue , oldValue ) {
66
+ this . resetAndSearch ( )
67
+ } )
68
+ )
54
69
} ,
55
70
'search.input.categories' : {
56
71
handler ( newValue , oldValue ) {
@@ -117,8 +132,11 @@ const app = createApp({
117
132
new URL ( link . href ) . pathname ,
118
133
{
119
134
url : url ,
120
- className : element . className ,
121
135
title : link . innerHTML ,
136
+ type : [ ...element . classList ]
137
+ . filter ( clazz => clazz . endsWith ( "bkg" ) )
138
+ . map ( clazz => clazz . substring ( 0 , clazz . length - "bkg" . length ) )
139
+ . at ( 0 ) ,
122
140
summary : element . querySelector ( 'div .description' ) . innerHTML ,
123
141
keywords : element . querySelector ( 'div .keywords' ) . innerHTML ,
124
142
categories : element . querySelector ( 'div .categories' ) . innerHTML ,
@@ -131,7 +149,6 @@ const app = createApp({
131
149
url ,
132
150
{
133
151
url : url ,
134
- className : element . className ,
135
152
title : element . querySelector ( 'p.title' ) . innerHTML ,
136
153
summary : element . querySelector ( 'div.description' ) . innerHTML ,
137
154
keywords : element . querySelector ( 'div.keywords' ) . innerHTML ,
@@ -161,34 +178,36 @@ const app = createApp({
161
178
} )
162
179
} ,
163
180
methods : {
164
- async resetAndSearch ( ) {
181
+ async resetAndMarkLoading ( ) {
165
182
if ( this . loading ) {
166
183
this . loading . abort ( )
167
184
}
168
- this . loading = new AbortController ( ) ;
185
+ this . loading = new AbortController ( )
169
186
this . _resetResults ( )
170
- await this . _searchBatch ( this . initialTimeout )
187
+ } ,
188
+ async resetAndSearch ( ) {
189
+ this . resetAndMarkLoading ( )
190
+ await this . _searchBatch ( this . loading , this . initialTimeout )
171
191
} ,
172
192
async searchMore ( ) {
173
193
if ( this . loading ) {
174
194
return // Already searching
175
195
}
176
196
this . loading = new AbortController ( ) ;
177
197
this . search . page = this . search . page + 1
178
- await this . _searchBatch ( this . moreTimeout )
198
+ await this . _searchBatch ( this . loading , this . moreTimeout )
179
199
} ,
180
200
_resetResults ( ) {
181
201
this . search . page = 0
182
202
this . search . result . hits = [ ]
183
203
this . search . result . hasMoreHits = false
184
204
} ,
185
- async _searchBatch ( timeout ) {
186
- if ( ! this . hasInput ) {
187
- // No input => no search
188
- return
189
- }
190
- const controller = this . loading
205
+ async _searchBatch ( controller , timeout ) {
191
206
try {
207
+ if ( ! this . hasInput ) {
208
+ // No input => no search
209
+ return
210
+ }
192
211
if ( this . hasInputWithTooFewChars ) {
193
212
throw 'Too few characters'
194
213
}
0 commit comments