@leafer/task 1.9.12 → 1.10.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/task",
3
- "version": "1.9.12",
3
+ "version": "1.10.0",
4
4
  "description": "@leafer/task",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,10 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/math": "1.9.12",
26
- "@leafer/debug": "1.9.12"
25
+ "@leafer/math": "1.10.0",
26
+ "@leafer/debug": "1.10.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.9.12"
29
+ "@leafer/interface": "1.10.0"
30
30
  }
31
31
  }
package/src/TaskItem.ts CHANGED
@@ -18,6 +18,8 @@ export class TaskItem implements ITaskItem {
18
18
  public isComplete: boolean
19
19
  public isCancel: boolean
20
20
 
21
+ public canUse?: IFunction
22
+
21
23
  private task: IFunction
22
24
 
23
25
  constructor(task?: IFunction) {
@@ -27,7 +29,9 @@ export class TaskItem implements ITaskItem {
27
29
 
28
30
  async run(): Promise<void> {
29
31
  try {
30
- if (this.task && !this.isComplete && this.parent.running) await this.task()
32
+ if (this.isComplete) return
33
+ if (this.canUse && !this.canUse()) return this.cancel()
34
+ if (this.task && this.parent.running) await this.task()
31
35
  } catch (error) {
32
36
  debug.error(error)
33
37
  }
@@ -35,8 +39,7 @@ export class TaskItem implements ITaskItem {
35
39
 
36
40
  public complete(): void {
37
41
  this.isComplete = true
38
- this.parent = null
39
- this.task = null
42
+ this.parent = this.task = this.canUse = null
40
43
  }
41
44
 
42
45
  public cancel(): void {
@@ -56,7 +56,7 @@ export class TaskProcessor implements ITaskProcessor {
56
56
 
57
57
  // list
58
58
 
59
- public add(taskCallback: IFunction, options?: ITaskOptions | number): ITaskItem {
59
+ public add(taskCallback: IFunction, options?: ITaskOptions | number, canUse?: IFunction): ITaskItem {
60
60
  let start: boolean, parallel: boolean, time: number, delay: number
61
61
 
62
62
  const task = new TaskItem(taskCallback)
@@ -69,10 +69,12 @@ export class TaskProcessor implements ITaskProcessor {
69
69
  start = options.start
70
70
  time = options.time
71
71
  delay = options.delay
72
+ if (!canUse) canUse = options.canUse
72
73
  }
73
74
 
74
75
  if (time) task.time = time
75
76
  if (parallel === false) task.parallel = false
77
+ if (canUse) task.canUse = canUse
76
78
 
77
79
  if (isUndefined(delay)) {
78
80
  this.push(task, start)
@@ -163,18 +165,12 @@ export class TaskProcessor implements ITaskProcessor {
163
165
  return
164
166
  }
165
167
 
166
- if (task.isCancel) {
167
- this.index++
168
- this.runTask()
169
- return
170
- }
171
-
172
168
  task.run().then(() => {
173
169
 
174
170
  this.onTask(task)
175
171
 
176
172
  this.index++
177
- this.nextTask()
173
+ task.isCancel ? this.runTask() : this.nextTask()
178
174
 
179
175
  }).catch(error => {
180
176
  this.onError(error)
package/types/index.d.ts CHANGED
@@ -15,7 +15,7 @@ declare class TaskProcessor implements ITaskProcessor {
15
15
  get remain(): number;
16
16
  get percent(): number;
17
17
  constructor(config?: ITaskProcessorConfig);
18
- add(taskCallback: IFunction, options?: ITaskOptions | number): ITaskItem;
18
+ add(taskCallback: IFunction, options?: ITaskOptions | number, canUse?: IFunction): ITaskItem;
19
19
  protected push(task: ITaskItem, start?: boolean): void;
20
20
  protected empty(): void;
21
21
  start(): void;
@@ -44,6 +44,7 @@ declare class TaskItem implements ITaskItem {
44
44
  time: number;
45
45
  isComplete: boolean;
46
46
  isCancel: boolean;
47
+ canUse?: IFunction;
47
48
  private task;
48
49
  constructor(task?: IFunction);
49
50
  run(): Promise<void>;