修复下载模块在数据写入速度较慢的情况下出现任务及文件异常的问题

pull/1761/head
lyswhut 2023-12-13 01:19:17 +08:00
parent 12f947ed2e
commit 965a1d03ba
2 changed files with 31 additions and 30 deletions

View File

@ -27,6 +27,7 @@
- Windows、MacOS平台下的字体列表取消使用原生方式获取以修复某些字体应用后无效的问题#1596 - Windows、MacOS平台下的字体列表取消使用原生方式获取以修复某些字体应用后无效的问题#1596
- 修复亮暗主题自动切换功能无效的问题(#1697 - 修复亮暗主题自动切换功能无效的问题(#1697
- 修复 MacOS 平台在 Finder 打开文件或目录时应用卡死的问题(#1684 - 修复 MacOS 平台在 Finder 打开文件或目录时应用卡死的问题(#1684
- 修复下载模块在数据写入速度较慢的情况下出现任务及文件异常的问题
### 其他 ### 其他

View File

@ -39,6 +39,8 @@ class Task extends EventEmitter {
requestInstance: http.ClientRequest | null = null requestInstance: http.ClientRequest | null = null
maxRedirectNum = 2 maxRedirectNum = 2
private redirectNum = 0 private redirectNum = 0
private dataWriteQueueLength = 0
private closeWaiting = false
constructor(url: string, savePath: string, filename: string, options: Partial<Options> = {}) { constructor(url: string, savePath: string, filename: string, options: Partial<Options> = {}) {
@ -65,6 +67,8 @@ class Task extends EventEmitter {
this.progress.downloaded = 0 this.progress.downloaded = 0
this.progress.progress = 0 this.progress.progress = 0
this.progress.speed = 0 this.progress.speed = 0
this.dataWriteQueueLength = 0
this.closeWaiting = false
if (startByte) this.requestOptions.headers!.range = `bytes=${startByte}-${endByte}` if (startByte) this.requestOptions.headers!.range = `bytes=${startByte}-${endByte}`
if (!path) return if (!path) return
@ -202,6 +206,7 @@ class Task extends EventEmitter {
this.ws = fs.createWriteStream(this.chunkInfo.path, options) this.ws = fs.createWriteStream(this.chunkInfo.path, options)
this.ws.on('finish', () => { this.ws.on('finish', () => {
if (this.closeWaiting) return
void this.__closeWriteStream() void this.__closeWriteStream()
}) })
this.ws.on('error', err => { this.ws.on('error', err => {
@ -245,16 +250,21 @@ class Task extends EventEmitter {
return return
} }
// console.log('close write stream') // console.log('close write stream')
this.ws.close(err => { if (this.closeWaiting || this.dataWriteQueueLength) {
if (err) { this.closeWaiting ||= true
this.status = STATUS.error this.ws.on('close', resolve)
this.emit('error', err) } else {
reject(err) this.ws.close(err => {
return if (err) {
} this.status = STATUS.error
this.ws = null this.emit('error', err)
resolve() reject(err)
}) return
}
this.ws = null
resolve()
})
}
}) })
} }
@ -293,12 +303,16 @@ class Task extends EventEmitter {
console.log('cancel write') console.log('cancel write')
return return
} }
this.dataWriteQueueLength++
this.__calculateProgress(chunk.length) this.__calculateProgress(chunk.length)
this.ws.write(chunk, err => { this.ws.write(chunk, err => {
if (!err) return this.dataWriteQueueLength--
console.log(err) if (err) {
this.__handleError(err) console.log(err)
void this.stop() this.__handleError(err)
void this.stop()
}
if (this.closeWaiting && !this.dataWriteQueueLength) this.ws?.close()
}) })
} }
@ -322,22 +336,8 @@ class Task extends EventEmitter {
} }
async __handleStop() { async __handleStop() {
return new Promise<void>((resolve, reject) => { this.__closeRequest()
this.__closeRequest() return this.__closeWriteStream()
if (this.ws) {
this.ws.close(err => {
if (err) {
reject(err)
this.emit('error', err)
return
}
this.ws = null
resolve()
})
} else {
resolve()
}
})
} }
__calculateProgress(receivedBytes: number) { __calculateProgress(receivedBytes: number) {