-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.ts
48 lines (41 loc) · 1.08 KB
/
parallel.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
import { Handler, HandlerGetter, Res, Status } from "./handler.ts";
import { Pipe } from "./pipe.ts";
import { HandlerBuilderGetter } from "./handler.ts";
export class Parallel implements Handler {
pipes: Pipe[];
constructor() {
this.pipes = new Array<Pipe>();
}
/** Handle the res, run pipes concrrently */
async handle(res: Res): Promise<Res> {
let reses = await Promise.all(
this.pipes.map((pipe) => {
return pipe.handle(res);
}),
);
return {
status: Status.Ok,
data: reses.map((res) => res.data),
};
}
}
export function buildParallel(
conf: any[],
builders?: HandlerBuilderGetter,
handlers?: HandlerGetter,
): Parallel {
let parallel = new Parallel();
for (let pipeConf of conf) {
const pipe = new Pipe(pipeConf, builders, handlers);
parallel.pipes.push(pipe);
}
return parallel;
}
export function buildParallelWithJson(
jsonConf: string,
builders?: HandlerBuilderGetter,
handlers?: HandlerGetter,
): Parallel {
const conf = JSON.parse(jsonConf);
return buildParallel(conf, builders, handlers);
}