1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | const Hook = require("./Hook");
|
---|
8 | const HookCodeFactory = require("./HookCodeFactory");
|
---|
9 |
|
---|
10 | class AsyncParallelBailHookCodeFactory extends HookCodeFactory {
|
---|
11 | content({ onError, onResult, onDone }) {
|
---|
12 | let code = "";
|
---|
13 | code += `var _results = new Array(${this.options.taps.length});\n`;
|
---|
14 | code += "var _checkDone = function() {\n";
|
---|
15 | code += "for(var i = 0; i < _results.length; i++) {\n";
|
---|
16 | code += "var item = _results[i];\n";
|
---|
17 | code += "if(item === undefined) return false;\n";
|
---|
18 | code += "if(item.result !== undefined) {\n";
|
---|
19 | code += onResult("item.result");
|
---|
20 | code += "return true;\n";
|
---|
21 | code += "}\n";
|
---|
22 | code += "if(item.error) {\n";
|
---|
23 | code += onError("item.error");
|
---|
24 | code += "return true;\n";
|
---|
25 | code += "}\n";
|
---|
26 | code += "}\n";
|
---|
27 | code += "return false;\n";
|
---|
28 | code += "}\n";
|
---|
29 | code += this.callTapsParallel({
|
---|
30 | onError: (i, err, done, doneBreak) => {
|
---|
31 | let code = "";
|
---|
32 | code += `if(${i} < _results.length && ((_results.length = ${i +
|
---|
33 | 1}), (_results[${i}] = { error: ${err} }), _checkDone())) {\n`;
|
---|
34 | code += doneBreak(true);
|
---|
35 | code += "} else {\n";
|
---|
36 | code += done();
|
---|
37 | code += "}\n";
|
---|
38 | return code;
|
---|
39 | },
|
---|
40 | onResult: (i, result, done, doneBreak) => {
|
---|
41 | let code = "";
|
---|
42 | code += `if(${i} < _results.length && (${result} !== undefined && (_results.length = ${i +
|
---|
43 | 1}), (_results[${i}] = { result: ${result} }), _checkDone())) {\n`;
|
---|
44 | code += doneBreak(true);
|
---|
45 | code += "} else {\n";
|
---|
46 | code += done();
|
---|
47 | code += "}\n";
|
---|
48 | return code;
|
---|
49 | },
|
---|
50 | onTap: (i, run, done, doneBreak) => {
|
---|
51 | let code = "";
|
---|
52 | if (i > 0) {
|
---|
53 | code += `if(${i} >= _results.length) {\n`;
|
---|
54 | code += done();
|
---|
55 | code += "} else {\n";
|
---|
56 | }
|
---|
57 | code += run();
|
---|
58 | if (i > 0) code += "}\n";
|
---|
59 | return code;
|
---|
60 | },
|
---|
61 | onDone
|
---|
62 | });
|
---|
63 | return code;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | const factory = new AsyncParallelBailHookCodeFactory();
|
---|
68 |
|
---|
69 | const COMPILE = function(options) {
|
---|
70 | factory.setup(this, options);
|
---|
71 | return factory.create(options);
|
---|
72 | };
|
---|
73 |
|
---|
74 | function AsyncParallelBailHook(args = [], name = undefined) {
|
---|
75 | const hook = new Hook(args, name);
|
---|
76 | hook.constructor = AsyncParallelBailHook;
|
---|
77 | hook.compile = COMPILE;
|
---|
78 | hook._call = undefined;
|
---|
79 | hook.call = undefined;
|
---|
80 | return hook;
|
---|
81 | }
|
---|
82 |
|
---|
83 | AsyncParallelBailHook.prototype = null;
|
---|
84 |
|
---|
85 | module.exports = AsyncParallelBailHook;
|
---|