react 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/{lib → dsl}/chain.js +5 -3
- package/{lib → dsl}/fstr.js +7 -5
- package/{lib → dsl}/pcode.js +8 -6
- package/examples/chain-events1.js +3 -3
- package/examples/chain1.js +2 -2
- package/examples/default-events1.js +2 -2
- package/examples/fstr-events1.js +4 -3
- package/examples/fstr1.js +3 -2
- package/examples/pcode1.js +2 -2
- package/lib/base-task.js +1 -0
- package/lib/cb-task.js +14 -1
- package/lib/core.js +33 -9
- package/lib/dsl.js +14 -6
- package/lib/event-manager.js +16 -5
- package/lib/finalcb-first-task.js +9 -6
- package/lib/finalcb-task.js +9 -6
- package/lib/input-parser.js +7 -3
- package/lib/parse.js +6 -3
- package/lib/promise-task.js +89 -0
- package/lib/ret-task.js +1 -1
- package/lib/task.js +23 -19
- package/lib/validate.js +3 -3
- package/lib/vcon.js +6 -3
- package/lib/when-task.js +81 -0
- package/package.json +4 -2
- package/promise-resolve.js +35 -0
- package/react.js +0 -4
- package/test/core-deferred.test.js +134 -0
- package/test/core-promised.test.js +132 -0
- package/test/core-when.test.js +84 -0
- package/test/core.test.js +63 -4
- package/test/dsl.test.js +58 -6
- package/test/{chain.test.js → dsl/chain.test.js} +71 -1
- package/test/{fstr.test.js → dsl/fstr.test.js} +1 -1
- package/test/{pcode.test.js → dsl/pcode.test.js} +122 -1
- package/test/exec-options.test.js +2 -1
- package/test/finalcb-task.test.js +6 -5
- package/test/input-parser.test.js +10 -6
- package/test/module-use.test.js +2 -190
- package/test/promise-auto-resolve.test.js +51 -0
- package/test/validate.test.js +4 -2
- package/test/vcon.test.js +13 -0
- package/oldExamples/analyze.js +0 -29
- package/oldExamples/analyze2.js +0 -29
- package/oldExamples/example10-dsl.js +0 -63
- package/oldExamples/example11.js +0 -62
- package/oldExamples/example12.js +0 -63
- package/oldExamples/example13.js +0 -63
- package/oldExamples/example14.js +0 -63
- package/oldExamples/example15.js +0 -75
- package/oldExamples/example6-ast.js +0 -47
- package/oldExamples/example6-dsl.js +0 -49
- package/oldExamples/example8-ast.js +0 -55
- package/oldExamples/example8-dsl.js +0 -53
- package/oldExamples/example9-ast.js +0 -58
- package/oldExamples/example9-dsl.js +0 -57
- package/oldExamples/function-str-ex1.js +0 -33
- package/oldExamples/function-str-ex2.js +0 -67
- package/oldExamples/trait1.js +0 -41
- package/oldExamples/trait2.js +0 -44
|
@@ -8,12 +8,20 @@ var VContext = require('./vcon.js');
|
|
|
8
8
|
|
|
9
9
|
var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
|
|
10
10
|
|
|
11
|
-
function FinalCbFirstSuccTask(
|
|
11
|
+
function FinalCbFirstSuccTask(outTaskOptions) {
|
|
12
|
+
var taskDef = outTaskOptions.taskDef;
|
|
13
|
+
var cbFunc = outTaskOptions.cbFunc;
|
|
14
|
+
var tasks = outTaskOptions.tasks;
|
|
15
|
+
var vCon = outTaskOptions.vCon;
|
|
16
|
+
var execOptions = outTaskOptions.execOptions;
|
|
17
|
+
var retValue = outTaskOptions.retValue;
|
|
18
|
+
if (typeof(cbFunc) !== 'function') throw new Error('callback is not a function');
|
|
12
19
|
var self = this;
|
|
13
20
|
Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
|
|
14
21
|
this.f = cbFunc;
|
|
15
22
|
this.tasks = tasks;
|
|
16
23
|
this.vCon = vCon;
|
|
24
|
+
this.retValue = retValue;
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
function format_error(errmsg, obj) {
|
|
@@ -27,11 +35,6 @@ FinalCbFirstSuccTask.validate = function (taskDef) {
|
|
|
27
35
|
errors.push(format_error(OUTTASK_A_REQ, taskDef));
|
|
28
36
|
}
|
|
29
37
|
return errors;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
FinalCbFirstSuccTask.create = function (taskDef, cbFunc, tasks, vCon) {
|
|
33
|
-
if (!(cbFunc && cbFunc instanceof Function)) throw new Error('callback is not a function');
|
|
34
|
-
return new FinalCbFirstSuccTask(taskDef, cbFunc, tasks, vCon);
|
|
35
38
|
};
|
|
36
39
|
|
|
37
40
|
/**
|
package/lib/finalcb-task.js
CHANGED
|
@@ -7,12 +7,20 @@ var STATUS = require('./status.js');
|
|
|
7
7
|
|
|
8
8
|
var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
|
|
9
9
|
|
|
10
|
-
function FinalCbTask(
|
|
10
|
+
function FinalCbTask(outTaskOptions) {
|
|
11
|
+
var taskDef = outTaskOptions.taskDef;
|
|
12
|
+
var cbFunc = outTaskOptions.cbFunc;
|
|
13
|
+
var tasks = outTaskOptions.tasks;
|
|
14
|
+
var vCon = outTaskOptions.vCon;
|
|
15
|
+
var execOptions = outTaskOptions.execOptions;
|
|
16
|
+
var retValue = outTaskOptions.retValue;
|
|
17
|
+
if (typeof(cbFunc) !== 'function') throw new Error('callback is not a function');
|
|
11
18
|
var self = this;
|
|
12
19
|
Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
|
|
13
20
|
this.f = cbFunc;
|
|
14
21
|
this.tasks = tasks;
|
|
15
22
|
this.vCon = vCon;
|
|
23
|
+
this.retValue = retValue;
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
function format_error(errmsg, obj) {
|
|
@@ -29,11 +37,6 @@ FinalCbTask.validate = function (taskDef) {
|
|
|
29
37
|
return errors;
|
|
30
38
|
};
|
|
31
39
|
|
|
32
|
-
FinalCbTask.create = function (taskDef, cbFunc, tasks, vCon) {
|
|
33
|
-
if (!(cbFunc && cbFunc instanceof Function)) throw new Error('callback is not a function');
|
|
34
|
-
return new FinalCbTask(taskDef, cbFunc, tasks, vCon);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
40
|
FinalCbTask.prototype.isReady = function () {
|
|
38
41
|
return (this.tasks.every(function (t) { return (t.status === STATUS.COMPLETE); }));
|
|
39
42
|
};
|
package/lib/input-parser.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var EventManager = require('./event-manager.js');
|
|
4
|
+
|
|
3
5
|
var defaultExecOptions = {
|
|
4
6
|
reactExecOptions: true,
|
|
5
|
-
outputStyle: '
|
|
7
|
+
outputStyle: 'cb',
|
|
6
8
|
};
|
|
7
9
|
|
|
8
10
|
var OUTPUT_STYLES = {
|
|
9
|
-
CALLBACK: '
|
|
11
|
+
CALLBACK: 'cb',
|
|
10
12
|
NONE: 'none'
|
|
11
13
|
};
|
|
12
14
|
|
|
@@ -36,7 +38,9 @@ function inputParser(inputArgs, ast) {
|
|
|
36
38
|
var splitResult = splitArgs(args, ast.inParams, parsedInput.options.outputStyle);
|
|
37
39
|
parsedInput.args = splitResult.args;
|
|
38
40
|
parsedInput.cb = splitResult.cb;
|
|
39
|
-
if (splitResult.
|
|
41
|
+
if (splitResult.outputStyle) parsedInput.options.outputStyle = splitResult.outputStyle;
|
|
42
|
+
if (splitResult.extra) parsedInput.extraArgs = splitResult.extra;
|
|
43
|
+
EventManager.global.emit(EventManager.TYPES.EXEC_INPUT_PREPROCESS, parsedInput); // hook
|
|
40
44
|
return parsedInput;
|
|
41
45
|
}
|
|
42
46
|
|
package/lib/parse.js
CHANGED
|
@@ -9,10 +9,13 @@ function splitTrimFilterArgs(commaSepArgs) { //parse 'one, two' into ['one', 'tw
|
|
|
9
9
|
.filter(function (s) { return (s); }); //filter out empty strings
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
@param patternFn regex + fn or splitStr + fn
|
|
14
|
+
*/
|
|
15
|
+
function parseReduce(accum, patternFn) {
|
|
13
16
|
if (typeof(accum) !== 'string') return accum; // already matched
|
|
14
|
-
var m =
|
|
15
|
-
if (m) return
|
|
17
|
+
var m = (patternFn.regex) ? patternFn.regex.exec(accum) : accum.split(patternFn.splitStr);
|
|
18
|
+
if (m) return patternFn.fn(m, accum); // pass in matches and origStr, return result obj
|
|
16
19
|
return accum; // no match, return str, will try next matcher
|
|
17
20
|
}
|
|
18
21
|
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
PromiseTask is a task which executes a fn that returns a promise
|
|
5
|
+
and when it completes it sets the values in vCon
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
var util = require('util');
|
|
9
|
+
var sprintf = require('sprintf').sprintf;
|
|
10
|
+
|
|
11
|
+
var BaseTask = require('./base-task.js');
|
|
12
|
+
|
|
13
|
+
function format_error(errmsg, obj) {
|
|
14
|
+
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var REQ = 'promiseTask requires f, a, out';
|
|
18
|
+
var FN_REQ = 'promiseTask requires f to be a function or string';
|
|
19
|
+
var A_REQ = 'promiseTask requires a to be an array of string param names';
|
|
20
|
+
var OUT_REQ = 'promiseTask requires out to be an array[1] of string param names';
|
|
21
|
+
|
|
22
|
+
function PromiseTask(taskDef) {
|
|
23
|
+
var self = this;
|
|
24
|
+
Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
PromiseTask.prototype = new BaseTask();
|
|
28
|
+
PromiseTask.prototype.constructor = PromiseTask;
|
|
29
|
+
|
|
30
|
+
PromiseTask.validate = function (taskDef) {
|
|
31
|
+
var errors = [];
|
|
32
|
+
if (!taskDef.f || !taskDef.a || !taskDef.out) {
|
|
33
|
+
errors.push(format_error(REQ, taskDef));
|
|
34
|
+
} else {
|
|
35
|
+
var ftype = typeof(taskDef.f);
|
|
36
|
+
if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
|
|
37
|
+
errors.push(format_error(FN_REQ, taskDef));
|
|
38
|
+
}
|
|
39
|
+
if (! (Array.isArray(taskDef.a) &&
|
|
40
|
+
taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
|
|
41
|
+
errors.push(format_error(A_REQ, taskDef));
|
|
42
|
+
}
|
|
43
|
+
if (! (Array.isArray(taskDef.out) && taskDef.out.length <= 1 &&
|
|
44
|
+
taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
|
|
45
|
+
errors.push(format_error(OUT_REQ, taskDef));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return errors;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
PromiseTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
|
|
52
|
+
var self = this;
|
|
53
|
+
this.nextFn = function (arg) {
|
|
54
|
+
var args = Array.prototype.slice.call(arguments);
|
|
55
|
+
vCon.saveResults(self.out, args);
|
|
56
|
+
self.complete(args);
|
|
57
|
+
contExec();
|
|
58
|
+
};
|
|
59
|
+
this.failFn = function (err) {
|
|
60
|
+
handleTaskError(self, err);
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
PromiseTask.prototype.exec = function exec(vCon, handleError, contExec) {
|
|
65
|
+
try {
|
|
66
|
+
var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
|
|
67
|
+
//console.error('PromiseTask.exec.args=', args);
|
|
68
|
+
//console.error('PromiseTask.exec.vCon=', vCon);
|
|
69
|
+
this.start(args); //note the start time, args
|
|
70
|
+
var func = this.f;
|
|
71
|
+
var bindObj = vCon.getVar('this'); //global space or the original this
|
|
72
|
+
if (this.isMethodCall()) { //if method call then reset func and bindObj
|
|
73
|
+
func = vCon.getVar(this.f);
|
|
74
|
+
bindObj = this.getMethodObj(vCon);
|
|
75
|
+
} else if (typeof(func) === 'string') {
|
|
76
|
+
func = vCon.getVar(func); // we want the actual fn from this string
|
|
77
|
+
}
|
|
78
|
+
var retValue = func.apply(bindObj, args);
|
|
79
|
+
if (retValue && typeof(retValue.then) === 'function') { // is a promise
|
|
80
|
+
retValue.then(this.nextFn, this.failFn);
|
|
81
|
+
} else { // just a value, proceed now
|
|
82
|
+
this.nextFn(retValue);
|
|
83
|
+
}
|
|
84
|
+
} catch (err) { //catch and handle the task error, calling final cb
|
|
85
|
+
handleError(this, err);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
module.exports = PromiseTask;
|
package/lib/ret-task.js
CHANGED
|
@@ -50,7 +50,7 @@ RetTask.prototype.exec = function exec(vCon, handleError, contExec) {
|
|
|
50
50
|
var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
|
|
51
51
|
this.start(args); //note the start time, args
|
|
52
52
|
var func = this.f;
|
|
53
|
-
var bindObj =
|
|
53
|
+
var bindObj = vCon.getVar('this'); //global space or the original this
|
|
54
54
|
if (this.isMethodCall()) { //if method call then reset func and bindObj
|
|
55
55
|
func = vCon.getVar(this.f);
|
|
56
56
|
bindObj = this.getMethodObj(vCon);
|
package/lib/task.js
CHANGED
|
@@ -4,16 +4,21 @@ var util = require('util');
|
|
|
4
4
|
var sprintf = require('sprintf').sprintf;
|
|
5
5
|
var array = require('ensure-array');
|
|
6
6
|
var CbTask = require('./cb-task.js');
|
|
7
|
+
var PromiseTask = require('./promise-task.js');
|
|
7
8
|
var RetTask = require('./ret-task.js');
|
|
9
|
+
var WhenTask = require('./when-task.js');
|
|
8
10
|
var FinalCbTask = require('./finalcb-task.js');
|
|
9
11
|
var FinalCbFirstSuccTask = require('./finalcb-first-task.js');
|
|
10
12
|
var STATUS = require('./status.js');
|
|
11
13
|
var error = require('./error.js');
|
|
12
14
|
var VContext = require('./vcon.js');
|
|
15
|
+
var EventManager = require('./event-manager.js');
|
|
13
16
|
|
|
14
17
|
var TASK_TYPES = {
|
|
15
18
|
cb: CbTask,
|
|
16
|
-
ret: RetTask
|
|
19
|
+
ret: RetTask,
|
|
20
|
+
promise: PromiseTask,
|
|
21
|
+
when: WhenTask
|
|
17
22
|
};
|
|
18
23
|
function taskTypeKeys() { return Object.keys(TASK_TYPES); }
|
|
19
24
|
|
|
@@ -106,7 +111,8 @@ function validateLocalFunctions(inParams, taskDefs, locals) {
|
|
|
106
111
|
var mock_args = inParams.map(function (p) { return foo; }); //mock args with fns
|
|
107
112
|
var vCon = VContext.create(mock_args, inParams, locals);
|
|
108
113
|
var tasks = taskDefs.map(create);
|
|
109
|
-
tasks.
|
|
114
|
+
var tasksWFunctions = tasks.filter(function (t) { return (t.type !== 'when'); }); // non-when tasks need f
|
|
115
|
+
tasksWFunctions.forEach(function (t, idx) {
|
|
110
116
|
if (!t.functionExists(vCon)) { // error if function doesnt exist AND
|
|
111
117
|
if (!t.isMethodCall()) errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx)); // not method OR
|
|
112
118
|
else {
|
|
@@ -121,7 +127,9 @@ function validateLocalFunctions(inParams, taskDefs, locals) {
|
|
|
121
127
|
}
|
|
122
128
|
|
|
123
129
|
function fName(fn) {
|
|
124
|
-
return (
|
|
130
|
+
return (fn) ?
|
|
131
|
+
((fn.name) ? fn.name : fn) : // if defined, try name, otherwise toString()
|
|
132
|
+
'undefined'; // not defined, use undefined
|
|
125
133
|
}
|
|
126
134
|
|
|
127
135
|
/**
|
|
@@ -155,24 +163,21 @@ function create(taskDef) {
|
|
|
155
163
|
return new TaskConstructor(taskDef);
|
|
156
164
|
}
|
|
157
165
|
|
|
158
|
-
function createOutTask(taskDef,
|
|
166
|
+
function createOutTask(taskDef, cbFunc, tasks, vCon, execOptions) {
|
|
159
167
|
setMissingOutTaskType(taskDef);
|
|
160
|
-
var
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (err) { handleTaskError(task, err); return; } //handle error and return, we are done
|
|
168
|
-
|
|
169
|
-
//no error, save callback args to vCon context, then continue execution
|
|
170
|
-
vCon.saveResults(task.out, args);
|
|
171
|
-
task.complete(args);
|
|
172
|
-
contExec();
|
|
168
|
+
var outTaskOptions = {
|
|
169
|
+
taskDef: taskDef,
|
|
170
|
+
cbFunc: cbFunc,
|
|
171
|
+
tasks: tasks,
|
|
172
|
+
vCon: vCon,
|
|
173
|
+
execOptions: execOptions,
|
|
174
|
+
TaskConstructor: OUT_TASK_TYPES[taskDef.type]
|
|
173
175
|
};
|
|
176
|
+
EventManager.global.emit(EventManager.TYPES.EXEC_OUTTASK_CREATE, outTaskOptions); // hook
|
|
177
|
+
var TaskConstructor = outTaskOptions.TaskConstructor; // hook could have changed
|
|
178
|
+
return new TaskConstructor(outTaskOptions);
|
|
174
179
|
}
|
|
175
|
-
|
|
180
|
+
|
|
176
181
|
function createErrorHandler(vCon, outTask) {
|
|
177
182
|
return function handleError(task, err) {
|
|
178
183
|
task.status = STATUS.ERRORED;
|
|
@@ -234,6 +239,5 @@ exports.validateLocalFunctions = validateLocalFunctions;
|
|
|
234
239
|
exports.nameTasks = nameTasks;
|
|
235
240
|
exports.create = create;
|
|
236
241
|
exports.createOutTask = createOutTask;
|
|
237
|
-
exports.createCallback = createCallback;
|
|
238
242
|
exports.createErrorHandler = createErrorHandler;
|
|
239
243
|
exports.findReadyAndExec = findReadyAndExec;
|
package/lib/validate.js
CHANGED
|
@@ -13,8 +13,8 @@ var LOCALS_NOTNULL = 'ast.locals should not be null';
|
|
|
13
13
|
var DUP_OUTPUTS = 'multiple tasks output the same param, must be unique. param';
|
|
14
14
|
var MISSING_INPUTS = 'missing or mispelled variable referenced in flow definition: %s';
|
|
15
15
|
|
|
16
|
-
// match any of our literals true, false, int, float, quoted strings, or is property (has dot)
|
|
17
|
-
var LITERAL_OR_PROP_RE = /^(true|false|\-?[0-9\.]+)$|'|"|\./i;
|
|
16
|
+
// match any of our literals true, false, int, float, quoted strings, or is property (has dot), match vcon.js
|
|
17
|
+
var LITERAL_OR_PROP_RE = /^(true|false|this|null|\-?[0-9\.]+)$|'|"|\./i;
|
|
18
18
|
|
|
19
19
|
var validateInParams, validateTasks, validateOutTask, validateTaskNamesUnique;
|
|
20
20
|
var validateLocals, validateOuputsUnique, validateNoMissingNames;
|
|
@@ -153,7 +153,7 @@ function validateNoMissingNames(ast) {
|
|
|
153
153
|
if (!isLiteralOrProp(p) && !names[p]) accum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
|
|
154
154
|
return accum;
|
|
155
155
|
}, errors);
|
|
156
|
-
return errors;
|
|
156
|
+
return errors;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
module.exports = validate;
|
package/lib/vcon.js
CHANGED
|
@@ -16,6 +16,7 @@ VContext.prototype.getVar = function (name) { //name might be simple or obj.prop
|
|
|
16
16
|
// literal checks need to match what is in validate.js
|
|
17
17
|
if (name === 'true') return true;
|
|
18
18
|
if (name === 'false') return false;
|
|
19
|
+
if (name === 'null') return null;
|
|
19
20
|
if (/^-?[0-9]+$/.test(name)) return parseInt(name, 10); //int
|
|
20
21
|
if (/^-?[0-9.]+$/.test(name)) return parseFloat(name); //float
|
|
21
22
|
var m = /^("|')([^\1]*)\1$/.exec(name); //check for quoted string " or '
|
|
@@ -32,13 +33,13 @@ VContext.prototype.getVar = function (name) { //name might be simple or obj.prop
|
|
|
32
33
|
variable :LAST_RESULTS which keeps an array of the last values
|
|
33
34
|
which can be used for chaining and testing last results, etc.
|
|
34
35
|
*/
|
|
35
|
-
VContext.prototype.saveResults = function(paramArr, valuesArr) { // set values for params
|
|
36
|
+
VContext.prototype.saveResults = function (paramArr, valuesArr) { // set values for params
|
|
36
37
|
var self = this;
|
|
37
38
|
paramArr.forEach(function (k, idx) { //save values to v context
|
|
38
39
|
self.setVar(k, (valuesArr[idx] !== undefined) ? valuesArr[idx] : null); //upgrade any undefined to null
|
|
39
40
|
});
|
|
40
41
|
this.setLastResults(valuesArr);
|
|
41
|
-
}
|
|
42
|
+
};
|
|
42
43
|
|
|
43
44
|
VContext.prototype.setVar = function (name, value) { //name might be simple or obj.prop
|
|
44
45
|
if (!name) return; // if name is undefined or null, then discard
|
|
@@ -61,9 +62,11 @@ VContext.prototype.setVar = function (name, value) { //name might be simple or o
|
|
|
61
62
|
Ignore extra arguments passed in. Locals can be
|
|
62
63
|
passed into seed the VContext otherwise empty {}
|
|
63
64
|
will be used
|
|
65
|
+
@param self used to pass 'this' context in
|
|
64
66
|
*/
|
|
65
|
-
VContext.create = function (args, inParams, locals) {
|
|
67
|
+
VContext.create = function (args, inParams, locals, self) {
|
|
66
68
|
var initValues = {};
|
|
69
|
+
if (self) initValues['this'] = self;
|
|
67
70
|
if (locals) Object.keys(locals).forEach(function (k) { initValues[k] = locals[k]; }); // copy over keys
|
|
68
71
|
var vContext = new VContext();
|
|
69
72
|
vContext.values = args.reduce(function (vcon, x, idx) { // create vCon start with input args
|
package/lib/when-task.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
When task which checks if is a promise (has a then method)
|
|
5
|
+
and waits for it to resolve.
|
|
6
|
+
|
|
7
|
+
If argument does not have a then method, it resolves immediately
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var util = require('util');
|
|
11
|
+
var sprintf = require('sprintf').sprintf;
|
|
12
|
+
|
|
13
|
+
var BaseTask = require('./base-task.js');
|
|
14
|
+
|
|
15
|
+
function format_error(errmsg, obj) {
|
|
16
|
+
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var REQ = 'whenTask requires a, out';
|
|
20
|
+
var A_REQ = 'whenTask requires a to be an array[1] of string param names';
|
|
21
|
+
var OUT_REQ = 'whenTask requires out to be an array[1] of string param names';
|
|
22
|
+
|
|
23
|
+
function WhenTask(taskDef) {
|
|
24
|
+
var self = this;
|
|
25
|
+
Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
WhenTask.prototype = new BaseTask();
|
|
29
|
+
WhenTask.prototype.constructor = WhenTask;
|
|
30
|
+
|
|
31
|
+
WhenTask.prototype.f = function when() { // just here to keep validations happy
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
WhenTask.validate = function (taskDef) {
|
|
35
|
+
var errors = [];
|
|
36
|
+
if (!taskDef.a || !taskDef.out) {
|
|
37
|
+
errors.push(format_error(REQ, taskDef));
|
|
38
|
+
} else {
|
|
39
|
+
if (! (Array.isArray(taskDef.a) && taskDef.a.length === 1 &&
|
|
40
|
+
taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
|
|
41
|
+
errors.push(format_error(A_REQ, taskDef));
|
|
42
|
+
}
|
|
43
|
+
if (! (Array.isArray(taskDef.out) && taskDef.out.length <= 1 &&
|
|
44
|
+
taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
|
|
45
|
+
errors.push(format_error(OUT_REQ, taskDef));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return errors;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
WhenTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
|
|
52
|
+
var self = this;
|
|
53
|
+
this.nextFn = function (arg) {
|
|
54
|
+
var args = Array.prototype.slice.call(arguments);
|
|
55
|
+
vCon.saveResults(self.out, args);
|
|
56
|
+
self.complete(args);
|
|
57
|
+
contExec();
|
|
58
|
+
};
|
|
59
|
+
this.failFn = function (err) {
|
|
60
|
+
handleTaskError(self, err);
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
WhenTask.prototype.exec = function exec(vCon, handleError, contExec) {
|
|
65
|
+
try {
|
|
66
|
+
var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
|
|
67
|
+
//console.error('WhenTask.exec.args=', args);
|
|
68
|
+
//console.error('WhenTask.exec.vCon=', vCon);
|
|
69
|
+
this.start(args); //note the start time, args
|
|
70
|
+
var arg = args[0]; // one value allowed
|
|
71
|
+
if (arg && typeof(arg.then) === 'function') { // is a promise
|
|
72
|
+
arg.then(this.nextFn, this.failFn);
|
|
73
|
+
} else { // not a promise continue immediately
|
|
74
|
+
this.nextFn(arg);
|
|
75
|
+
}
|
|
76
|
+
} catch (err) { //catch and handle the task error, calling final cb
|
|
77
|
+
handleError(this, err);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
module.exports = WhenTask;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react",
|
|
3
3
|
"description": "React is a javascript module to make it easier to work with asynchronous code, by reducing boilerplate code and improving error and exception handling while allowing variable and task dependencies when defining flow.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"author": "Jeff Barczewski <jeff.barczewski@gmail.com>",
|
|
6
6
|
"repository": { "type": "git", "url": "http://github.com/jeffbski/react.git" },
|
|
7
7
|
"bugs" : { "url": "http://github.com/jeffbski/react/issues" },
|
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"tap" : "~0.1.0",
|
|
18
|
-
"tapr" : "~0.1.0"
|
|
18
|
+
"tapr" : "~0.1.0",
|
|
19
|
+
"promised-io" : "~0.3.0",
|
|
20
|
+
"Deferred" : "~0.1.1"
|
|
19
21
|
},
|
|
20
22
|
"scripts": {
|
|
21
23
|
"test": "node_modules/.bin/tapr test"
|