| 32 | } |
| 33 | |
| 34 | private _handle() { |
| 35 | if (this.runQueue.length < this.runSize && this.queue.length > 0) { |
| 36 | const task = this.queue.shift()! |
| 37 | this.runQueue.push(task) |
| 38 | const next = () => { |
| 39 | this.progressFn?.(this._progress) |
| 40 | this.runQueue.splice(this.runQueue.indexOf(task), 1) |
| 41 | this._handle() |
| 42 | } |
| 43 | task |
| 44 | .run() |
| 45 | .then((res) => { |
| 46 | this._progress.finish += 1 |
| 47 | if (res) { |
| 48 | this._progress.success += 1 |
| 49 | this._progress.successTask.push(task) |
| 50 | } |
| 51 | next() |
| 52 | }) |
| 53 | .catch((err) => { |
| 54 | console.log('task catch: ', err) |
| 55 | this._progress.finish += 1 |
| 56 | this._progress.fail += 1 |
| 57 | task.msg = err.toString() |
| 58 | this._progress.failTask.push(task) |
| 59 | next() |
| 60 | }) |
| 61 | } |
| 62 | if (this.queue.length === 0 && this.runQueue.length === 0) { |
| 63 | this.endFn?.() |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | initQueue(queue: Array<TaskItem>) { |
| 68 | this.queue = queue |