9***@qq.com
9***@qq.com
  • 发布:2023-08-18 10:52
  • 更新:2023-08-18 10:52
  • 阅读:111

【报Bug】downloadTask 调用 abort() 报 Cannot read property 'promise' of undefined

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win11

HBuilderX类型: 正式

HBuilderX版本号: 3.8.11

第三方开发者工具版本号: 1.06.2306020

基础库版本号: 2.30.4

项目创建方式: HBuilderX

操作步骤:

创建实例对象后,在下载过程中触发 abort() 方法

预期结果:

下载任务应触发下载失败回调

实际结果:

报错

bug描述:

export default class DownloadTask {  
    private downloadTask = null  
    _promise = Promise.resolve()  
    progress : number = 0  
    totalBytesWritten : number = 0  
    totalBytesExpectedToWrite : number = 0  
    loading = true //是否下载中  
    success = false//是否下载成功  
    error = false//是否下载失败  

    constructor(option : {  
        url : string,  
        onProgressUpdate ?: (res ?: { progress : number, totalBytesWritten : number, totalBytesExpectedToWrite : number }) => void  
    }) {  
        this._promise = new Promise((resolve, reject) => {  
            this.loading = true  
            this.error = false  
            this.success = false  
            this.downloadTask = uni.downloadFile({  
                url: option.url,  
                success: (res : any) => {  
                    this.loading = false  
                    this.error = false  
                    this.success = true  
                    resolve(res)  
                },  
                fail: (err) => {  
                    this.loading = false  
                    this.success = false  
                    this.error = true  
                    reject(err)  
                },  
                complete: () => { }  
            })  
            this.onProgressUpdate(option.onProgressUpdate)  
            // this.downloadTask.abort()//此处调用abort()正常中断并触发catch  
        })  
    }  
    // 中断下载任务  
    abort() {  
        //通过点击事件调用  
        console.log(this.downloadTask)  
        try {  
            this.downloadTask?.abort()//此处调用abort()会出现异常 Proxy.abort()  
            this.offProgressUpdate()//该取消监听也会触发同样错误  
        } catch (err) {  
            console.log(err)  
        }  
    }  
    // 监听下载进度变化  
    private onProgressUpdate(cb ?: Function) {  
        let t : any = null  
        let _res = { progress: 0, totalBytesWritten: 0, totalBytesExpectedToWrite: 0 }  
        this.downloadTask?.onProgressUpdate((res : { progress : number, totalBytesWritten : number, totalBytesExpectedToWrite : number }) => {  
            _res = res//收集  
            const fn = () => {  
                this.progress = _res.progress  
                this.totalBytesExpectedToWrite = _res.totalBytesExpectedToWrite  
                this.totalBytesWritten = _res.totalBytesWritten  
                cb && cb(_res)  
            }  
            // 节流,防止卡顿  
            if (!t) {  
                t = setTimeout(() => {  
                    fn()  
                    clearTimeout(t)  
                    t = null  
                }, 300)  
            }  
        })  
    }  
    // 停止监听下载进度变化   
    offProgressUpdate() {  
        this.downloadTask?.offProgressUpdate()  
    }  
}
2023-08-18 10:52 负责人:无 分享
已邀请:

要回复问题请先登录注册