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 |
5x
5x
5x
5x
5x
74x
73x
73x
62x
20x
62x
1x
1x
1x
61x
73x
73x
73x
73x
2x
2x
2x
1x
1x
1x
1x
73x
73x
1x
1x
1x
1x
1x
3x
3x
1x
1x
1x
1x
3x
5x
3x
1x
4x
7x
2x
2x
2x
1x
4x
1x
3x
3x
3x
3x
1x
2x
5x
| "use strict";
var core = require("./core");
var coreCmds = require("./cmds");
var coreInterpreters = require("./interpreters");
var interpreters = Object.assign({}, coreInterpreters);
var context = {};
function promisify(fn) {
if (fn.eadPromisified) return fn;
var validator = fn.validator;
var promised = function promised() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (validator) {
try {
validator.apply(void 0, args);
} catch (e) {
return Promise.reject(e);
}
}
return core.call.apply(core, [context, interpreters, fn].concat(args));
}; // try/catch because this is nice for reporting, but not
// necessary for the system to function
// Note: there is a unit test to validate this behavior
// so errors, although swallowed here, are picked
// up in the unit test.
try {
Object.defineProperty(promised, "name", {
value: fn.name,
writable: false
});
} catch (e) {}
promised.eadFn = fn;
promised.callWithContext = function (c) {
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
if (validator) {
try {
validator.apply(void 0, args);
} catch (e) {
return Promise.reject(e);
}
}
return core.call.apply(core, [Object.assign({}, context, c), interpreters, fn].concat(args));
};
promised.eadPromisified = true;
return promised;
}
function call(fn) {
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
}
return promisify(fn).apply(void 0, args);
}
function doCmd(cmd) {
return promisify(
/*#__PURE__*/
regeneratorRuntime.mark(function doCmd() {
return regeneratorRuntime.wrap(function doCmd$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return cmd;
case 2:
return _context.abrupt("return", _context.sent);
case 3:
case "end":
return _context.stop();
}
}
}, doCmd, this);
}))();
}
function setContext(c) {
context = c;
}
function getContext() {
return context;
}
function addToContext(c) {
context = Object.assign({}, context, c);
}
function setInterpreters(h) {
interpreters = h;
}
function getInterpreters() {
return interpreters;
}
function addInterpreters(h) {
interpreters = Object.assign({}, interpreters, h);
}
function reset() {
interpreters = {};
context = {};
}
function onError(fn) {
if (typeof fn !== "function") throw new Error("onError requires a function");
addToContext({
onError: fn
});
}
function effect(fn, validator) {
if (validator !== undefined && typeof validator !== "function") {
throw new Error("validator must be a function");
}
return function () {
var _coreCmds$call;
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
if (typeof validator === "function") {
validator.apply(void 0, args);
}
return (_coreCmds$call = coreCmds.call).fn.apply(_coreCmds$call, [fn].concat(args));
};
}
module.exports = {
cmds: coreCmds,
interpreters: coreInterpreters,
promisify: promisify,
call: call,
doCmd: doCmd,
setContext: setContext,
getContext: getContext,
addToContext: addToContext,
setInterpreters: setInterpreters,
getInterpreters: getInterpreters,
addInterpreters: addInterpreters,
reset: reset,
onError: onError,
effect: effect
};
//# sourceMappingURL=index.js.map |