Allowed search to recover after a connection failure

This commit is contained in:
Geomitron
2021-01-30 18:15:29 -05:00
parent 3078309d86
commit f2aae10987
3 changed files with 10 additions and 7 deletions

View File

@@ -17,8 +17,8 @@ export class SearchBarComponent implements AfterViewInit {
$('#searchIcon').popup({
onShow: () => this.isError // Only show the popup if there is an error
})
this.searchService.onSearchError(() => {
this.isError = true
this.searchService.onSearchErrorStateUpdate((isError) => {
this.isError = isError
})
}

View File

@@ -10,6 +10,7 @@ export class SearchService {
private resultsChangedEmitter = new EventEmitter<SongResult[]>() // For when any results change
private newResultsEmitter = new EventEmitter<SongResult[]>() // For when a new search happens
private errorStateEmitter = new EventEmitter<boolean>() // To indicate the search's error state
private results: SongResult[] = []
private awaitingResults = false
private currentQuery: SongSearch
@@ -23,9 +24,11 @@ export class SearchService {
this.currentQuery = { query, type: SearchType.Any, offset: 0, length: 50 + 1 } // TODO: make length a setting
try {
this.results = this.trimLastChart(await this.electronService.invoke('song-search', this.currentQuery))
this.errorStateEmitter.emit(false)
} catch (err) {
this.results = []
this.resultsChangedEmitter.error(undefined)
console.log(err.message)
this.errorStateEmitter.emit(true)
}
this.awaitingResults = false
@@ -55,11 +58,11 @@ export class SearchService {
}
/**
* Event emitted when a search fails.
* Event emitted when the error state of the search changes.
* (emitted before `onSearchChanged`)
*/
onSearchError(callback: () => void) {
this.resultsChangedEmitter.subscribe(() => { /** Do nothing */ }, callback)
onSearchErrorStateUpdate(callback: (isError: boolean) => void) {
this.errorStateEmitter.subscribe(callback)
}
get resultCount() {

View File

@@ -21,7 +21,7 @@ class SearchHandler implements IPCInvokeHandler<'song-search'> {
offset: search.offset
}, (err, response) => {
if (err) {
reject(err)
reject(err.message)
} else {
resolve(response.body)
}