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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 |
10x
10x
10x
10x
218x
60x
218x
217x
216x
216x
216x
216x
216x
280x
280x
216x
216x
216x
172x
1x
1x
1x
44x
44x
3x
3x
3x
435x
435x
435x
389x
389x
389x
217x
217x
217x
181x
181x
181x
38x
38x
46x
216x
181x
435x
389x
389x
389x
217x
217x
223x
217x
217x
223x
223x
223x
223x
223x
221x
18x
221x
187x
2x
2x
2x
34x
34x
34x
34x
34x
34x
1x
1x
1x
223x
223x
223x
223x
223x
223x
223x
89x
89x
89x
78x
3x
2x
2x
2x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
216x
216x
216x
216x
1x
1x
1x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
1x
1x
1x
1x
1x
1x
1x
1x
1x
10x
| "use strict";
var _require = require("./util"),
toArray = _require.toArray,
toPromise = _require.toPromise,
delay = _require.delay;
function call(context, interpreters, fn) {
for (var _len = arguments.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
args[_key - 3] = arguments[_key];
}
if (!context) return Promise.reject(new Error("context required"));
if (!fn) return Promise.reject(new Error("function required"));
var gen = fn.apply(null, args);
var el = newExecutionLog();
var childContext = Object.assign({}, context);
childContext.stack = (childContext.stack || []).concat({
context: childContext,
interpreters: interpreters,
fn: fn,
args: args
}); // clean up circular references to allow for serialization
childContext.stack = childContext.stack.map(function (s) {
s.context = Object.assign({}, s.context, {
stack: undefined
});
return s;
});
var start = Date.now();
onCall({
args: args,
fn: fn,
context: childContext
});
return run(childContext, interpreters, fn, gen, null, el).then(function (result) {
if (!childContext.onCallComplete) return result;
var end = Date.now();
onCallComplete({
success: true,
fn: fn,
result: result,
context: childContext,
start: start,
end: end,
latency: end - start
});
return result;
}).catch(function (e) {
onError(e, childContext);
if (!childContext.onCallComplete) return Promise.reject(e);
var end = Date.now();
onCallComplete({
success: false,
fn: fn,
result: e,
context: childContext,
start: start,
end: end,
latency: end - start
});
return Promise.reject(e);
});
}
function run(context, interpreters, fn, gen, input, el) {
var genOperation = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : "next";
try {
var _getNextOutput = getNextOutput(gen, input, genOperation),
output = _getNextOutput.output,
done = _getNextOutput.done;
if (done) return toPromise(output);
var isList = Array.isArray(output);
var commandsList = toArray(output);
return processCommands(context, interpreters, fn, commandsList, el).then(function (results) {
var unwrappedResults = unwrapResults(isList, results);
el.step++;
return run(context, interpreters, fn, gen, unwrappedResults, el, "next");
}).catch(function (e) {
el.step++;
return run(context, interpreters, fn, gen, e, el, "throw");
});
} catch (e) {
return Promise.reject(e);
}
}
function newExecutionLog() {
return {
step: 0
};
}
function unwrapResults(isList, results) {
return isList ? results : results[0];
}
function getNextOutput(fn, input, op) {
var _fn$op = fn[op](input),
output = _fn$op.value,
done = _fn$op.done;
return {
output: output,
done: done
};
}
function processCommands(context, interpreters, fn, commands, el) {
try {
var pc = function pc(c, index) {
return processCommand(context, interpreters, fn, c, el, index);
};
var promises = commands.map(pc);
return Promise.all(promises);
} catch (e) {
// istanbul ignore next
return Promise.reject(e);
}
}
function processCommand(context, interpreters, fn, command, el, index) {
var start = Date.now();
onCommand({
command: command,
fn: fn,
start: start,
index: index,
step: el.step,
context: context
});
var result;
try {
var interpreter = interpreters[command.type];
if (!interpreter) return Promise.reject(new Error("Interpreter of type \"".concat(command.type, "\" is not registered.")));
result = interpreter(command, {
call: call,
context: context,
interpreters: interpreters
});
} catch (e) {
result = Promise.reject(e);
}
return toPromise(result).then(function (r) {
if (!context.onCommandComplete) return r;
var end = Date.now();
onCommandComplete({
success: true,
context: context,
command: command,
fn: fn,
step: el.step,
index: index,
result: r,
start: start,
end: end
});
return r;
}).catch(function (e) {
e.command = command;
e.step = el.step;
e.fn = fn;
e.index = index;
onError(e, context);
if (!context.onCommandComplete) return Promise.reject(e);
var end = Date.now();
onCommandComplete({
success: false,
context: context,
command: command,
fn: fn,
step: el.step,
index: index,
result: e,
start: start,
end: end
});
return Promise.reject(e);
});
}
function onCommand(_ref) {
var command = _ref.command,
index = _ref.index,
step = _ref.step,
context = _ref.context,
start = _ref.start,
fn = _ref.fn;
if (!context.onCommand || typeof context.onCommand !== "function") return;
var r = {
command: command,
start: start,
index: index,
step: step,
context: context,
fn: fn
};
delay(function () {
return context.onCommand(r);
});
}
function onError(error, context) {
if (!context.onError) return;
if (error.eadReported) return; // tag error to prevent multiple reports as the error bubbles up
error.eadReported = true;
delay(function () {
return context.onError(error, context);
});
}
function onCommandComplete(_ref2) {
var success = _ref2.success,
command = _ref2.command,
index = _ref2.index,
step = _ref2.step,
result = _ref2.result,
context = _ref2.context,
start = _ref2.start,
end = _ref2.end,
fn = _ref2.fn;
var r = {
success: success,
command: command,
latency: end - start,
start: start,
end: end,
index: index,
step: step,
result: result,
context: context,
fn: fn
};
delay(function () {
return context.onCommandComplete(r);
});
}
function onCall(_ref3) {
var args = _ref3.args,
fn = _ref3.fn,
context = _ref3.context;
if (!context.onCall || typeof context.onCall !== "function") return;
var r = {
args: args,
fn: fn,
context: context
};
delay(function () {
return context.onCall(r);
});
}
function onCallComplete(_ref4) {
var success = _ref4.success,
result = _ref4.result,
fn = _ref4.fn,
context = _ref4.context,
start = _ref4.start,
end = _ref4.end,
latency = _ref4.latency;
var r = {
success: success,
fn: fn,
context: context,
end: end,
latency: latency,
result: result,
start: start
};
delay(function () {
return context.onCallComplete(r);
});
}
function buildFunctions(context, interpreters, functions) {
var promiseFunctions = {};
var _loop = function _loop(i) {
promiseFunctions[i] = function () {
var localContext = Object.assign({
name: i
}, context);
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return call.apply(void 0, [localContext, interpreters, functions[i]].concat(args));
};
};
for (var i in functions) {
_loop(i);
}
return promiseFunctions;
}
module.exports = {
call: call,
buildFunctions: buildFunctions
};
//# sourceMappingURL=core.js.map |