@leafer/task 1.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +26 -0
- package/src/TaskItem.ts +37 -0
- package/src/TaskProcessor.ts +284 -0
- package/src/index.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present, Chao (Leafer) Wan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @leafer/task
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leafer/task",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "@leafer/task",
|
|
5
|
+
"author": "Chao (Leafer) Wan",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "src/index.ts",
|
|
8
|
+
"files": ["src"],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/leaferjs/leafer.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/leaferjs/leafer/tree/main/packages/task",
|
|
14
|
+
"bugs": "https://github.com/leaferjs/leafer/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"leafer",
|
|
17
|
+
"leaferjs"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@leafer/math": "1.0.0-alpha.1",
|
|
21
|
+
"@leafer/debug": "1.0.0-alpha.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@leafer/interface": "1.0.0-alpha.1"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/TaskItem.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { IFunction } from '@leafer/interface'
|
|
2
|
+
import { IncrementId } from '@leafer/math'
|
|
3
|
+
|
|
4
|
+
import { TaskProcessor } from './TaskProcessor'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class TaskItem {
|
|
8
|
+
|
|
9
|
+
readonly id: number
|
|
10
|
+
|
|
11
|
+
public parent: TaskProcessor
|
|
12
|
+
|
|
13
|
+
public parallel: boolean
|
|
14
|
+
public isComplete: boolean
|
|
15
|
+
|
|
16
|
+
private task: IFunction
|
|
17
|
+
|
|
18
|
+
public taskTime = 1 // 预估任务需要运行的时间, 毫秒为单位
|
|
19
|
+
|
|
20
|
+
constructor(task?: IFunction) {
|
|
21
|
+
this.id = IncrementId.create(IncrementId.TASK)
|
|
22
|
+
this.task = task
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async run(): Promise<void> {
|
|
26
|
+
if (this.task && !this.isComplete && this.parent.running) {
|
|
27
|
+
await this.task()
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public complete(): void {
|
|
32
|
+
this.isComplete = true
|
|
33
|
+
this.parent = undefined
|
|
34
|
+
this.task = undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { IFunction } from '@leafer/interface'
|
|
2
|
+
import { Debug } from '@leafer/debug'
|
|
3
|
+
|
|
4
|
+
import { TaskItem } from './TaskItem'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const debug = Debug.get('TaskProcessor')
|
|
8
|
+
|
|
9
|
+
|
|
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
|
+
private list: Array<TaskItem> = []
|
|
25
|
+
private index = 0
|
|
26
|
+
|
|
27
|
+
private parallelList: Array<TaskItem>
|
|
28
|
+
private parallelSuccessNumber: number
|
|
29
|
+
|
|
30
|
+
private _isComplete: boolean
|
|
31
|
+
public get isComplete(): boolean {
|
|
32
|
+
return this._isComplete
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private _running: boolean
|
|
36
|
+
public get running(): boolean {
|
|
37
|
+
return this._running
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
constructor(params?: ITaskProcessorParams) {
|
|
41
|
+
if (params) {
|
|
42
|
+
this.params = params
|
|
43
|
+
if (params.parallel) this.parallel = params.parallel
|
|
44
|
+
}
|
|
45
|
+
this.init()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get percent(): number {
|
|
49
|
+
const len = this.list.length
|
|
50
|
+
let totalTime = 0
|
|
51
|
+
let runTime = 0
|
|
52
|
+
for (let i = 0; i < len; i++) {
|
|
53
|
+
if (i <= this.index) {
|
|
54
|
+
runTime += this.list[i].taskTime
|
|
55
|
+
if (i === this.index) totalTime = runTime
|
|
56
|
+
} else {
|
|
57
|
+
totalTime += this.list[i].taskTime
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let percent = this._isComplete ? 1 : (runTime / totalTime)
|
|
62
|
+
if (Number.isNaN(percent)) percent = 0
|
|
63
|
+
return percent
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get total(): number {
|
|
68
|
+
return this.list.length
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get runIndex(): number {
|
|
72
|
+
return this.index
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected init(): void {
|
|
76
|
+
this.empty()
|
|
77
|
+
this.index = 0
|
|
78
|
+
this.parallelSuccessNumber = 0
|
|
79
|
+
this._running = false
|
|
80
|
+
this._isComplete = false
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
protected empty(): void {
|
|
84
|
+
this.list = []
|
|
85
|
+
this.parallelList = []
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
public start(): void {
|
|
91
|
+
this._running = true
|
|
92
|
+
this._isComplete = false
|
|
93
|
+
this.run()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public pause(): void {
|
|
97
|
+
this._running = false
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public resume(): void {
|
|
101
|
+
this._running = true
|
|
102
|
+
this._isComplete = false
|
|
103
|
+
this.run()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public skip(): void {
|
|
107
|
+
this.index++
|
|
108
|
+
this.resume()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public stop(): void {
|
|
112
|
+
this._running = false
|
|
113
|
+
this.list.forEach(item => {
|
|
114
|
+
item.complete()
|
|
115
|
+
})
|
|
116
|
+
this.empty()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
public add(taskCallback: IFunction, taskTime?: number): void {
|
|
122
|
+
this.push(new TaskItem(taskCallback), taskTime)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public addParallel(taskCallback: IFunction, taskTime?: number): void {
|
|
126
|
+
const task = new TaskItem(taskCallback)
|
|
127
|
+
task.parallel = true
|
|
128
|
+
this.push(task, taskTime)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public addEmpty(callback?: IFunction): void {
|
|
132
|
+
this.push(new TaskItem(callback))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
private push(task: TaskItem, taskTime?: number): void {
|
|
138
|
+
if (taskTime) task.taskTime = taskTime
|
|
139
|
+
task.parent = this
|
|
140
|
+
this.list.push(task)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private run(): void {
|
|
144
|
+
if (!this._running) return
|
|
145
|
+
|
|
146
|
+
this.setParallelList()
|
|
147
|
+
|
|
148
|
+
if (this.parallelList.length > 1) {
|
|
149
|
+
|
|
150
|
+
this.runParallelTask()
|
|
151
|
+
|
|
152
|
+
} else {
|
|
153
|
+
|
|
154
|
+
this.runTask()
|
|
155
|
+
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
protected runTask(): void {
|
|
160
|
+
const task = this.list[this.index]
|
|
161
|
+
task.run().then(() => {
|
|
162
|
+
|
|
163
|
+
this.onTask(task)
|
|
164
|
+
|
|
165
|
+
this.index++
|
|
166
|
+
this.nextTask()
|
|
167
|
+
|
|
168
|
+
}).catch(error => {
|
|
169
|
+
this.onError(error)
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
protected runParallelTask(): void {
|
|
174
|
+
this.parallelList.forEach(task => {
|
|
175
|
+
task.run().then(() => {
|
|
176
|
+
|
|
177
|
+
this.onTask(task)
|
|
178
|
+
this.fillParallelTask()
|
|
179
|
+
}).catch(error => {
|
|
180
|
+
this.onParallelError(error)
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
protected setParallelList(): void {
|
|
186
|
+
let task: TaskItem
|
|
187
|
+
|
|
188
|
+
this.parallelList = []
|
|
189
|
+
this.parallelSuccessNumber = 0
|
|
190
|
+
let end = this.index + this.parallel
|
|
191
|
+
|
|
192
|
+
if (end > this.list.length) end = this.list.length
|
|
193
|
+
|
|
194
|
+
for (let i = this.index; i < end; i++) {
|
|
195
|
+
task = this.list[i]
|
|
196
|
+
if (task.parallel) {
|
|
197
|
+
this.parallelList.push(task)
|
|
198
|
+
} else {
|
|
199
|
+
break
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
protected fillParallelTask(): void {
|
|
206
|
+
|
|
207
|
+
let task: TaskItem
|
|
208
|
+
const parallelList = this.parallelList
|
|
209
|
+
|
|
210
|
+
// 完成一个任务
|
|
211
|
+
this.parallelSuccessNumber++
|
|
212
|
+
parallelList.pop()
|
|
213
|
+
|
|
214
|
+
// 找到下一个可以并行的任务
|
|
215
|
+
const parallelWaitNumber = parallelList.length
|
|
216
|
+
const nextIndex = this.index + this.parallelSuccessNumber + parallelWaitNumber
|
|
217
|
+
|
|
218
|
+
if (parallelList.length) {
|
|
219
|
+
|
|
220
|
+
if (!this._running) return
|
|
221
|
+
|
|
222
|
+
if (nextIndex < this.list.length) {
|
|
223
|
+
|
|
224
|
+
task = this.list[nextIndex]
|
|
225
|
+
|
|
226
|
+
if (task.parallel) {
|
|
227
|
+
|
|
228
|
+
parallelList.push(task)
|
|
229
|
+
|
|
230
|
+
task.run().then(() => {
|
|
231
|
+
|
|
232
|
+
this.onTask(task)
|
|
233
|
+
this.fillParallelTask()
|
|
234
|
+
}).catch(error => {
|
|
235
|
+
this.onParallelError(error)
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
} else {
|
|
242
|
+
|
|
243
|
+
this.index += this.parallelSuccessNumber
|
|
244
|
+
this.nextTask()
|
|
245
|
+
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private nextTask(): void {
|
|
250
|
+
setTimeout(() => {
|
|
251
|
+
this.run()
|
|
252
|
+
}, 0)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private onComplete(): void {
|
|
256
|
+
this.stop()
|
|
257
|
+
this._isComplete = true
|
|
258
|
+
if (this.params.onComplete) this.params.onComplete()
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private onTask(task: TaskItem): void {
|
|
262
|
+
task.complete()
|
|
263
|
+
if (this.params.onTask) this.params.onTask()
|
|
264
|
+
if (this.index === this.list.length - 1) this.onComplete()
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private onParallelError(error: unknown): void {
|
|
268
|
+
debug.error('ParallelError')
|
|
269
|
+
|
|
270
|
+
// 并行变串行, 以便下次重试
|
|
271
|
+
this.parallelList.forEach(task => {
|
|
272
|
+
task.parallel = false
|
|
273
|
+
})
|
|
274
|
+
this.parallelList.length = 0
|
|
275
|
+
this.parallelSuccessNumber = 0
|
|
276
|
+
|
|
277
|
+
this.onError(error)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private onError(error: unknown): void {
|
|
281
|
+
this.pause()
|
|
282
|
+
if (this.params.onError) this.params.onError(error)
|
|
283
|
+
}
|
|
284
|
+
}
|
package/src/index.ts
ADDED