/
dep-builder.ts
76 lines (70 loc) · 2.08 KB
/
dep-builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Dependency, TaskDep } from './task'
import { Is } from './utils'
import { TaskContext } from './task-manager'
export class DepBuilder<O = any> {
private _dep: TaskDep<O>
readonly _isDepBuilder = true
constructor(name: Dependency) {
if (Is.str(name)) {
name = { name }
} else if (name instanceof DepBuilder) {
name = name.toTaskDep()
}
this._dep = name
}
/**
*
* Dependences are executed serially by default.
* If order doesn't matter and you want better performance via parallel, you can mark it as asynchronized.
* Asynchronized will run immediately whether there are synchronized tasks before them or not.
* You can pass a number as the priority of asynchronized tasks, bigger is formmer.
* @param priority
*/
async(priority: number | boolean = true) {
this._dep.async = priority
return this
}
force() {
this._dep.force = true
return this
}
options(opts: O | ((ctx: TaskContext) => O)) {
if (Is.fn(opts)) {
this._dep.resolveOptions = opts
} else {
this._dep.options = opts
}
return this
}
toTaskDep() {
return this._dep
}
}
export function dep(dep: Dependency) {
if (Is.str(dep)) {
dep = { name: dep }
}
return new DepBuilder(dep)
}
declare global {
interface String {
/**
* Dependences are executed serially by default.
* If order doesn't matter and you want better performance via parallel, you can mark it as asynchronized.
* Asynchronized will run immediately whether there are synchronized tasks before them or not.
* You can pass a number as the priority of asynchronized tasks, bigger is formmer.
*/
async(priority?: number | boolean): DepBuilder
force(): DepBuilder
options<O>(opts: O | NonNullable<TaskDep['resolveOptions']>): DepBuilder
}
}
String.prototype.async = function (priority?: number | boolean) {
return dep(this as string).async(priority)
}
String.prototype.force = function () {
return dep(this as string).force()
}
String.prototype.options = function (opts) {
return dep(this as string).options(opts)
}