forked from wasdk/WebAssemblyStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpy.ts
141 lines (130 loc) · 4.09 KB
/
gulpy.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2018 Mozilla Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type PromiseMaker = () => Promise<any>;
class Task {
dependencies: Task [];
promiseMaker: PromiseMaker;
constructor(dependencies: Task [], promiseMaker: PromiseMaker) {
this.dependencies = dependencies;
this.promiseMaker = promiseMaker;
}
}
class TaskInstance {
task: Task;
promise: Promise<any>;
constructor(task: Task) {
this.task = task;
this.promise = null;
}
makePromise(): Promise<any> {
if (this.promise) {
return this.promise;
}
return this.promise = this.task.promiseMaker();
}
}
class GulpySession {
private gulpy: Gulpy;
private tasks: Map<Task, TaskInstance> = new Map();
constructor(gulpy: Gulpy) {
this.gulpy = gulpy;
}
ensureInstance(task: Task): TaskInstance {
let instance = this.tasks.get(task);
if (instance) {
return instance;
}
instance = new TaskInstance(task);
this.tasks.set(task, instance);
return instance;
}
async runInstance(instance: TaskInstance): Promise<any> {
const dependencies = instance.task.dependencies.map(x => this.ensureInstance(x));
await Promise.all(dependencies.map(x => this.runInstance(x)));
return instance.makePromise();
}
async run(task: Task): Promise<any> {
return this.runInstance(this.ensureInstance(task));
}
}
export class Gulpy {
private tasks: { [name: string]: Task } = {};
private session: GulpySession;
task(name: string, fn: PromiseMaker): void;
task(name: string, dependencies: string[], fn?: PromiseMaker): void;
task(name: string, a: string [] | PromiseMaker, b?: PromiseMaker): void {
let dependencies: string [] = [];
let fn: PromiseMaker = null;
if (arguments.length === 3) {
dependencies = a as string[];
fn = b;
} else if (arguments.length === 2) {
if (Array.isArray(a)) {
dependencies = a as string[];
fn = b || (() => {/**/}) as PromiseMaker;
} else {
fn = a as PromiseMaker;
}
}
this.tasks[name] = new Task(dependencies.map(x => this.tasks[x]), fn);
}
series(tasks: string[]): PromiseMaker {
return null;
}
parallel(tasks: string[]): PromiseMaker {
return null;
}
hasTask(name: string) {
return name in this.tasks;
}
async run(name: string) {
const session = new GulpySession(this);
await session.run(this.tasks[name]);
}
}
export function testGulpy() {
const gulp = new Gulpy();
gulp.task("b", () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Running Task B " + performance.now());
resolve();
}, 50);
});
});
gulp.task("c", [], () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Running Task C " + performance.now());
resolve();
}, 100);
});
});
gulp.task("a", ["b", "c"], () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Running Task A " + performance.now());
resolve();
}, 200);
});
});
gulp.run("a");
}