-
Notifications
You must be signed in to change notification settings - Fork 10
/
clues.js
339 lines (296 loc) · 12.4 KB
/
clues.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
(function(self) {
'use strict';
if (typeof module !== 'undefined') {
clues.Promise = require('bluebird');
module.exports = clues;
} else {
clues.Promise = self.Promise;
self.clues = clues;
}
var reArgs = /^\s*function(?:.|\n)*?\(([^)]*?)\).*/;
var reEs6 = /^\s*\({0,1}([^)]*?)\){0,1}\s*=>/;
var reEs6Class = /^\s*[a-zA-Z0-9\-$_]+\s*\(((?:.|\n)*?)\)\s*{/;
var createEx = (e,fullref,caller,ref,report) => {
if (e.fullref) return e;
let result = {ref : e.ref || ref || fullref, message: e.message || e, fullref: e.fullref || fullref, caller: e.caller || caller, stack: e.stack || '', error: true, notDefined: e.notDefined, report: e.report || report, value: e.value, cache: e.cache, cluesHasLogged: e.cluesHasLogged};
return result;
};
var reject = (e,fullref,caller,ref) => clues.reject(createEx(e || {},fullref,caller,ref));
var isPromise = f => f && f.then && typeof f.then === 'function';
var noop = d => d;
var undefinedPromise = clues.Promise.resolve(undefined);
var fakePrivate = function(){};
fakePrivate.private = true;
function matchArgs(fn) {
if (!fn.__args__) {
var match = fn.prototype && fn.prototype.constructor.toString() || fn.toString();
match = match.replace(/^\s*async/,'');
match = reArgs.exec(match) || reEs6.exec(match) || reEs6Class.exec(match);
fn.__args__ = !match ? [] : match[1].replace(/\s/g,'')
.split(',')
.filter(function(d) {
if (d === '$private')
fn.private = true;
if (d === '$prep')
fn.prep = true;
return d.length;
});
}
return fn.__args__;
}
// fast promise rejection
const Rejection = clues.Promise.reject();
Rejection.suppressUnhandledRejections();
clues.reject = d => Object.create(Rejection,{_fulfillmentHandler0: {value: d}});
function clues(logic,fn,$global,caller,fullref) {
try {
let rawCluesResult = _rawClues(logic,fn,$global,caller,fullref);
return clues.Promise.resolve(rawCluesResult);
}
catch (e) {
return reject(e,fullref,caller);
}
}
function promiseHelper(val, success, error, _finally, _errorMessage) {
if (isPromise(val)) {
// if it's already resolve, we can just use that direct
if (!val.isFulfilled) val = clues.Promise.resolve(val);
if (val.isFulfilled()) return promiseHelper(val.value(), success, error, _finally);
if (val.isRejected()) return promiseHelper(null, success, error, _finally, val.reason());
let result = val;
if (success) result = result.then(success);
if (error) result = result.catch(error);
if (_finally) result = result.finally(_finally);
return result;
}
if (_errorMessage) {
let result = reject(_errorMessage);
if (error) {
result = error(_errorMessage);
}
if (_finally) _finally(val);
return result;
}
let result = null;
try {
result = success(val);
if (isPromise(result) && result.isRejected && result.isRejected()) {
result = promiseHelper(null, success, error, _finally, result.reason());
}
}
catch (e) { result = promiseHelper(null, success, error, _finally, e); }
if (_finally) _finally(val);
return result;
}
function storeRef(logic, ref, value, fullref, caller) {
if (ref) {
try {
logic[ref] = value;
}
catch (e) {
try {
Object.defineProperty(logic,ref,{value: value, enumerable: true, configurable: true, writable: true});
return value;
}
catch (e) {
return reject({ref : ref, message: 'Object immutable', fullref:fullref,caller: caller, stack: e.stack || '', value: value,error:true});
}
}
}
return value;
}
function expandFullRef(fullref, next) {
var separator = fullref && fullref[fullref.length-1] !== '(' && '.' || '';
return (fullref ? fullref+separator : '')+next;
}
function _rawClues(logic,fn,$global,caller,fullref) {
var args,ref;
if (!$global) $global = {};
if (typeof logic === 'function' || isPromise(logic))
return promiseHelper(_rawClues({},logic,$global,caller,fullref),
logic => _rawClues(logic,fn,$global,caller,fullref));
if (typeof fn === 'string') {
ref = fn;
var dot = ref.search(/ᐅ|\./);
if (dot > -1 && (!logic || logic[ref] === undefined)) {
var next = ref.slice(0,dot);
let handleError = e => {
if (e && e.notDefined && logic && logic.$external && typeof logic.$external === 'function') {
if (!logic[ref]) {
try {
return storeRef(logic, ref, _rawClues(logic,function() { return logic.$external.call(logic,ref); },$global,ref,expandFullRef(fullref, ref)), fullref, caller);
}
catch (e) {
fullref = expandFullRef(fullref, ref);
return storeRef(logic, ref, reject({message:e.message || e, value: value}, fullref, ref, ref), fullref, caller);
}
}
return logic[ref];
}
else {
return reject(e);
}
};
return promiseHelper(_rawClues(logic,next,$global,caller,fullref),
d => {
logic = d;
fullref = expandFullRef(fullref, next);
ref = ref.slice(dot+1);
return promiseHelper(_rawClues(logic,ref,$global,caller,fullref), a => a, handleError);
},
handleError);
}
fullref = expandFullRef(fullref, ref);
fn = logic ? logic[ref] : undefined;
if (fn === undefined) {
if (typeof(logic) === 'object' && logic !== null && (Object.getPrototypeOf(logic) || {})[ref] !== undefined)
fn = Object.getPrototypeOf(logic)[ref];
else if ($global[ref] && caller && caller !== '__user__')
return _rawClues($global,ref,$global,caller,fullref);
else if (logic && logic.$property && typeof logic.$property === 'function')
fn = logic[ref] = function() { return logic.$property.call(logic,ref); };
else return reject({message: ref+' not defined', notDefined:true},fullref,caller,ref);
}
}
// Support an array with some argument names in front and the function as last element
if (typeof fn === 'object' && fn && fn.length && typeof fn[fn.length-1] == 'function') {
if (fn.length > 1 && (typeof(fn[0]) === 'object' || typeof(fn[0]) == 'function') && !fn[0].length) {
var obj = fn[0];
fn = fn.slice(1);
if (fn.length === 1) fn = fn[0];
var result = _rawClues(obj,fn,$global,caller,fullref);
return storeRef(logic, ref, result, fullref, caller);
}
args = fn.slice(0,fn.length-1);
fn = fn[fn.length-1];
var fnArgs = matchArgs(fn);
var numExtraArgs = fnArgs.length-args.length;
if (numExtraArgs) {
args = args.concat(fnArgs.slice(numExtraArgs));
}
}
if (typeof fn === 'function')
args = (args || matchArgs(fn));
// If fn name is private or promise private is true, reject when called directly
if (fn && (!caller || caller == '__user__') && ((typeof(fn) === 'function' && (fn.private || fn.name == '$private' || fn.name == 'private')) || (fn.then && fn.private)))
return reject({message: ref+' not defined', notDefined:true }, fullref, caller, ref);
// If the logic reference is not a function, we simply return the value
if (typeof fn !== 'function' || ((ref && ref[0] === '$') && !fn.prep && fn.name !== '$prep')) {
// If the value is a promise we wait for it to resolve to inspect the result
if (isPromise(fn))
return promiseHelper(fn, d => {
return (typeof d == 'function' || (d && typeof d == 'object' && d.length)) ? _rawClues(logic,d,$global,caller,fullref) : d;
});
else
return fn;
}
// Shortcuts to define empty objects with $property or $external
if (fn.name === '$property' || (args[0] === '$property' && args.length === 1)) return storeRef(logic, ref, {$property: fn.bind(logic)}, fullref, caller);
if (fn.name === '$external' || (args[0] === '$external' && args.length === 1)) return storeRef(logic, ref, {$external: fn.bind(logic)}, fullref, caller);
if (fn.name === '$service') return fn;
let argsHasPromise = false, errorArgs = null;
args = args.map(function(arg) {
if (arg === null || arg === undefined) return arg;
var optional,showError,res;
if ((optional = (arg[0] === '_'))) arg = arg.slice(1);
if ((showError = (arg[0] === '_'))) arg = arg.slice(1);
if (arg[0] === '$' && logic[arg] === undefined) {
if (arg === '$caller') return caller;
else if (arg === '$fullref') return fullref;
else if (arg === '$global') return $global;
else if (arg === '$private') {
fn.private = true;
return true;
}
else if (arg === '$prep') {
fn.prep = true;
return true;
}
}
let processError = e => {
if (optional) return (showError) ? e : undefined;
let rejection = reject(e);
if (!errorArgs) errorArgs = rejection;
return rejection;
};
try {
res = promiseHelper(_rawClues(logic,arg,$global,ref || 'fn',fullref + '('), d => d, processError);
}
catch (e) {
res = processError(e);
}
if (!argsHasPromise && isPromise(res)) argsHasPromise = true;
return res;
});
var inputs = errorArgs ? errorArgs : (argsHasPromise ? clues.Promise.all(args) : args),
wait = Date.now(),
duration,
hasHandledError = false;
let solveFn = args => {
duration = Date.now();
let result = null;
try {
result = fn.apply(logic || {}, args);
if (result === undefined) {
result = undefinedPromise;
fn = fakePrivate; // private function results remain promises in `Logic`. We don't want to modify the fn, and this is somewhat more efficient than adding another flag
}
}
catch (e) {
// If fn is a class we solve for the constructor variables (if defined) and return a new instance
if (e instanceof TypeError && /^Class constructor/.exec(e.message)) {
let constructorArgs = (/constructor\s*\((.*?)\)/.exec(fn.toString()) || [])[1];
constructorArgs = constructorArgs ? constructorArgs.split(',').map(d => d.trim()) : [];
constructorArgs.push(function() {
let newObj = new (Function.prototype.bind.apply(fn,[null].concat(Array.prototype.slice.call(arguments))));
return newObj;
});
result = _rawClues(logic,constructorArgs,$global,ref || 'fn',fullref);
}
else {
throw e;
}
}
if (isPromise(result) && !result.isFulfilled) result = clues.Promise.resolve(result); // wrap non-bluebird promise
return result;
};
let handleError = e => {
if (hasHandledError) return reject(e);
hasHandledError = true;
let wrappedEx = createEx(e || {}, fullref, caller, ref, true);
if (e && e.stack && !wrappedEx.cluesHasLogged && typeof $global.$logError === 'function') {
wrappedEx.cluesHasLogged = true;
$global.$logError(wrappedEx, fullref);
}
return storeRef(logic, ref, reject(wrappedEx), fullref, caller);
};
let captureTime = () => {
if (typeof $global.$duration === 'function')
$global.$duration(fullref || ref || (fn && fn.name),[(Date.now()-duration),(Date.now())-wait],ref);
};
var value = null;
if (errorArgs) {
args.forEach(input => input && input.suppressUnhandledRejections && input.suppressUnhandledRejections());
value = handleError(errorArgs.reason());
}
else if (isPromise(inputs)) {
value = inputs.then(d => solveFn(d)).catch(e => {
return handleError(e);
}).finally(captureTime);
}
else {
try { value = solveFn(inputs); }
catch (e) { value = handleError(e); }
}
value = promiseHelper(value, noop, handleError, captureTime);
value = promiseHelper(value,
d => (typeof d == 'string' || typeof d == 'number') ? d : _rawClues(logic,d,$global,caller,fullref),
e => reject(e, fullref, caller, ref)
);
if (fn.name == 'private' || fn.name == '$private' || fn.private) {
if (!isPromise(value)) value = clues.Promise.resolve(value);
value.private = true;
}
return storeRef(logic, ref, value, fullref, caller);
}
})(this);