react 0.2.6 → 0.5.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/.npmignore +2 -1
- package/README.md +94 -149
- package/doc/advanced.md +161 -0
- package/doc/color-def.graffle +938 -0
- package/doc/color-def.png +0 -0
- package/doc/default-simple.dot +19 -0
- package/doc/default-simple.dot.png +0 -0
- package/examples/default-events1.js +33 -6
- package/examples/{fstr1.js → default-log-events.js} +20 -13
- package/examples/default-simple.js +45 -0
- package/lib/base-task.js +8 -6
- package/lib/cb-task.js +14 -1
- package/lib/core.js +43 -12
- package/lib/dsl.js +14 -6
- package/lib/event-collector.js +65 -0
- package/lib/event-manager.js +29 -16
- package/lib/finalcb-first-task.js +16 -10
- package/lib/finalcb-task.js +17 -10
- package/lib/input-parser.js +7 -3
- package/lib/log-events.js +86 -0
- package/lib/parse.js +6 -3
- package/lib/promise-resolve.js +35 -0
- package/lib/promise-task.js +89 -0
- package/lib/ret-task.js +1 -1
- package/lib/task.js +32 -23
- package/lib/track-tasks.js +60 -0
- package/lib/validate.js +3 -3
- package/lib/vcon.js +6 -3
- package/lib/when-task.js +81 -0
- package/package.json +5 -3
- package/react.js +33 -5
- 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 +108 -60
- package/test/dsl.test.js +58 -6
- 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 +16 -199
- package/test/promise-auto-resolve.test.js +52 -0
- package/test/validate.test.js +4 -2
- package/test/vcon.test.js +13 -0
- package/Jakefile.js +0 -8
- package/examples/chain-events1.js +0 -34
- package/examples/chain1.js +0 -19
- package/examples/fstr-events1.js +0 -51
- package/examples/pcode1.js +0 -22
- package/jake-tasks/jake-test.js +0 -64
- package/lib/chain.js +0 -148
- package/lib/fstr.js +0 -119
- package/lib/pcode.js +0 -173
- 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
- package/test/chain.test.js +0 -253
- package/test/fstr.test.js +0 -300
- package/test/pcode.test.js +0 -335
package/.npmignore
CHANGED
package/README.md
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
# React.js
|
|
2
2
|
|
|
3
|
-
React is a javascript module to make it easier to work with asynchronous code,
|
|
4
|
-
by reducing boilerplate code and improving error and exception handling while
|
|
5
|
-
allowing variable and task dependencies when defining flow. This project is
|
|
6
|
-
applying the concepts of Reactive programming or Dataflow to controlling
|
|
7
|
-
application flow.
|
|
3
|
+
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. This project is applying the concepts of Reactive programming or Dataflow to controlling application flow.
|
|
8
4
|
|
|
9
|
-
This async flow control module is initially designed to work with Node.js but
|
|
10
|
-
is planned to be extended to browser and other environments.
|
|
5
|
+
This async flow control module is initially designed to work with Node.js but is planned to be extended to browser and other environments.
|
|
11
6
|
|
|
12
7
|
React gets its name from similarities with how "chain reactions" work in the physical world. You start the reaction and then it cascades and continues until complete.
|
|
13
8
|
|
|
@@ -40,8 +35,8 @@ It takes inspiration from several projects including:
|
|
|
40
35
|
- object instance method calls
|
|
41
36
|
- class method calls
|
|
42
37
|
- selectFirst flow where the first task that returns defined, non-null value is used
|
|
43
|
-
-
|
|
44
|
-
-
|
|
38
|
+
- promise style functions - also automatic resolution of promise inputs (optional require('react/promise-resolve');)
|
|
39
|
+
- use of resulting flow function as callback style or promise style (if no callback provided) (provided via plugin corresponding to the promise library used) See https://github.com/jeffbski/react-deferred
|
|
45
40
|
- (planned) iteration on arrays, streams, sockets
|
|
46
41
|
- (planned) event emitter integration
|
|
47
42
|
|
|
@@ -49,7 +44,8 @@ The tasks can be mixed, meaning you can use async, sync, object method calls, cl
|
|
|
49
44
|
|
|
50
45
|
## Concept
|
|
51
46
|
|
|
52
|
-
Borrowing heavily from Tim and Elijah's ideas for conductor, this async flow control module provides a way to construct a flow from a
|
|
47
|
+
Borrowing heavily from Tim and Elijah's ideas for conductor, this async flow control module provides a way to construct a flow from a
|
|
48
|
+
collection of rules based on functions or methods (referred to as _tasks_ in this module). It allows dependencies to be defined between the tasks so they can run in parallel as their dependencies are satisfied. React can us both variable dependencies and task dependencies.
|
|
53
49
|
|
|
54
50
|
As tasks complete, React watches the dependencies and kicks off additional tasks that have all their dependencies met and are ready to execute. This allows the flow to run at maximum speed without needing to arbitrarily block tasks into groups of parallel and serial flow.
|
|
55
51
|
|
|
@@ -57,11 +53,12 @@ To reduce the boilerplate code needed and improve error handling, React automati
|
|
|
57
53
|
|
|
58
54
|
1. check for error and handle by calling outer callback function with this error after augmenting it with additional context information for easier debugging
|
|
59
55
|
2. save the callback variables into a context for future reference
|
|
60
|
-
3. call back into React (and it will kick off additional tasks that
|
|
56
|
+
3. call back into React (and it will kick off additional tasks that are now ready to go)
|
|
57
|
+
4. Using the dependencies specified for each
|
|
61
58
|
|
|
62
59
|
## Design
|
|
63
60
|
|
|
64
|
-
- Parse and validate DSL rules at module load time
|
|
61
|
+
- Parse and validate DSL rules at module load time creating AST
|
|
65
62
|
- Validate the flow AST at module load time - determine if dependencies can all be met as defined
|
|
66
63
|
- Execute the flow AST by calling the function with arguments
|
|
67
64
|
|
|
@@ -75,173 +72,120 @@ Pull from github - http://github.com/jeffbski/react
|
|
|
75
72
|
|
|
76
73
|
## Examples
|
|
77
74
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
3. [Using pseudocode DSL](#pcode)
|
|
81
|
-
4. [Using jquery-like chaining DSL](#chain)
|
|
75
|
+
<a name="defaultDSL"/>
|
|
76
|
+
### Example using default DSL interface
|
|
82
77
|
|
|
78
|
+
- Simple example showing flow definition of two async functions feeding a
|
|
79
|
+
synchronous function.
|
|
83
80
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
as the specific tests for the DSL you want to use.
|
|
81
|
+
- First two async functions inputs are satisfied by the flow inputs, so
|
|
82
|
+
they will both run immediately in parallel.
|
|
87
83
|
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
- The last function waits for the outputs of the previous ones, then
|
|
85
|
+
executes synchronously.
|
|
86
|
+
|
|
87
|
+
- Finally the flow calls the callback with the output values once all
|
|
88
|
+
the tasks have completed.
|
|
90
89
|
|
|
91
90
|
```javascript
|
|
92
|
-
// in your
|
|
91
|
+
// in your foobar module
|
|
93
92
|
var react = require('react');
|
|
94
93
|
|
|
95
94
|
// some normal async and sync functions
|
|
96
|
-
function
|
|
97
|
-
function
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
function
|
|
95
|
+
function loadFoo(fooPath, cb) {
|
|
96
|
+
setTimeout(function () {
|
|
97
|
+
cb(null, [fooPath, 'data'].join(':'));
|
|
98
|
+
}, 10);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function loadBar(barPath, barP2, cb) {
|
|
102
|
+
setTimeout(function () {
|
|
103
|
+
cb(null, [barPath, barP2, 'data'].join(':'));
|
|
104
|
+
}, 10);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function render(foo, bar) {
|
|
108
|
+
return ['<html>', foo, '/', bar, '</html>'].join('');
|
|
109
|
+
}
|
|
103
110
|
|
|
104
111
|
// define fn, glue together with react, it will parallelize
|
|
105
112
|
// starts with name and in/out params, then the tasks
|
|
106
|
-
var
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
writeOutput, 'html, user, cb -> err, htmlBytesWritten',
|
|
111
|
-
loadEmailTemplate, 'cb -> err, emailmd',
|
|
112
|
-
markdown, 'emailmd -> emailHtml', // no cb, implies sync fn
|
|
113
|
-
customizeEmail, 'user, emailHtml, cb -> err, custEHtml',
|
|
114
|
-
deliverEmail, 'custEHtml, cb -> err, custBytesWritten'
|
|
113
|
+
var loadRender = react('loadRender', 'fooPath, barPath, barP2, cb -> err, renderedOut',
|
|
114
|
+
loadFoo, 'fooPath, cb -> err, foo', // async cb function
|
|
115
|
+
loadBar, 'barPath, barP2, cb -> err, bar', // async cb function
|
|
116
|
+
render, 'foo, bar -> renderedOut' // sync function using outputs from first two
|
|
115
117
|
);
|
|
116
|
-
|
|
118
|
+
|
|
119
|
+
exports.loadRender = loadRender; // is a normal fn created by react
|
|
120
|
+
|
|
117
121
|
|
|
118
122
|
// in a different module far far away, use this as any other node function
|
|
119
|
-
var
|
|
120
|
-
foo.
|
|
121
|
-
// tasks were parallelized based on their
|
|
122
|
-
|
|
123
|
+
var foobar = require('foobar');
|
|
124
|
+
foobar.loadRender('foo.txt', 'bar.txt', 'BBB', function (err, renderedOut) {
|
|
125
|
+
// tasks in loadRender were parallelized based on their input dependencies
|
|
126
|
+
console.error('results:', renderedOut);
|
|
127
|
+
});
|
|
123
128
|
```
|
|
124
129
|
|
|
130
|
+
Below is a graph of how the dependencies are mapped by React which
|
|
131
|
+
also indicates how the tasks will be executed
|
|
125
132
|
|
|
126
|
-
|
|
127
|
-
### Example directly using AST
|
|
133
|
+

|
|
128
134
|
|
|
129
|
-
```javascript
|
|
130
|
-
var react = require('react');
|
|
131
135
|
|
|
132
|
-
function load(res, cb) { setTimeout(cb, 100, null, res + '-loaded'); }
|
|
133
|
-
function prefix(prefstr, str, cb) { setTimeout(cb, 100, null, prefstr + str); }
|
|
134
|
-
function postfix(str, poststr, cb) { setTimeout(cb, 100, null, str + poststr); }
|
|
135
|
-
function upper(str) { return str.toUpperCase(); }
|
|
136
|
-
|
|
137
|
-
var fn = react();
|
|
138
|
-
var errors = fn.setAndValidateAST({
|
|
139
|
-
inParams: ['res', 'prefstr', 'poststr'],
|
|
140
|
-
tasks: [
|
|
141
|
-
{ f: load, a: ['res'], out: ['lres'] },
|
|
142
|
-
{ f: upper, a: ['lres'], out: ['ulres'], type: 'ret' },
|
|
143
|
-
{ f: prefix, a: ['prefstr', 'ulres'], out: ['plres'] },
|
|
144
|
-
{ f: postfix, a: ['plres', 'poststr'], out: ['plresp'] }
|
|
145
|
-
],
|
|
146
|
-
outTask: { a: ['plresp'] }
|
|
147
|
-
});
|
|
148
|
-
console.error('errors:', errors); // []
|
|
149
136
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
154
|
-
```
|
|
137
|
+
## User API
|
|
138
|
+
|
|
139
|
+
The main function returned from require('react') can be used to define the AST used for the processing of the rules or flow.
|
|
155
140
|
|
|
156
|
-
|
|
157
|
-
### Example using Function String DSL interface
|
|
141
|
+
It takes the following arguments to define a flow function:
|
|
158
142
|
|
|
159
143
|
```javascript
|
|
160
|
-
var
|
|
144
|
+
var fn = react('loadRender', 'fooPath, barPath, barP2, cb -> err, renderedOut',
|
|
145
|
+
loadFoo, 'fooPath, cb -> err, foo',
|
|
146
|
+
loadBar, 'barPath, barP2, cb -> err, bar',
|
|
147
|
+
render, 'foo, bar -> renderedOut'
|
|
148
|
+
);
|
|
149
|
+
```
|
|
161
150
|
|
|
162
|
-
|
|
163
|
-
function loadFile(filename, cb){ setTimeout(cb, 100, null, 'Filedata'+filename); }
|
|
164
|
-
function markdown(filedata) { return 'html'+filedata; }
|
|
165
|
-
function prepareDirectory(outDirname, cb){ setTimeout(cb, 200, null, 'dircreated-'+outDirname); }
|
|
166
|
-
function writeOutput(html, user, cb){ setTimeout(cb, 300, null, html+'_bytesWritten'); }
|
|
167
|
-
function loadEmailTemplate(cb) { setTimeout(cb, 50, null, 'emailmd'); }
|
|
168
|
-
function customizeEmail(user, emailHtml, cb) { return 'cust-'+user+emailHtml; }
|
|
169
|
-
function deliverEmail(custEmailHtml, cb) { setTimeout(cb, 100, null, 'delivered-'+custEmailHtml); }
|
|
170
|
-
|
|
171
|
-
function useHtml(err, html, user, bytesWritten) {
|
|
172
|
-
if(err) {
|
|
173
|
-
console.log('***Error: %s', err);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
console.log('final result: %s, user: %s, written:%s', html, user, bytesWritten);
|
|
177
|
-
}
|
|
151
|
+

|
|
178
152
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
deliverEmail, 'custEmailHtml -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
|
|
189
|
-
], 'err, html, user, bytesWritten'); // callback output params
|
|
190
|
-
|
|
191
|
-
loadAndSave('file.md', 100, '/tmp/foo', useHtml); // executing the flow
|
|
192
|
-
```
|
|
153
|
+
1. **flow/function name** - string - represents the name of the flow or function that will be created. React will use the name when generating events so you can monitor progress and performance and also when errors occur.
|
|
154
|
+
2. **in/out flow parameter definition** - string - the inputs and outputs for the flow function. The parameters are specified in one single string for easy typing, separated by commas. The output follows the input after being separated by a `->`. Use the parameter name `cb` or `callback` to specify the Node style callback and `err` to represent the error parameter as the first output parameter of the callback. Literal values can also be specified directly (true, false, numbers, this, null). Literal strings can simply be quoted using single or double quotes.
|
|
155
|
+
3. **optional flow options** - object - If an object is provided immediately after the in/out flow def, then these options will be provided to react to customize the flow. This is reserved for future use.
|
|
156
|
+
4. **function reference or method string** - Specify the function to be called for this task, or if calling a method off of an object being passed in or returned by a task, use a string to specify like `'obj.method'`. These can be asynchronous Node-style callback `cb(err, ...)` functions or synchronous functions which simply return values directly.
|
|
157
|
+
5. **in/out task parameter definition** - string - similar to the in/out flow parameter definition above, these are the inputs and outputs that are passed to a task function and returned from a task function. The inputs will need to match either those from the flow inputs or outputs from other tasks that will run before this task. React will use the inputs as dependencies, so it will invoke and wait for response from the tasks that provide the dependent inputs. So simply by specifying inputs and outputs for the tasks, React will prioritize and parallelize tasks to run as fast as possible. Use `cb` or `callback` along with `err` to specify asynchronous Node style `cb(err, ...)` task, or omit both to specify a synchronous task.A synchronous task can only have a single return parameter.
|
|
158
|
+
6. **optional task options** - object - if an object is provided this can be used to specify additional options for this task. Currently the valid options for a task are:
|
|
159
|
+
- **name** - string - specifies a name for a task, otherwise React will try to use the function name or method string if it is unique in the flow. If a name is not unique subsequent tasks will have `_index` (zero based index of the task) added to create unique name. If you specify a name, you will also want to indicate a unique name for within the flow otherwise it will get a suffix as well. Example: `{ name: 'myTaskName' }`
|
|
160
|
+
- **after** - string, function reference, or array of string or function refs - specify additional preconditions that need to be complete before this task can run. In addition to the input dependencies being met, wait for these named tasks to complete before running. The preconditions are specified using the name of the task or if the task function was only used once and is a named function (not anonymous), you can just provide the function reference and it will determine name from it. Example: `{ after: 'foo' }` or `{ after: ['foo', 'bar'] }`
|
|
161
|
+
7. **repeat 4-6** - repeat steps 4-6 to specify additional tasks in this flow. As dependencies are met for tasks, React will invoke additional tasks that are ready to run in the order they are defined in this flow definition. So while the order does have some influence on the execution, it is primarily defined by the input dependencies and any other additonal preconditions specified with the `after` option. If you want to guarantee that something only runs after something else completes, then it will need to use an output from that task or specify the dependency with an `after`.
|
|
193
162
|
|
|
194
|
-
<a name="pcode"/>
|
|
195
|
-
### Example using pseudocode DSL interface
|
|
196
163
|
|
|
197
|
-
|
|
198
|
-
var react = require('react');
|
|
164
|
+
The flow function created by react from the input definition is a normal Node-style function which can be used like any other. These flow functions can be defined in a module and exported, they can be passed into other functions, used as methods on objects (the `this` context is passed in and available).
|
|
199
165
|
|
|
200
|
-
|
|
201
|
-
function add(a, b) { return a + b; }
|
|
202
|
-
var locals = { // since pcodeDefine uses strings, need references to functions passed into react
|
|
203
|
-
multiply: multiply,
|
|
204
|
-
add: add
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
var fn = react.pcodeDefine('a, b, cb', [ // input params
|
|
208
|
-
'm := multiply(a, b)', // using a callback function, use :=
|
|
209
|
-
's = add(m, a)', // using a sync function, use =
|
|
210
|
-
'cb(err, m, s)' // output params for final callback
|
|
211
|
-
], locals); // hash of functions that will be used
|
|
212
|
-
|
|
213
|
-
fn(2, 3, function (err, m, s) {
|
|
214
|
-
console.error('err:', err); // null
|
|
215
|
-
console.error('m:', m); // 2 * 3 = 6
|
|
216
|
-
console.error('s:', s); // 6 + 2 = 8
|
|
217
|
-
});
|
|
218
|
-
```
|
|
166
|
+
### Debugging React
|
|
219
167
|
|
|
220
|
-
|
|
221
|
-
### Example using jquery-like chaining DSL interface
|
|
168
|
+
React has a built-in plugin which can be loaded that will enable logging of tasks and flow as it executes very useful for debugging. For full details see [Advanced React - LogEvents](https://github.com/jeffbski/react/blob/master/doc/advanced.md#LogEvents) along with the other plugins and an explanation of the AST React uses.
|
|
222
169
|
|
|
223
170
|
```javascript
|
|
224
171
|
var react = require('react');
|
|
172
|
+
react.logEvents(); // turn on flow and task logging for all react functions
|
|
173
|
+
```
|
|
225
174
|
|
|
226
|
-
|
|
227
|
-
|
|
175
|
+
### Advanced React
|
|
176
|
+
|
|
177
|
+
React has many additional plugins and features which enable logging, monitoring, promise resolution, etc.
|
|
178
|
+
|
|
179
|
+
See the [Advanced React](https://github.com/jeffbski/react/blob/master/doc/advanced.md) for details on the AST React uses for processing and other plugins that are available.
|
|
228
180
|
|
|
229
|
-
var fn = react.chainDefine()
|
|
230
|
-
.in('a', 'b', 'cb') // input params
|
|
231
|
-
.out('err', 'm', 's') // final callback output params
|
|
232
|
-
.async(multiply).in('a', 'b', 'cb').out('err', 'm') // task def - async fn, in params, callback out params
|
|
233
|
-
.sync(add).in('m', 'a').out('s') // task def - sync fn, in params, return value
|
|
234
|
-
.end();
|
|
235
181
|
|
|
236
|
-
fn(2, 3, function (err, m, s) {
|
|
237
|
-
console.error('err:', err); // null
|
|
238
|
-
console.error('m:', m); // 2 * 3 = 6
|
|
239
|
-
console.error('s:', s); // 6 + 2 = 8
|
|
240
|
-
});
|
|
241
|
-
```
|
|
242
182
|
|
|
243
183
|
## Status
|
|
244
184
|
|
|
185
|
+
- 2012-01-18 - Remove old DSL interfaces, improve plugin loading (v0.5.0)
|
|
186
|
+
- 2012-01-17 - Additional documentation (v0.3.5)
|
|
187
|
+
- 2012-01-16 - Refine events and create logging plugin (v0.3.3)
|
|
188
|
+
- 2012-01-13 - Add promise tasks, promise resolution, refactor alternate DSL interfaces as optional requires (v0.3.0)
|
|
245
189
|
- 2012-01-11 - Provide warning/error when name is skipped in default DSL, literal check in validate (v0.2.5)
|
|
246
190
|
- 2012-01-10 - Create default DSL for react(), create error for missing variables, list remaining tasks when flow won't complete
|
|
247
191
|
- 2011-12-21 - Refactor from ground up with tests, changes to the interfaces
|
|
@@ -252,23 +196,24 @@ fn(2, 3, function (err, m, s) {
|
|
|
252
196
|
```bash
|
|
253
197
|
ok ast.test.js .................... 10/10
|
|
254
198
|
ok cb-task.test.js ................ 31/31
|
|
255
|
-
ok
|
|
256
|
-
ok core.test.js
|
|
257
|
-
ok
|
|
199
|
+
ok core-deferred.test.js .......... 11/11
|
|
200
|
+
ok core-promised.test.js .......... 11/11
|
|
201
|
+
ok core-when.test.js ................ 6/6
|
|
202
|
+
ok core.test.js ................. 104/104
|
|
203
|
+
ok dsl.test.js .................... 70/70
|
|
258
204
|
ok event-manager.test.js .......... 13/13
|
|
259
205
|
ok exec-options.test.js ............. 3/3
|
|
260
206
|
ok finalcb-task.test.js ............. 5/5
|
|
261
|
-
ok fstr.test.js ................... 67/67
|
|
262
207
|
ok input-parser.test.js ........... 15/15
|
|
263
|
-
ok module-use.test.js .............
|
|
264
|
-
ok
|
|
208
|
+
ok module-use.test.js ............. 24/24
|
|
209
|
+
ok promise-auto-resolve.test.js ..... 4/4
|
|
265
210
|
ok ret-task.test.js ............... 31/31
|
|
266
211
|
ok task.test.js ..................... 1/1
|
|
267
212
|
ok validate-cb-task.test.js ......... 6/6
|
|
268
213
|
ok validate-ret-task.test.js ........ 7/7
|
|
269
214
|
ok validate.test.js ............... 31/31
|
|
270
|
-
ok vcon.test.js ...................
|
|
271
|
-
total ...........................
|
|
215
|
+
ok vcon.test.js ................... 55/55
|
|
216
|
+
total ........................... 457/457
|
|
272
217
|
|
|
273
218
|
ok
|
|
274
219
|
```
|
package/doc/advanced.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# Advanced React
|
|
2
|
+
|
|
3
|
+
<a name="directAST"/>
|
|
4
|
+
## Example defining directly using AST
|
|
5
|
+
|
|
6
|
+
Defining flow directly using the AST. Additional DSL interfaces can be built by simply having them build the proper AST.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
var react = require('react');
|
|
11
|
+
|
|
12
|
+
function load(res, cb) { setTimeout(cb, 100, null, res + '-loaded'); }
|
|
13
|
+
function prefix(prefstr, str, cb) { setTimeout(cb, 100, null, prefstr + str); }
|
|
14
|
+
function postfix(str, poststr, cb) { setTimeout(cb, 100, null, str + poststr); }
|
|
15
|
+
function upper(str) { return str.toUpperCase(); }
|
|
16
|
+
|
|
17
|
+
var fn = react();
|
|
18
|
+
var errors = fn.setAndValidateAST({
|
|
19
|
+
inParams: ['res', 'prefstr', 'poststr'],
|
|
20
|
+
tasks: [
|
|
21
|
+
{ f: load, a: ['res'], out: ['lres'] },
|
|
22
|
+
{ f: upper, a: ['lres'], out: ['ulres'], type: 'ret' },
|
|
23
|
+
{ f: prefix, a: ['prefstr', 'ulres'], out: ['plres'] },
|
|
24
|
+
{ f: postfix, a: ['plres', 'poststr'], out: ['plresp'] }
|
|
25
|
+
],
|
|
26
|
+
outTask: { a: ['plresp'] }
|
|
27
|
+
});
|
|
28
|
+
console.error('errors:', errors); // []
|
|
29
|
+
|
|
30
|
+
fn('foo', 'pre-', '-post', function cb(err, lres) {
|
|
31
|
+
console.error('err:', err); // null
|
|
32
|
+
console.error('lres:', lres); // pre-FOO-LOADED-post
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## AST Definition
|
|
38
|
+
|
|
39
|
+
The abstract syntax tree or AST provided by React represents the data necessary to define the flow. By abstracting this from the DSL, it allows new skins or interfaces to be developed without need to change the core engine.
|
|
40
|
+
|
|
41
|
+
The AST is normally created at parse time when the React main function is called (or one of the alternate DSL's is called). This can be done a module load time such that after loading the React defined flow function's AST is generated and ready to process eliminating parsing and validation overhead when it is invoked in the future. This has the added advantage that since validation has also been performed that additional syntax issues or incomplete flow defintion errors can be caught quickly.
|
|
42
|
+
|
|
43
|
+
After the flow function has been created, you can review the generated AST for a function by accessing the ast.
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
var react = require('react');
|
|
47
|
+
var fn = react('my-flow-name', 'paramName1, paramName2, cb -> err, outParamName1, outParamName2',
|
|
48
|
+
functionRefOrMethodStr, 'paramName1, cb -> err, outParamName2', // async cb task
|
|
49
|
+
functionRefOrMethodStr2, 'paramName2, paramName1 -> outParamName1' // sync task
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
console.error(fn.ast); // output the generated AST
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The AST contains the following pieces:
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
var ast = {
|
|
59
|
+
name: flowName,
|
|
60
|
+
inParams: [],
|
|
61
|
+
tasks: [],
|
|
62
|
+
outTask: {},
|
|
63
|
+
locals: {}
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
- **name** - string - represents the name of the flow or function that will be created. React will use the name when generating events so you can monitor progress and performance and also when errors occur.
|
|
67
|
+
- **inParams** - array of strings - the flow input parameter names (excluding the callback param)
|
|
68
|
+
- **tasks** - array of task defintion objects - each containing:
|
|
69
|
+
- **f** - function reference or method string - async or sync function to be used for this task
|
|
70
|
+
- **a** - array of input parameter names (excluding the callback param)
|
|
71
|
+
- **out** - array of output parameter names (excluding the err parame)
|
|
72
|
+
- **type** - type of function determining how function is invoked and its output style - one of: ('cb', 'ret', 'promise', 'when')
|
|
73
|
+
- **name** - string - unique name for each task provided or generated by React
|
|
74
|
+
- **outTask** - task definition object specifying the flow's output style and parameters containing:
|
|
75
|
+
- **f** - will contain reference to the callback function at runtime
|
|
76
|
+
- **a** - parameters being passed as output from the flow
|
|
77
|
+
- **locals** - object provided which contains additional values that will become part of the React variable space like input parameters but can be defined in advance at flow definition. This can be used to provide functions and objects to React enabling string based DSL's.
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Plugins (optional requires which turn on additional functionality)
|
|
81
|
+
|
|
82
|
+
Additional functionality which is not enabled by default but available by requiring additional modules.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
<a name="LogEvents"/>
|
|
86
|
+
### LogEvents - log react progress to stderr
|
|
87
|
+
|
|
88
|
+
For convenience in debugging or in monitoring flow and performance, React has a built-in plugin for easily logging progress to stderr which is loaded and activated calling the react method `logEvents`. It can be specified to log globally for all react functions or only for particular react functions. You also may optionally listen to select events rather than all flow and task events.
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
var react = require('react');
|
|
92
|
+
react.logEvents(); // turn on flow and task logging for all react functions
|
|
93
|
+
|
|
94
|
+
// OR
|
|
95
|
+
|
|
96
|
+
react.logEvents(myReactFn); // turn on flow and task logging for a specific function, repeat as needed
|
|
97
|
+
react.logEvents(myReactFn).logEvents(myReactFn2); // can also chain
|
|
98
|
+
|
|
99
|
+
// Both methods can also take an optional event wildcard to specify what you want to listen to
|
|
100
|
+
|
|
101
|
+
react.logEvents('flow.*'); // turn on flow logging for all react functions
|
|
102
|
+
react.logEvents(myReactFn, 'task.*'); // turn on task logging for myReactFn
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Available Events that can be logged:
|
|
106
|
+
|
|
107
|
+
- flow.begin - flow execution has started (receives a flow env)
|
|
108
|
+
- flow.complete - flow execution has successfully completed (receives a flow env)
|
|
109
|
+
- flow.errored - flow execution has errored (receives a flow env)
|
|
110
|
+
- task.begin - task has started (receives task)
|
|
111
|
+
- task.complete - task has successfully complted (receives task)
|
|
112
|
+
- task.errored - task has errored (receives task)
|
|
113
|
+
|
|
114
|
+
### Automatic Promise Resolution for inputs
|
|
115
|
+
|
|
116
|
+
If you want to automatically resolve promises in React without having to manually call `when` or `then`, React provides a plugin which will detect the existence of a `then` method (indicating a promise) at runtime from any inputs to the flow and will internally create `when` tasks to resolve them before passing the values to other tasks. This built-in plugin is not loaded normally but is loaded by invoking the react method `resolvePromises`. External plugins like `react-deferred` also enable this but also provide additional promise integration. See https://github.com/jeffbski/react-deferred
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
var react = require('react');
|
|
120
|
+
react.resolvePromises(); // turn on automatic promise detection and resolution
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Track tasks - enable task tracking
|
|
124
|
+
|
|
125
|
+
Instead of only logging events to stderr (like LogEvents), this built-in plugin fires events that can be directly monitored. The LogEvent plugin uses this internally to get access to the metrics.
|
|
126
|
+
|
|
127
|
+
Enable this like the other built-in plugins using the method `trackTasks`
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
var react = require('react');
|
|
131
|
+
react.trackTasks(); // turn on flow and task tracking events
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Available Events that can be consumed
|
|
135
|
+
|
|
136
|
+
- flow.begin - flow execution has started (receives a flow env)
|
|
137
|
+
- flow.complete - flow execution has successfully completed (receives a flow env)
|
|
138
|
+
- flow.errored - flow execution has errored (receives a flow env)
|
|
139
|
+
- task.begin - task has started (receives task)
|
|
140
|
+
- task.complete - task has successfully complted (receives task)
|
|
141
|
+
- task.errored - task has errored (receives task)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
### EventCollector - simple event accumulator for debug use
|
|
145
|
+
|
|
146
|
+
When developing or debugging it is often useful to accumulate events and then interrogate them to verify operation, especially in testing.
|
|
147
|
+
|
|
148
|
+
To make this easier to accomplish, this plugin provides a simple event accumulator for development use. Note that this accumulator is designed for short term debug use, as it will continue to accumulate events and does not have any size restrictions, it should not be used in production since it will just continue to grow in size unless manually cleared.
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
var EventCollector = require('react/lib/event-collector);
|
|
152
|
+
var collector = new EventCollector();
|
|
153
|
+
|
|
154
|
+
collector.capture(); // capture all flow and task events for all react flows
|
|
155
|
+
collector.capture('flow.*'); // capture all flow events for all react flows
|
|
156
|
+
collector.capture(flowFn, 'task.*'); // capture task events on a flow
|
|
157
|
+
collector.capture(flowFn, 'flow.*'); // add capture flow events on a flow
|
|
158
|
+
|
|
159
|
+
var events = collector.list(); // retrieve the list of events
|
|
160
|
+
collector.clear(); // clear the list of events;
|
|
161
|
+
```
|