Improved performance for large download queues

This commit is contained in:
Geomitron
2021-04-08 18:16:36 -05:00
parent 8a68b7dbbe
commit 592a237cfd
4 changed files with 22 additions and 25 deletions

View File

@@ -11,7 +11,19 @@ export class DownloadService {
private downloads: DownloadProgress[] = []
constructor(private electronService: ElectronService) {
process.setMaxListeners(100)
this.electronService.receiveIPC('download-updated', result => {
// Update <this.downloads> with result
const thisDownloadIndex = this.downloads.findIndex(download => download.versionID == result.versionID)
if (result.type == 'cancel') {
this.downloads = this.downloads.filter(download => download.versionID != result.versionID)
} else if (thisDownloadIndex == -1) {
this.downloads.push(result)
} else {
this.downloads[thisDownloadIndex] = result
}
this.downloadUpdatedEmitter.emit(result)
})
}
get downloadCount() {
@@ -32,19 +44,6 @@ export class DownloadService {
addDownload(versionID: number, newDownload: NewDownload) {
if (!this.downloads.find(download => download.versionID == versionID)) { // Don't download something twice
this.electronService.receiveIPC('download-updated', result => {
// Update <this.downloads> with result
const thisDownloadIndex = this.downloads.findIndex(download => download.versionID == result.versionID)
if (result.type == 'cancel') {
this.downloads = this.downloads.filter(download => download.versionID != versionID)
} else if (thisDownloadIndex == -1) {
this.downloads.push(result)
} else {
this.downloads[thisDownloadIndex] = result
}
this.downloadUpdatedEmitter.emit(result)
})
this.electronService.sendIPC('download', { action: 'add', versionID, data: newDownload })
}
}

View File

@@ -17,7 +17,7 @@ export class SelectionService {
private searchResults: SongResult[] = []
private selectAllChangedEmitter = new EventEmitter<boolean>()
private selectionChangedEmitter = new EventEmitter<SelectionEvent>()
private selectionChangedCallbacks: { [songID: number]: (selection: boolean) => void } = {}
private allSelected = false
private selections: { [songID: number]: boolean | undefined } = {}
@@ -32,7 +32,9 @@ export class SelectionService {
searchService.onNewSearch((results) => {
this.searchResults = results
this.deselectAll()
this.selectionChangedCallbacks = {}
this.selections = {}
this.selectAllChangedEmitter.emit(false)
})
}
@@ -49,11 +51,7 @@ export class SelectionService {
* (note: only one emitter can be registered per `songID`)
*/
onSelectionChanged(songID: number, callback: (selection: boolean) => void) {
this.selectionChangedEmitter.subscribe((selection: SelectionEvent) => {
if (selection.songID == songID) {
callback(selection.selected)
}
})
this.selectionChangedCallbacks[songID] = callback
}
@@ -78,14 +76,14 @@ export class SelectionService {
deselectSong(songID: number) {
if (this.selections[songID]) {
this.selections[songID] = false
this.selectionChangedEmitter.emit({ songID, selected: false })
this.selectionChangedCallbacks[songID](false)
}
}
selectSong(songID: number) {
if (!this.selections[songID]) {
this.selections[songID] = true
this.selectionChangedEmitter.emit({ songID, selected: true })
this.selectionChangedCallbacks[songID](true)
}
}
}

View File

@@ -19,7 +19,7 @@ class DownloadHandler implements IPCEmitHandler<'download'> {
}
private addDownload(data: Download) {
const filesHash = data.data.driveData.filesHash
const filesHash = data.data.driveData.filesHash // Note: using versionID would cause chart packs to download multiple times
if (this.currentDownload?.hash == filesHash || this.downloadQueue.isDownloadingLink(filesHash)) {
return
}

View File

@@ -4,7 +4,7 @@ import { emitIPCEvent } from '../../main'
export class DownloadQueue {
downloadQueue: ChartDownload[] = []
private downloadQueue: ChartDownload[] = []
isDownloadingLink(filesHash: string) {
return this.downloadQueue.some(download => download.hash == filesHash)