@leafer/task 1.0.0-beta.4 → 1.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/TaskProcessor.ts +44 -56
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/task",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.5",
|
|
4
4
|
"description": "@leafer/task",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"leaferjs"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@leafer/math": "1.0.0-beta.
|
|
23
|
-
"@leafer/debug": "1.0.0-beta.
|
|
22
|
+
"@leafer/math": "1.0.0-beta.5",
|
|
23
|
+
"@leafer/debug": "1.0.0-beta.5"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@leafer/interface": "1.0.0-beta.
|
|
26
|
+
"@leafer/interface": "1.0.0-beta.5"
|
|
27
27
|
}
|
|
28
28
|
}
|
package/src/TaskProcessor.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { IFunction } from '@leafer/interface'
|
|
1
|
+
import { IFunction, ITaskProcessor, ITaskProcessorConfig } from '@leafer/interface'
|
|
2
|
+
import { DataHelper } from '@leafer/data'
|
|
2
3
|
import { Debug } from '@leafer/debug'
|
|
3
4
|
|
|
4
5
|
import { TaskItem } from './TaskItem'
|
|
@@ -6,78 +7,64 @@ import { TaskItem } from './TaskItem'
|
|
|
6
7
|
|
|
7
8
|
const debug = Debug.get('TaskProcessor')
|
|
8
9
|
|
|
10
|
+
export class TaskProcessor implements ITaskProcessor {
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
onComplete?: IFunction
|
|
12
|
-
onTask?: IFunction
|
|
13
|
-
onError?: IFunction
|
|
14
|
-
parallel?: number
|
|
15
|
-
}
|
|
12
|
+
public config: ITaskProcessorConfig = { parallel: 6 }
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
export class TaskProcessor {
|
|
19
|
-
|
|
20
|
-
private parallel = 6
|
|
21
|
-
private params: ITaskProcessorParams = {}
|
|
22
|
-
|
|
23
|
-
// 需要初始化的动态数据
|
|
24
14
|
private list: Array<TaskItem> = []
|
|
25
|
-
private index = 0
|
|
26
15
|
|
|
27
16
|
private parallelList: Array<TaskItem>
|
|
28
17
|
private parallelSuccessNumber: number
|
|
29
18
|
|
|
19
|
+
public get isComplete(): boolean { return this._isComplete }
|
|
30
20
|
private _isComplete: boolean
|
|
31
|
-
public get isComplete(): boolean {
|
|
32
|
-
return this._isComplete
|
|
33
|
-
}
|
|
34
21
|
|
|
22
|
+
public get running(): boolean { return this._running }
|
|
35
23
|
private _running: boolean
|
|
36
|
-
public get running(): boolean {
|
|
37
|
-
return this._running
|
|
38
|
-
}
|
|
39
24
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (params.parallel) this.parallel = params.parallel
|
|
44
|
-
}
|
|
45
|
-
this.init()
|
|
46
|
-
}
|
|
25
|
+
public get percent(): number {
|
|
26
|
+
const { total } = this
|
|
27
|
+
let totalTime = 0, runTime = 0
|
|
47
28
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let totalTime = 0
|
|
51
|
-
let runTime = 0
|
|
52
|
-
for (let i = 0; i < len; i++) {
|
|
53
|
-
if (i <= this.index) {
|
|
29
|
+
for (let i = 0; i < total; i++) {
|
|
30
|
+
if (i <= this.finishedIndex) {
|
|
54
31
|
runTime += this.list[i].taskTime
|
|
55
|
-
if (i === this.
|
|
32
|
+
if (i === this.finishedIndex) totalTime = runTime
|
|
56
33
|
} else {
|
|
57
34
|
totalTime += this.list[i].taskTime
|
|
58
35
|
}
|
|
59
36
|
}
|
|
60
37
|
|
|
61
|
-
|
|
62
|
-
if (Number.isNaN(percent)) percent = 0
|
|
63
|
-
return percent
|
|
64
|
-
|
|
38
|
+
return this._isComplete ? 1 : (runTime / totalTime)
|
|
65
39
|
}
|
|
66
40
|
|
|
67
|
-
get total(): number {
|
|
41
|
+
public get total(): number {
|
|
68
42
|
return this.list.length
|
|
69
43
|
}
|
|
70
44
|
|
|
71
|
-
|
|
72
|
-
|
|
45
|
+
public index = 0
|
|
46
|
+
|
|
47
|
+
public get finishedIndex(): number {
|
|
48
|
+
return this._isComplete ? 0 : this.index + this.parallelSuccessNumber
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public get remain(): number {
|
|
52
|
+
return this._isComplete ? 0 : this.total - this.finishedIndex
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
constructor(config?: ITaskProcessorConfig) {
|
|
57
|
+
if (config) DataHelper.assign(this.config, config)
|
|
58
|
+
this.init()
|
|
73
59
|
}
|
|
74
60
|
|
|
61
|
+
|
|
75
62
|
protected init(): void {
|
|
76
63
|
this.empty()
|
|
77
64
|
this.index = 0
|
|
78
65
|
this.parallelSuccessNumber = 0
|
|
79
66
|
this._running = false
|
|
80
|
-
this._isComplete =
|
|
67
|
+
this._isComplete = true
|
|
81
68
|
}
|
|
82
69
|
|
|
83
70
|
protected empty(): void {
|
|
@@ -85,8 +72,6 @@ export class TaskProcessor {
|
|
|
85
72
|
this.parallelList = []
|
|
86
73
|
}
|
|
87
74
|
|
|
88
|
-
|
|
89
|
-
|
|
90
75
|
public start(): void {
|
|
91
76
|
this._running = true
|
|
92
77
|
this._isComplete = false
|
|
@@ -132,8 +117,6 @@ export class TaskProcessor {
|
|
|
132
117
|
this.push(new TaskItem(callback))
|
|
133
118
|
}
|
|
134
119
|
|
|
135
|
-
|
|
136
|
-
|
|
137
120
|
private push(task: TaskItem, taskTime?: number): void {
|
|
138
121
|
if (taskTime) task.taskTime = taskTime
|
|
139
122
|
task.parent = this
|
|
@@ -151,7 +134,7 @@ export class TaskProcessor {
|
|
|
151
134
|
|
|
152
135
|
} else {
|
|
153
136
|
|
|
154
|
-
this.runTask()
|
|
137
|
+
this.remain ? this.runTask() : this.onComplete()
|
|
155
138
|
|
|
156
139
|
}
|
|
157
140
|
}
|
|
@@ -187,7 +170,7 @@ export class TaskProcessor {
|
|
|
187
170
|
|
|
188
171
|
this.parallelList = []
|
|
189
172
|
this.parallelSuccessNumber = 0
|
|
190
|
-
let end = this.index + this.parallel
|
|
173
|
+
let end = this.index + this.config.parallel
|
|
191
174
|
|
|
192
175
|
if (end > this.list.length) end = this.list.length
|
|
193
176
|
|
|
@@ -213,13 +196,13 @@ export class TaskProcessor {
|
|
|
213
196
|
|
|
214
197
|
// 找到下一个可以并行的任务
|
|
215
198
|
const parallelWaitNumber = parallelList.length
|
|
216
|
-
const nextIndex = this.
|
|
199
|
+
const nextIndex = this.finishedIndex + parallelWaitNumber
|
|
217
200
|
|
|
218
201
|
if (parallelList.length) {
|
|
219
202
|
|
|
220
203
|
if (!this._running) return
|
|
221
204
|
|
|
222
|
-
if (nextIndex < this.
|
|
205
|
+
if (nextIndex < this.total) {
|
|
223
206
|
|
|
224
207
|
task = this.list[nextIndex]
|
|
225
208
|
|
|
@@ -253,15 +236,15 @@ export class TaskProcessor {
|
|
|
253
236
|
}
|
|
254
237
|
|
|
255
238
|
private onComplete(): void {
|
|
256
|
-
this.stop()
|
|
257
239
|
this._isComplete = true
|
|
258
|
-
|
|
240
|
+
this.stop()
|
|
241
|
+
if (this.config.onComplete) this.config.onComplete()
|
|
259
242
|
}
|
|
260
243
|
|
|
261
244
|
private onTask(task: TaskItem): void {
|
|
262
245
|
task.complete()
|
|
263
|
-
if (this.
|
|
264
|
-
if (this.
|
|
246
|
+
if (this.config.onTask) this.config.onTask()
|
|
247
|
+
if (this.finishedIndex + 1 === this.total) this.onComplete()
|
|
265
248
|
}
|
|
266
249
|
|
|
267
250
|
private onParallelError(error: unknown): void {
|
|
@@ -279,6 +262,11 @@ export class TaskProcessor {
|
|
|
279
262
|
|
|
280
263
|
private onError(error: unknown): void {
|
|
281
264
|
this.pause()
|
|
282
|
-
if (this.
|
|
265
|
+
if (this.config.onError) this.config.onError(error)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
public destory(): void {
|
|
269
|
+
this.empty()
|
|
270
|
+
this.config = {}
|
|
283
271
|
}
|
|
284
272
|
}
|