@leafer/task
Advanced tools
+4
-4
| { | ||
| "name": "@leafer/task", | ||
| "version": "1.0.0-beta.4", | ||
| "version": "1.0.0-beta.5", | ||
| "description": "@leafer/task", | ||
@@ -22,8 +22,8 @@ "author": "Chao (Leafer) Wan", | ||
| "dependencies": { | ||
| "@leafer/math": "1.0.0-beta.4", | ||
| "@leafer/debug": "1.0.0-beta.4" | ||
| "@leafer/math": "1.0.0-beta.5", | ||
| "@leafer/debug": "1.0.0-beta.5" | ||
| }, | ||
| "devDependencies": { | ||
| "@leafer/interface": "1.0.0-beta.4" | ||
| "@leafer/interface": "1.0.0-beta.5" | ||
| } | ||
| } |
+44
-56
@@ -1,2 +0,3 @@ | ||
| import { IFunction } from '@leafer/interface' | ||
| import { IFunction, ITaskProcessor, ITaskProcessorConfig } from '@leafer/interface' | ||
| import { DataHelper } from '@leafer/data' | ||
| import { Debug } from '@leafer/debug' | ||
@@ -9,19 +10,7 @@ | ||
| export class TaskProcessor implements ITaskProcessor { | ||
| export interface ITaskProcessorParams { | ||
| onComplete?: IFunction | ||
| onTask?: IFunction | ||
| onError?: IFunction | ||
| parallel?: number | ||
| } | ||
| public config: ITaskProcessorConfig = { parallel: 6 } | ||
| export class TaskProcessor { | ||
| private parallel = 6 | ||
| private params: ITaskProcessorParams = {} | ||
| // 需要初始化的动态数据 | ||
| private list: Array<TaskItem> = [] | ||
| private index = 0 | ||
@@ -31,28 +20,16 @@ private parallelList: Array<TaskItem> | ||
| public get isComplete(): boolean { return this._isComplete } | ||
| private _isComplete: boolean | ||
| public get isComplete(): boolean { | ||
| return this._isComplete | ||
| } | ||
| public get running(): boolean { return this._running } | ||
| private _running: boolean | ||
| public get running(): boolean { | ||
| return this._running | ||
| } | ||
| constructor(params?: ITaskProcessorParams) { | ||
| if (params) { | ||
| this.params = params | ||
| if (params.parallel) this.parallel = params.parallel | ||
| } | ||
| this.init() | ||
| } | ||
| public get percent(): number { | ||
| const { total } = this | ||
| let totalTime = 0, runTime = 0 | ||
| get percent(): number { | ||
| const len = this.list.length | ||
| let totalTime = 0 | ||
| let runTime = 0 | ||
| for (let i = 0; i < len; i++) { | ||
| if (i <= this.index) { | ||
| for (let i = 0; i < total; i++) { | ||
| if (i <= this.finishedIndex) { | ||
| runTime += this.list[i].taskTime | ||
| if (i === this.index) totalTime = runTime | ||
| if (i === this.finishedIndex) totalTime = runTime | ||
| } else { | ||
@@ -63,16 +40,26 @@ totalTime += this.list[i].taskTime | ||
| let percent = this._isComplete ? 1 : (runTime / totalTime) | ||
| if (Number.isNaN(percent)) percent = 0 | ||
| return percent | ||
| return this._isComplete ? 1 : (runTime / totalTime) | ||
| } | ||
| get total(): number { | ||
| public get total(): number { | ||
| return this.list.length | ||
| } | ||
| get runIndex(): number { | ||
| return this.index | ||
| public index = 0 | ||
| public get finishedIndex(): number { | ||
| return this._isComplete ? 0 : this.index + this.parallelSuccessNumber | ||
| } | ||
| public get remain(): number { | ||
| return this._isComplete ? 0 : this.total - this.finishedIndex | ||
| } | ||
| constructor(config?: ITaskProcessorConfig) { | ||
| if (config) DataHelper.assign(this.config, config) | ||
| this.init() | ||
| } | ||
| protected init(): void { | ||
@@ -83,3 +70,3 @@ this.empty() | ||
| this._running = false | ||
| this._isComplete = false | ||
| this._isComplete = true | ||
| } | ||
@@ -92,4 +79,2 @@ | ||
| public start(): void { | ||
@@ -140,4 +125,2 @@ this._running = true | ||
| private push(task: TaskItem, taskTime?: number): void { | ||
@@ -160,3 +143,3 @@ if (taskTime) task.taskTime = taskTime | ||
| this.runTask() | ||
| this.remain ? this.runTask() : this.onComplete() | ||
@@ -197,3 +180,3 @@ } | ||
| this.parallelSuccessNumber = 0 | ||
| let end = this.index + this.parallel | ||
| let end = this.index + this.config.parallel | ||
@@ -224,3 +207,3 @@ if (end > this.list.length) end = this.list.length | ||
| const parallelWaitNumber = parallelList.length | ||
| const nextIndex = this.index + this.parallelSuccessNumber + parallelWaitNumber | ||
| const nextIndex = this.finishedIndex + parallelWaitNumber | ||
@@ -231,3 +214,3 @@ if (parallelList.length) { | ||
| if (nextIndex < this.list.length) { | ||
| if (nextIndex < this.total) { | ||
@@ -266,5 +249,5 @@ task = this.list[nextIndex] | ||
| private onComplete(): void { | ||
| this._isComplete = true | ||
| this.stop() | ||
| this._isComplete = true | ||
| if (this.params.onComplete) this.params.onComplete() | ||
| if (this.config.onComplete) this.config.onComplete() | ||
| } | ||
@@ -274,4 +257,4 @@ | ||
| task.complete() | ||
| if (this.params.onTask) this.params.onTask() | ||
| if (this.index === this.list.length - 1) this.onComplete() | ||
| if (this.config.onTask) this.config.onTask() | ||
| if (this.finishedIndex + 1 === this.total) this.onComplete() | ||
| } | ||
@@ -294,4 +277,9 @@ | ||
| this.pause() | ||
| if (this.params.onError) this.params.onError(error) | ||
| if (this.config.onError) this.config.onError(error) | ||
| } | ||
| public destory(): void { | ||
| this.empty() | ||
| this.config = {} | ||
| } | ||
| } |
8993
0.3%223
-4.29%+ Added
+ Added
- Removed
- Removed
Updated
Updated