Possibly improved memory usage

This commit is contained in:
Geomitron
2021-04-10 12:42:51 -05:00
parent d88962a07e
commit 36803a5d0c
3 changed files with 16 additions and 14 deletions

View File

@@ -30,7 +30,6 @@ export class ChartDownload {
private files: DriveFile[]
private percent = 0 // Needs to be stored here because errors won't know the exact percent
private tempPath: string
private dropFastUpdate = false
private wasCanceled = false
private readonly individualFileProgressPortion: number
@@ -100,14 +99,6 @@ export class ChartDownload {
*/
private updateGUI(header: string, description: string, type: ProgressType, isLink = false) {
if (this.wasCanceled) { return }
if (type == 'fastUpdate') {
if (this.dropFastUpdate) {
return
} else {
this.dropFastUpdate = true
setTimeout(() => this.dropFastUpdate = false, 30)
}
}
emitIPCEvent('download-updated', {
versionID: this.versionID,
@@ -222,7 +213,7 @@ export class ChartDownload {
const size = Number(this.files[fileIndex].size)
fileProgress = interpolate(bytesDownloaded, 0, size, downloadStartPoint, this.individualFileProgressPortion)
this.percent = this._allFilesProgress + fileProgress
this.updateGUI(downloadHeader, `Downloading... (${Math.round(1000 * bytesDownloaded / size) / 10}%)`, 'fastUpdate')
this.updateGUI(downloadHeader, `Downloading... (${Math.round(1000 * bytesDownloaded / size) / 10}%)`, 'good')
})
downloader.on('error', this.handleError.bind(this))
@@ -249,7 +240,7 @@ export class ChartDownload {
extractor.on('extractProgress', (percent, filecount) => {
this.percent = interpolate(percent, 0, 100, 80, 95)
this.updateGUI(`[${archive}] (${filecount} file${filecount == 1 ? '' : 's'} extracted)`, `Extracting... (${percent}%)`, 'fastUpdate')
this.updateGUI(`[${archive}] (${filecount} file${filecount == 1 ? '' : 's'} extracted)`, `Extracting... (${percent}%)`, 'good')
})
extractor.on('error', this.handleError.bind(this))

View File

@@ -134,22 +134,33 @@ class APIFileDownloader {
private handleDownloadResponse() {
this.callbacks.downloadProgress(0)
let downloadedSize = 0
const writeStream = createWriteStream(this.fullPath)
try {
this.downloadStream.pipe(createWriteStream(this.fullPath))
this.downloadStream.pipe(writeStream)
} catch (err) {
this.failDownload(downloadErrors.connectionError(err))
}
this.downloadStream.on('data', this.cancelable((chunk: Buffer) => {
downloadedSize += chunk.length
this.callbacks.downloadProgress(downloadedSize)
}))
const progressUpdater = setInterval(() => {
this.callbacks.downloadProgress(downloadedSize)
}, 100)
this.downloadStream.on('error', this.cancelable((err: Error) => {
clearInterval(progressUpdater)
this.failDownload(downloadErrors.connectionError(err))
}))
this.downloadStream.on('end', this.cancelable(() => {
clearInterval(progressUpdater)
writeStream.end()
this.downloadStream.destroy()
this.downloadStream = null
this.callbacks.complete()
}))
}

View File

@@ -32,4 +32,4 @@ export interface DownloadProgress {
isLink: boolean
}
export type ProgressType = 'good' | 'error' | 'cancel' | 'done' | 'fastUpdate'
export type ProgressType = 'good' | 'error' | 'cancel' | 'done'