@leafer/task 1.0.0-bate → 1.0.0-beta.10
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/TaskItem.ts +6 -2
- package/src/TaskProcessor.ts +60 -70
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/task",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
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-
|
|
23
|
-
"@leafer/debug": "1.0.0-
|
|
22
|
+
"@leafer/math": "1.0.0-beta.10",
|
|
23
|
+
"@leafer/debug": "1.0.0-beta.10"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@leafer/interface": "1.0.0-
|
|
26
|
+
"@leafer/interface": "1.0.0-beta.10"
|
|
27
27
|
}
|
|
28
28
|
}
|
package/src/TaskItem.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { IFunction } from '@leafer/interface'
|
|
2
2
|
import { IncrementId } from '@leafer/math'
|
|
3
|
+
import { Debug } from '@leafer/debug'
|
|
3
4
|
|
|
4
5
|
import { TaskProcessor } from './TaskProcessor'
|
|
5
6
|
|
|
6
7
|
|
|
8
|
+
const debug = Debug.get('TaskProcessor')
|
|
7
9
|
export class TaskItem {
|
|
8
10
|
|
|
9
11
|
readonly id: number
|
|
@@ -23,8 +25,10 @@ export class TaskItem {
|
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
async run(): Promise<void> {
|
|
26
|
-
|
|
27
|
-
await this.task()
|
|
28
|
+
try {
|
|
29
|
+
if (this.task && !this.isComplete && this.parent.running) await this.task()
|
|
30
|
+
} catch (error) {
|
|
31
|
+
debug.error(error)
|
|
28
32
|
}
|
|
29
33
|
}
|
|
30
34
|
|
package/src/TaskProcessor.ts
CHANGED
|
@@ -1,92 +1,74 @@
|
|
|
1
|
-
import { IFunction } from '@leafer/interface'
|
|
2
|
-
import {
|
|
1
|
+
import { IFunction, ITaskProcessor, ITaskProcessorConfig } from '@leafer/interface'
|
|
2
|
+
import { DataHelper } from '@leafer/data'
|
|
3
3
|
|
|
4
4
|
import { TaskItem } from './TaskItem'
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
export class TaskProcessor implements ITaskProcessor {
|
|
8
8
|
|
|
9
|
+
public config: ITaskProcessorConfig = { parallel: 6 }
|
|
9
10
|
|
|
10
|
-
export interface ITaskProcessorParams {
|
|
11
|
-
onComplete?: IFunction
|
|
12
|
-
onTask?: IFunction
|
|
13
|
-
onError?: IFunction
|
|
14
|
-
parallel?: number
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export class TaskProcessor {
|
|
19
|
-
|
|
20
|
-
private parallel = 6
|
|
21
|
-
private params: ITaskProcessorParams = {}
|
|
22
|
-
|
|
23
|
-
// 需要初始化的动态数据
|
|
24
11
|
private list: Array<TaskItem> = []
|
|
25
|
-
private index = 0
|
|
26
12
|
|
|
27
13
|
private parallelList: Array<TaskItem>
|
|
28
14
|
private parallelSuccessNumber: number
|
|
29
15
|
|
|
16
|
+
public get isComplete(): boolean { return this._isComplete }
|
|
30
17
|
private _isComplete: boolean
|
|
31
|
-
public get isComplete(): boolean {
|
|
32
|
-
return this._isComplete
|
|
33
|
-
}
|
|
34
18
|
|
|
19
|
+
public get running(): boolean { return this._running }
|
|
35
20
|
private _running: boolean
|
|
36
|
-
|
|
37
|
-
return this._running
|
|
38
|
-
}
|
|
21
|
+
private _timer: any
|
|
39
22
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (params.parallel) this.parallel = params.parallel
|
|
44
|
-
}
|
|
45
|
-
this.init()
|
|
46
|
-
}
|
|
23
|
+
public get percent(): number {
|
|
24
|
+
const { total } = this
|
|
25
|
+
let totalTime = 0, runTime = 0
|
|
47
26
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let totalTime = 0
|
|
51
|
-
let runTime = 0
|
|
52
|
-
for (let i = 0; i < len; i++) {
|
|
53
|
-
if (i <= this.index) {
|
|
27
|
+
for (let i = 0; i < total; i++) {
|
|
28
|
+
if (i <= this.finishedIndex) {
|
|
54
29
|
runTime += this.list[i].taskTime
|
|
55
|
-
if (i === this.
|
|
30
|
+
if (i === this.finishedIndex) totalTime = runTime
|
|
56
31
|
} else {
|
|
57
32
|
totalTime += this.list[i].taskTime
|
|
58
33
|
}
|
|
59
34
|
}
|
|
60
35
|
|
|
61
|
-
|
|
62
|
-
if (Number.isNaN(percent)) percent = 0
|
|
63
|
-
return percent
|
|
64
|
-
|
|
36
|
+
return this._isComplete ? 1 : (runTime / totalTime)
|
|
65
37
|
}
|
|
66
38
|
|
|
67
|
-
get total(): number {
|
|
39
|
+
public get total(): number {
|
|
68
40
|
return this.list.length
|
|
69
41
|
}
|
|
70
42
|
|
|
71
|
-
|
|
72
|
-
|
|
43
|
+
public index = 0
|
|
44
|
+
|
|
45
|
+
public get finishedIndex(): number {
|
|
46
|
+
return this._isComplete ? 0 : this.index + this.parallelSuccessNumber
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public get remain(): number {
|
|
50
|
+
return this._isComplete ? this.total : this.total - this.finishedIndex
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
constructor(config?: ITaskProcessorConfig) {
|
|
55
|
+
if (config) DataHelper.assign(this.config, config)
|
|
56
|
+
this.init()
|
|
73
57
|
}
|
|
74
58
|
|
|
75
59
|
protected init(): void {
|
|
76
60
|
this.empty()
|
|
77
|
-
this.index = 0
|
|
78
|
-
this.parallelSuccessNumber = 0
|
|
79
61
|
this._running = false
|
|
80
|
-
this._isComplete =
|
|
62
|
+
this._isComplete = true
|
|
81
63
|
}
|
|
82
64
|
|
|
83
65
|
protected empty(): void {
|
|
66
|
+
this.index = 0
|
|
67
|
+
this.parallelSuccessNumber = 0
|
|
84
68
|
this.list = []
|
|
85
69
|
this.parallelList = []
|
|
86
70
|
}
|
|
87
71
|
|
|
88
|
-
|
|
89
|
-
|
|
90
72
|
public start(): void {
|
|
91
73
|
this._running = true
|
|
92
74
|
this._isComplete = false
|
|
@@ -94,6 +76,7 @@ export class TaskProcessor {
|
|
|
94
76
|
}
|
|
95
77
|
|
|
96
78
|
public pause(): void {
|
|
79
|
+
clearTimeout(this._timer)
|
|
97
80
|
this._running = false
|
|
98
81
|
}
|
|
99
82
|
|
|
@@ -109,7 +92,9 @@ export class TaskProcessor {
|
|
|
109
92
|
}
|
|
110
93
|
|
|
111
94
|
public stop(): void {
|
|
95
|
+
clearTimeout(this._timer)
|
|
112
96
|
this._running = false
|
|
97
|
+
this._isComplete = true
|
|
113
98
|
this.list.forEach(item => {
|
|
114
99
|
item.complete()
|
|
115
100
|
})
|
|
@@ -118,26 +103,25 @@ export class TaskProcessor {
|
|
|
118
103
|
|
|
119
104
|
|
|
120
105
|
|
|
121
|
-
public add(taskCallback: IFunction, taskTime?: number): void {
|
|
122
|
-
this.push(new TaskItem(taskCallback), taskTime)
|
|
106
|
+
public add(taskCallback: IFunction, taskTime?: number, start?: boolean,): void {
|
|
107
|
+
this.push(new TaskItem(taskCallback), taskTime, start)
|
|
123
108
|
}
|
|
124
109
|
|
|
125
|
-
public addParallel(taskCallback: IFunction, taskTime?: number): void {
|
|
110
|
+
public addParallel(taskCallback: IFunction, taskTime?: number, start?: boolean): void {
|
|
126
111
|
const task = new TaskItem(taskCallback)
|
|
127
112
|
task.parallel = true
|
|
128
|
-
this.push(task, taskTime)
|
|
113
|
+
this.push(task, taskTime, start)
|
|
129
114
|
}
|
|
130
115
|
|
|
131
116
|
public addEmpty(callback?: IFunction): void {
|
|
132
117
|
this.push(new TaskItem(callback))
|
|
133
118
|
}
|
|
134
119
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
private push(task: TaskItem, taskTime?: number): void {
|
|
120
|
+
private push(task: TaskItem, taskTime?: number, start?: boolean): void {
|
|
138
121
|
if (taskTime) task.taskTime = taskTime
|
|
139
122
|
task.parent = this
|
|
140
123
|
this.list.push(task)
|
|
124
|
+
if (start && !this.running) this.start()
|
|
141
125
|
}
|
|
142
126
|
|
|
143
127
|
private run(): void {
|
|
@@ -151,7 +135,7 @@ export class TaskProcessor {
|
|
|
151
135
|
|
|
152
136
|
} else {
|
|
153
137
|
|
|
154
|
-
this.runTask()
|
|
138
|
+
this.remain ? this.runTask() : this.onComplete()
|
|
155
139
|
|
|
156
140
|
}
|
|
157
141
|
}
|
|
@@ -187,7 +171,7 @@ export class TaskProcessor {
|
|
|
187
171
|
|
|
188
172
|
this.parallelList = []
|
|
189
173
|
this.parallelSuccessNumber = 0
|
|
190
|
-
let end = this.index + this.parallel
|
|
174
|
+
let end = this.index + this.config.parallel
|
|
191
175
|
|
|
192
176
|
if (end > this.list.length) end = this.list.length
|
|
193
177
|
|
|
@@ -213,13 +197,13 @@ export class TaskProcessor {
|
|
|
213
197
|
|
|
214
198
|
// 找到下一个可以并行的任务
|
|
215
199
|
const parallelWaitNumber = parallelList.length
|
|
216
|
-
const nextIndex = this.
|
|
200
|
+
const nextIndex = this.finishedIndex + parallelWaitNumber
|
|
217
201
|
|
|
218
202
|
if (parallelList.length) {
|
|
219
203
|
|
|
220
204
|
if (!this._running) return
|
|
221
205
|
|
|
222
|
-
if (nextIndex < this.
|
|
206
|
+
if (nextIndex < this.total) {
|
|
223
207
|
|
|
224
208
|
task = this.list[nextIndex]
|
|
225
209
|
|
|
@@ -236,36 +220,37 @@ export class TaskProcessor {
|
|
|
236
220
|
})
|
|
237
221
|
|
|
238
222
|
}
|
|
223
|
+
|
|
239
224
|
}
|
|
240
225
|
|
|
241
226
|
} else {
|
|
242
227
|
|
|
243
228
|
this.index += this.parallelSuccessNumber
|
|
229
|
+
this.parallelSuccessNumber = 0
|
|
244
230
|
this.nextTask()
|
|
245
231
|
|
|
246
232
|
}
|
|
247
233
|
}
|
|
248
234
|
|
|
249
235
|
private nextTask(): void {
|
|
250
|
-
|
|
251
|
-
this.
|
|
252
|
-
}
|
|
236
|
+
if (this.total === this.finishedIndex) {
|
|
237
|
+
this.onComplete()
|
|
238
|
+
} else {
|
|
239
|
+
this._timer = setTimeout(() => this.run(), 0)
|
|
240
|
+
}
|
|
253
241
|
}
|
|
254
242
|
|
|
255
243
|
private onComplete(): void {
|
|
256
244
|
this.stop()
|
|
257
|
-
this.
|
|
258
|
-
if (this.params.onComplete) this.params.onComplete()
|
|
245
|
+
if (this.config.onComplete) this.config.onComplete()
|
|
259
246
|
}
|
|
260
247
|
|
|
261
248
|
private onTask(task: TaskItem): void {
|
|
262
249
|
task.complete()
|
|
263
|
-
if (this.
|
|
264
|
-
if (this.index === this.list.length - 1) this.onComplete()
|
|
250
|
+
if (this.config.onTask) this.config.onTask()
|
|
265
251
|
}
|
|
266
252
|
|
|
267
253
|
private onParallelError(error: unknown): void {
|
|
268
|
-
debug.error('ParallelError')
|
|
269
254
|
|
|
270
255
|
// 并行变串行, 以便下次重试
|
|
271
256
|
this.parallelList.forEach(task => {
|
|
@@ -279,6 +264,11 @@ export class TaskProcessor {
|
|
|
279
264
|
|
|
280
265
|
private onError(error: unknown): void {
|
|
281
266
|
this.pause()
|
|
282
|
-
if (this.
|
|
267
|
+
if (this.config.onError) this.config.onError(error)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
public destroy(): void {
|
|
271
|
+
this.empty()
|
|
272
|
+
this.config = {}
|
|
283
273
|
}
|
|
284
274
|
}
|