@@ -14,6 +14,13 @@ function debounce(wait, fn) {
1414 }
1515}
1616
17+ function concat ( fn1 , fn2 ) {
18+ return function ( ...args ) {
19+ fn1 . apply ( this , args )
20+ fn2 . apply ( this , args )
21+ }
22+ }
23+
1724const appSelector = '#guides-app'
1825const appElement = document . querySelector ( appSelector ) ;
1926
@@ -44,13 +51,21 @@ const app = createApp({
4451 } ,
4552 watch : {
4653 '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+ )
5469 } ,
5570 'search.input.categories' : {
5671 handler ( newValue , oldValue ) {
@@ -117,8 +132,11 @@ const app = createApp({
117132 new URL ( link . href ) . pathname ,
118133 {
119134 url : url ,
120- className : element . className ,
121135 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 ) ,
122140 summary : element . querySelector ( 'div .description' ) . innerHTML ,
123141 keywords : element . querySelector ( 'div .keywords' ) . innerHTML ,
124142 categories : element . querySelector ( 'div .categories' ) . innerHTML ,
@@ -131,7 +149,6 @@ const app = createApp({
131149 url ,
132150 {
133151 url : url ,
134- className : element . className ,
135152 title : element . querySelector ( 'p.title' ) . innerHTML ,
136153 summary : element . querySelector ( 'div.description' ) . innerHTML ,
137154 keywords : element . querySelector ( 'div.keywords' ) . innerHTML ,
@@ -161,34 +178,36 @@ const app = createApp({
161178 } )
162179 } ,
163180 methods : {
164- async resetAndSearch ( ) {
181+ async resetAndMarkLoading ( ) {
165182 if ( this . loading ) {
166183 this . loading . abort ( )
167184 }
168- this . loading = new AbortController ( ) ;
185+ this . loading = new AbortController ( )
169186 this . _resetResults ( )
170- await this . _searchBatch ( this . initialTimeout )
187+ } ,
188+ async resetAndSearch ( ) {
189+ this . resetAndMarkLoading ( )
190+ await this . _searchBatch ( this . loading , this . initialTimeout )
171191 } ,
172192 async searchMore ( ) {
173193 if ( this . loading ) {
174194 return // Already searching
175195 }
176196 this . loading = new AbortController ( ) ;
177197 this . search . page = this . search . page + 1
178- await this . _searchBatch ( this . moreTimeout )
198+ await this . _searchBatch ( this . loading , this . moreTimeout )
179199 } ,
180200 _resetResults ( ) {
181201 this . search . page = 0
182202 this . search . result . hits = [ ]
183203 this . search . result . hasMoreHits = false
184204 } ,
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 ) {
191206 try {
207+ if ( ! this . hasInput ) {
208+ // No input => no search
209+ return
210+ }
192211 if ( this . hasInputWithTooFewChars ) {
193212 throw 'Too few characters'
194213 }
0 commit comments