修复下载模块在数据写入速度较慢的情况下出现任务及文件异常的问题
parent
12f947ed2e
commit
965a1d03ba
|
@ -27,6 +27,7 @@
|
||||||
- Windows、MacOS平台下的字体列表取消使用原生方式获取以修复某些字体应用后无效的问题(#1596)
|
- Windows、MacOS平台下的字体列表取消使用原生方式获取以修复某些字体应用后无效的问题(#1596)
|
||||||
- 修复亮暗主题自动切换功能无效的问题(#1697)
|
- 修复亮暗主题自动切换功能无效的问题(#1697)
|
||||||
- 修复 MacOS 平台在 Finder 打开文件或目录时应用卡死的问题(#1684)
|
- 修复 MacOS 平台在 Finder 打开文件或目录时应用卡死的问题(#1684)
|
||||||
|
- 修复下载模块在数据写入速度较慢的情况下出现任务及文件异常的问题
|
||||||
|
|
||||||
### 其他
|
### 其他
|
||||||
|
|
||||||
|
|
|
@ -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,6 +250,10 @@ class Task extends EventEmitter {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// console.log('close write stream')
|
// console.log('close write stream')
|
||||||
|
if (this.closeWaiting || this.dataWriteQueueLength) {
|
||||||
|
this.closeWaiting ||= true
|
||||||
|
this.ws.on('close', resolve)
|
||||||
|
} else {
|
||||||
this.ws.close(err => {
|
this.ws.close(err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
this.status = STATUS.error
|
this.status = STATUS.error
|
||||||
|
@ -255,6 +264,7 @@ class Task extends EventEmitter {
|
||||||
this.ws = null
|
this.ws = null
|
||||||
resolve()
|
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--
|
||||||
|
if (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
this.__handleError(err)
|
this.__handleError(err)
|
||||||
void this.stop()
|
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()
|
||||||
if (this.ws) {
|
return this.__closeWriteStream()
|
||||||
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) {
|
||||||
|
|
Loading…
Reference in New Issue