@@ -133,6 +133,10 @@ import HeaderMenu from '../components/HeaderMenu'
133133import SearchResult from ' ../components/UnifiedSearch/SearchResult'
134134import SearchResultPlaceholders from ' ../components/UnifiedSearch/SearchResultPlaceholders'
135135
136+ const REQUEST_FAILED = 0
137+ const REQUEST_OK = 1
138+ const REQUEST_CANCELED = 2
139+
136140export default {
137141 name: ' UnifiedSearch' ,
138142
@@ -325,22 +329,25 @@ export default {
325329 this .resetState ()
326330 this .focusInput ()
327331 },
328- resetState () {
332+ async resetState () {
329333 this .cursors = {}
330334 this .limits = {}
331- this .loading = {}
332335 this .reached = {}
333336 this .results = {}
334337 this .focused = null
335- this .cancelPendingRequests ()
338+ await this .cancelPendingRequests ()
336339 },
337340
338341 /**
339342 * Cancel any ongoing searches
340343 */
341- cancelPendingRequests () {
344+ async cancelPendingRequests () {
345+ // Cloning so we can keep processing other requests
346+ const requests = this .requests .slice (0 )
347+ this .requests = []
348+
342349 // Cancel all pending requests
343- this . requests .forEach (cancel => cancel ())
350+ await Promise . all ( requests .map (cancel => cancel () ))
344351 },
345352
346353 /**
@@ -394,19 +401,18 @@ export default {
394401 // Remove any filters from the query
395402 query = query .replace (regexFilterIn, ' ' ).replace (regexFilterNot, ' ' )
396403
397- this .logger .debug (` Searching ${ query} in` , types)
398-
399404 // Reset search if the query changed
400- this .resetState ()
405+ await this .resetState ()
406+ this .$set (this .loading , ' all' , true )
407+ this .logger .debug (` Searching ${ query} in` , types)
401408
402409 Promise .all (types .map (async type => {
403410 try {
404411 // Init cancellable request
405412 const { request , cancel } = search ({ type, query })
406413 this .requests .push (cancel)
407414
408- // Mark this type as loading and fetch results
409- this .$set (this .loading , type, true )
415+ // Fetch results
410416 const { data } = await request ()
411417
412418 // Process results
@@ -434,18 +440,24 @@ export default {
434440 if (this .focused === null ) {
435441 this .focused = 0
436442 }
443+ return REQUEST_OK
437444 } catch (error) {
445+ this .$delete (this .results , type)
446+
438447 // If this is not a cancelled throw
439448 if (error .response && error .response .status ) {
440449 this .logger .error (` Error searching for ${ this .typesMap [type]} ` , error)
441450 showError (this .t (' core' , ' An error occurred while searching for {type}' , { type: this .typesMap [type] }))
451+ return REQUEST_FAILED
442452 }
443-
444- this .$delete (this .results , type)
445- } finally {
446- this .$set (this .loading , type, false )
453+ return REQUEST_CANCELED
454+ }
455+ })).then (results => {
456+ // Do not declare loading finished if the request have been cancelled
457+ // This means another search was triggered and we're therefore still loading
458+ if (results .some (result => result === REQUEST_CANCELED )) {
459+ return
447460 }
448- })).then (() => {
449461 // We finished all searches
450462 this .loading = {}
451463 })
@@ -463,22 +475,27 @@ export default {
463475 if (this .loading [type]) {
464476 return
465477 }
466- this .$set (this .loading , type, true )
467478
468479 if (this .cursors [type]) {
469- const request = await search ({ type, query: this .query , cursor: this .cursors [type] })
480+ // Init cancellable request
481+ const { request , cancel } = search ({ type, query: this .query , cursor: this .cursors [type] })
482+ this .requests .push (cancel)
483+
484+ // Fetch results
485+ const { data } = await request ()
470486
471487 // Save cursor if any
472- if (request . data .ocs .data .cursor ) {
473- this .$set (this .cursors , type, request . data .ocs .data .cursor )
488+ if (data .ocs .data .cursor ) {
489+ this .$set (this .cursors , type, data .ocs .data .cursor )
474490 }
475491
476- if (request .data .ocs .data .entries .length > 0 ) {
477- this .results [type].push (... request .data .ocs .data .entries )
492+ // Process results
493+ if (data .ocs .data .entries .length > 0 ) {
494+ this .results [type].push (... data .ocs .data .entries )
478495 }
479496
480497 // Check if we reached end of pagination
481- if (request . data .ocs .data .entries .length < this .defaultLimit ) {
498+ if (data .ocs .data .entries .length < this .defaultLimit ) {
482499 this .$set (this .reached , type, true )
483500 }
484501 } else
@@ -500,8 +517,6 @@ export default {
500517 this .focusIndex (this .focused )
501518 })
502519 }
503-
504- this .$set (this .loading , type, false )
505520 },
506521
507522 /**
0 commit comments