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({ $('#searchIcon').popup({
onShow: () => this.isError // Only show the popup if there is an error onShow: () => this.isError // Only show the popup if there is an error
}) })
this.searchService.onSearchError(() => { this.searchService.onSearchErrorStateUpdate((isError) => {
this.isError = true this.isError = isError
}) })
} }

View File

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

View File

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