-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (155 loc) · 6.07 KB
/
index.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
module.exports = (()=> {
let app = {};
app.older = require('./v0.4');
let assert = (condition, message)=> {
if (!condition) {
message = message || "Assertion failed";
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message;
}
};
let instance = function (instanceName) {
if (!instanceName) instanceName = 'flowpipe-' + new Date().getTime();
let works = {};
let order = [];
let args = {};
let MAX_THREAD = 10;
let AUTO_INCREASED = 0;
this.then = this.add = this.pipe = (name, work)=> {
if (!work) {
work = name;
name = work.name;
if (!name || name == '') {
name = 'auto-' + AUTO_INCREASED;
AUTO_INCREASED++;
}
}
order.push({type: 'work', name: name});
works[name] = work;
return this;
};
this.for = this.loop = this.loopback = (name, condition)=> {
order.push({type: 'loop', to: name, condition: condition});
return this;
};
this.init = (configure)=> {
order.push({type: 'init', configure: configure});
return this;
};
this.timestamp = (display)=> {
order.push({type: 'timestamp', display: display});
return this;
};
this.log = (display)=> {
order.push({type: 'log', display: display});
return this;
}
this.parallel = (setter, work)=> {
order.push({type: 'parallel', set: setter, do: work});
return this;
};
this.args = args;
this.list = ()=> order;
this.maxThread = (maxThread)=> {
order.push({type: 'MAX_THREAD', MAX_THREAD: maxThread});
return this;
};
this.run = this.start = this.end = this.finish = (callback)=> {
let starttime = new Date().getTime();
let pretime = new Date().getTime();
let manager = (wi)=> {
if (typeof wi == 'undefined') wi = 0;
let indicate = order[wi];
if (!indicate) {
if (callback) callback(args);
return;
}
if (indicate.type !== 'timestamp')
pretime = new Date().getTime();
if (indicate.type === 'work') {
works[indicate.name](args).then((result)=> {
if (typeof result === 'object')
for (let key in result)
args[key] = result[key];
manager(wi + 1);
}).catch((e)=> {
console.log(`[${instanceName}] ERROR IN "${indicate.name}"`);
console.log(e);
});
} else if (indicate.type === 'loop') {
try {
if (indicate.condition(args)) {
for (let i = 0; i < order.length; i++)
if (order[i].name === indicate.to)
wi = i;
} else {
wi++;
}
manager(wi);
} catch (e) {
console.log(`[${instanceName}] ERROR IN loop "${indicate.condition.toString()}"`);
console.log(e);
}
} else if (indicate.type === 'init') {
try {
indicate.configure(args);
manager(wi + 1);
} catch (e) {
console.log(`[${instanceName}] ERROR IN init "${indicate.configure.toString()}"`);
console.log(e);
}
} else if (indicate.type === 'parallel') {
try {
let pdata = indicate.set(args);
let pCnt = 0;
let pidx = 0;
let proceed = 0;
let parallelWork = ()=> {
if (!pdata[pidx]) return;
if (pCnt > MAX_THREAD) return;
pCnt++;
indicate.do(args, pdata[pidx], pidx).then(()=> {
proceed++;
pCnt--;
if (proceed === pdata.length) {
manager(wi + 1);
return;
}
parallelWork();
});
pidx++;
parallelWork();
};
parallelWork();
} catch (e) {
console.log(`[${instanceName}] ERROR IN parallel "${indicate.set.toString()}"`);
console.log(e);
}
} else if (indicate.type === 'MAX_THREAD') {
MAX_THREAD = indicate.MAX_THREAD;
manager(wi + 1);
} else if (indicate.type === 'log') {
try {
console.log(`[${instanceName}] ${indicate.display(args)}`);
} catch (e) {
}
manager(wi + 1);
} else if (indicate.type === 'timestamp') {
try {
console.log(`[${instanceName}] ${indicate.display(new Date().getTime() - starttime, new Date().getTime() - pretime)}`);
} catch (e) {
}
manager(wi + 1);
}
};
manager();
};
};
app.instance = (instanceName)=> {
return new instance(instanceName);
};
return app;
})();