import { Injectable, EventEmitter } from '@angular/core' import { ElectronService } from './electron.service' import { NewDownload, DownloadProgress } from '../../../electron/shared/interfaces/download.interface' import * as _ from 'underscore' @Injectable({ providedIn: 'root' }) export class DownloadService { private downloadUpdatedEmitter = new EventEmitter() private downloads: DownloadProgress[] = [] constructor(private electronService: ElectronService) { } get downloadCount() { return this.downloads.length } get totalPercent() { let total = 0 for (const download of this.downloads) { total += download.percent } return total / this.downloads.length } addDownload(versionID: number, newDownload: NewDownload) { if (this.downloads.findIndex(download => download.versionID == versionID) != -1) { return } // Don't download something twice this.electronService.receiveIPC('download-updated', result => { this.downloadUpdatedEmitter.emit(result) // Update with result const thisDownloadIndex = this.downloads.findIndex(download => download.versionID == result.versionID) if (thisDownloadIndex == -1) { this.downloads.push(result) } else { this.downloads[thisDownloadIndex] = result } }) this.electronService.sendIPC('download', { action: 'add', versionID, data: newDownload }) } onDownloadUpdated(callback: (download: DownloadProgress) => void) { const debouncedCallback = _.throttle(callback, 30) this.downloadUpdatedEmitter.subscribe((download: DownloadProgress) => { if (this.downloads.findIndex(oldDownload => oldDownload.versionID == download.versionID) == -1) { // If this is a new download item, don't call debouncedCallback; it may miss adding new versions to the list callback(download) } else if (download.type == 'wait') { callback(download) // Many wait events can be recieved at once } else { debouncedCallback(download) } }) } cancelDownload(versionID: number) { const removedDownload = this.downloads.find(download => download.versionID == versionID) this.downloads = this.downloads.filter(download => download.versionID != versionID) removedDownload.type = 'cancel' this.downloadUpdatedEmitter.emit(removedDownload) this.electronService.sendIPC('download', { action: 'cancel', versionID }) } retryDownload(versionID: number) { this.electronService.sendIPC('download', { action: 'retry', versionID }) } continueDownload(versionID: number) { this.electronService.sendIPC('download', { action: 'continue', versionID }) } }