react 0.5.0 → 0.6.1

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.
Files changed (92) hide show
  1. package/.travis.yml +4 -0
  2. package/Jakefile.js +39 -0
  3. package/README.md +18 -26
  4. package/browser-test/dist.html +89 -0
  5. package/browser-test/index.html +85 -0
  6. package/browser-test/min.html +89 -0
  7. package/dist/react.js +3093 -0
  8. package/dist/react.min.js +1 -0
  9. package/doc/advanced.md +7 -2
  10. package/doc/simple.dot +25 -0
  11. package/doc/simple.png +0 -0
  12. package/examples/{default1.js → longer-example.js} +0 -0
  13. package/examples/{default-simple.js → simple.js} +0 -0
  14. package/examples/{ast1.js → using-ast-directly.js} +4 -0
  15. package/examples/{default-events1.js → using-events1.js} +1 -1
  16. package/examples/{default-log-events.js → using-log-events.js} +0 -0
  17. package/lib/base-task.js +116 -110
  18. package/lib/cb-task.js +71 -67
  19. package/lib/core.js +118 -107
  20. package/lib/dsl.js +120 -115
  21. package/lib/error.js +44 -36
  22. package/lib/event-collector.js +69 -53
  23. package/lib/event-manager.js +69 -55
  24. package/lib/eventemitter.js +20 -0
  25. package/lib/finalcb-first-task.js +56 -53
  26. package/lib/finalcb-task.js +55 -51
  27. package/lib/id.js +18 -6
  28. package/lib/input-parser.js +49 -41
  29. package/lib/log-events.js +79 -73
  30. package/lib/parse.js +34 -25
  31. package/lib/promise-resolve.js +42 -27
  32. package/lib/promise-task.js +78 -74
  33. package/lib/react.js +59 -0
  34. package/lib/ret-task.js +59 -55
  35. package/lib/sprintf.js +18 -0
  36. package/lib/status.js +11 -2
  37. package/lib/task.js +216 -213
  38. package/lib/track-tasks.js +72 -58
  39. package/lib/validate.js +136 -136
  40. package/lib/vcon.js +78 -69
  41. package/lib/when-task.js +69 -65
  42. package/package.json +10 -9
  43. package/src/dist.build.requirejs +20 -0
  44. package/test/ast.mocha.js +136 -0
  45. package/test/cb-task.mocha.js +220 -0
  46. package/test/core-deferred.mocha.js +143 -0
  47. package/test/core-when.mocha.js +96 -0
  48. package/test/core.mocha.js +589 -0
  49. package/test/dsl.mocha.js +350 -0
  50. package/test/event-manager.mocha.js +119 -0
  51. package/test/exec-options.mocha.js +48 -0
  52. package/test/finalcb-task.mocha.js +58 -0
  53. package/test/input-parser.mocha.js +86 -0
  54. package/test/mocha.opts +2 -0
  55. package/test/module-use.mocha.js +147 -0
  56. package/test/promise-auto-resolve.mocha.js +68 -0
  57. package/test/ret-task.mocha.js +220 -0
  58. package/test/task.mocha.js +42 -0
  59. package/test/validate-cb-task.mocha.js +100 -0
  60. package/test/validate-ret-task.mocha.js +110 -0
  61. package/test/validate.mocha.js +324 -0
  62. package/test/vcon.mocha.js +193 -0
  63. package/vendor/chai/chai.js +2038 -0
  64. package/vendor/jquery/jquery-1.7.1.js +9266 -0
  65. package/vendor/jquery/jquery-1.7.1.min.js +4 -0
  66. package/vendor/mocha/mocha.css +135 -0
  67. package/vendor/mocha/mocha.js +3589 -0
  68. package/vendor/node/util.js +531 -0
  69. package/vendor/requirejs/require.js +2053 -0
  70. package/vendor/requirejs/require.min.js +33 -0
  71. package/doc/default-simple.dot +0 -19
  72. package/doc/default-simple.dot.png +0 -0
  73. package/react.js +0 -40
  74. package/test/ast.test.js +0 -69
  75. package/test/cb-task.test.js +0 -197
  76. package/test/core-deferred.test.js +0 -134
  77. package/test/core-promised.test.js +0 -132
  78. package/test/core-when.test.js +0 -84
  79. package/test/core.test.js +0 -568
  80. package/test/dsl.test.js +0 -330
  81. package/test/event-manager.test.js +0 -102
  82. package/test/exec-options.test.js +0 -33
  83. package/test/finalcb-task.test.js +0 -38
  84. package/test/input-parser.test.js +0 -66
  85. package/test/module-use.test.js +0 -134
  86. package/test/promise-auto-resolve.test.js +0 -52
  87. package/test/ret-task.test.js +0 -199
  88. package/test/task.test.js +0 -21
  89. package/test/validate-cb-task.test.js +0 -74
  90. package/test/validate-ret-task.test.js +0 -83
  91. package/test/validate.test.js +0 -295
  92. package/test/vcon.test.js +0 -173
package/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: node_js
2
+ node_js:
3
+ - 0.6
4
+ - 0.7
package/Jakefile.js ADDED
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+ /*global fail:false, complete:false, desc:false, jake:false, namespace:false, task:false */
3
+
4
+ var path = require('path');
5
+ var child_process = require('child_process');
6
+
7
+ var REQUIREJS = path.resolve(__dirname, 'node_modules/requirejs/bin/r.js');
8
+
9
+ desc('Default - build all dist versions');
10
+ task('default', ['dist-all'], function (params) { });
11
+
12
+
13
+ function runRequireJs(argStr) {
14
+ var proc = child_process.spawn(REQUIREJS, argStr.split(' '));
15
+ proc.stdout.pipe(process.stdout);
16
+ proc.stderr.pipe(process.stderr);
17
+ proc.on('exit', function (exitCode) {
18
+ if (exitCode !== 0) {
19
+ console.error('Child process exited with error code: %s, exiting', exitCode);
20
+ process.exit(exitCode);
21
+ }
22
+ });
23
+ }
24
+
25
+ desc('Create core single-file developer dist');
26
+ task('dist', function () {
27
+ runRequireJs('-o src/dist.build.requirejs out=dist/react.js');
28
+ });
29
+
30
+ desc('Create full single-file minified dist');
31
+ task('min', function () {
32
+ runRequireJs('-o src/dist.build.requirejs out=dist/react.min.js optimize=uglify');
33
+ });
34
+
35
+ desc('Create all dist versions');
36
+ task('dist-all', ['dist', 'min']);
37
+
38
+
39
+
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # React.js
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/jeffbski/react.png?branch=master)](http://travis-ci.org/jeffbski/react)
4
+
3
5
  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.
4
6
 
5
7
  This async flow control module is initially designed to work with Node.js but is planned to be extended to browser and other environments.
@@ -35,7 +37,7 @@ It takes inspiration from several projects including:
35
37
  - object instance method calls
36
38
  - class method calls
37
39
  - selectFirst flow where the first task that returns defined, non-null value is used
38
- - promise style functions - also automatic resolution of promise inputs (optional require('react/promise-resolve');)
40
+ - promise style functions - also automatic resolution of promise inputs (optionally loaded with `react.resolvePromises();`)
39
41
  - 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
40
42
  - (planned) iteration on arrays, streams, sockets
41
43
  - (planned) event emitter integration
@@ -128,9 +130,11 @@ foobar.loadRender('foo.txt', 'bar.txt', 'BBB', function (err, renderedOut) {
128
130
  ```
129
131
 
130
132
  Below is a graph of how the dependencies are mapped by React which
131
- also indicates how the tasks will be executed
133
+ also indicates how the tasks will be executed. This was generated by the
134
+ react plugin [react-graphviz](https://github.com/jeffbski/react-graphviz)
135
+ which you can use to also graph your flows.
132
136
 
133
- ![default-simple.dot.png](https://github.com/jeffbski/react/raw/master/doc/default-simple.dot.png)
137
+ ![simple.png](https://github.com/jeffbski/react/raw/master/doc/simple.png)
134
138
 
135
139
 
136
140
 
@@ -182,7 +186,12 @@ See the [Advanced React](https://github.com/jeffbski/react/blob/master/doc/advan
182
186
 
183
187
  ## Status
184
188
 
185
- - 2012-01-18 - Remove old DSL interfaces, improve plugin loading (v0.5.0)
189
+ - 2012-04-25 - Browser compatibility issue with process check, (v0.6.1)
190
+ - 2012-04-05 - Remove dependency on sprint, use util.format
191
+ - 2012-03-28 - Make react AMD-enabled and compatible with ES5 browsers and node.js, provide single file dist and min, add browser tests (v0.6.0)
192
+ - 2012-03-24 - Add Travis-CI, remove promised-io since failing to install in travis-ci for node 0.6/0.7, switch from tap to mocha/chai
193
+ - 2012-03-12 - Pass ast.define events to process (v0.5.2)
194
+ - 2012-01-18 - Remove old DSL interfaces, improve plugin loading, log flow name with task name, ast.defined event, test with node 0.7.0 (v0.5.1)
186
195
  - 2012-01-17 - Additional documentation (v0.3.5)
187
196
  - 2012-01-16 - Refine events and create logging plugin (v0.3.3)
188
197
  - 2012-01-13 - Add promise tasks, promise resolution, refactor alternate DSL interfaces as optional requires (v0.3.0)
@@ -193,29 +202,12 @@ See the [Advanced React](https://github.com/jeffbski/react/blob/master/doc/advan
193
202
 
194
203
  ## Test Results
195
204
 
205
+ [![Build Status](https://secure.travis-ci.org/jeffbski/react.png?branch=master)](http://travis-ci.org/jeffbski/react)
206
+
196
207
  ```bash
197
- ok ast.test.js .................... 10/10
198
- ok cb-task.test.js ................ 31/31
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
204
- ok event-manager.test.js .......... 13/13
205
- ok exec-options.test.js ............. 3/3
206
- ok finalcb-task.test.js ............. 5/5
207
- ok input-parser.test.js ........... 15/15
208
- ok module-use.test.js ............. 24/24
209
- ok promise-auto-resolve.test.js ..... 4/4
210
- ok ret-task.test.js ............... 31/31
211
- ok task.test.js ..................... 1/1
212
- ok validate-cb-task.test.js ......... 6/6
213
- ok validate-ret-task.test.js ........ 7/7
214
- ok validate.test.js ............... 31/31
215
- ok vcon.test.js ................... 55/55
216
- total ........................... 457/457
217
-
218
- ok
208
+ mocha
209
+
210
+ 178 tests complete
219
211
  ```
220
212
 
221
213
  ## License
@@ -0,0 +1,89 @@
1
+ <html>
2
+ <head>
3
+ <meta charset="utf-8">
4
+ <title>Mocha Tests</title>
5
+ <link rel="stylesheet" href="../vendor/mocha/mocha.css" />
6
+ <script type="text/javascript" src="../vendor/jquery/jquery-1.7.1.js"></script>
7
+ <script type="text/javascript" src="../vendor/chai/chai.js"></script>
8
+ <script type="text/javascript" src="../vendor/mocha/mocha.js"></script>
9
+ <script>mocha.setup('qunit')</script>
10
+ <script src="../vendor/requirejs/require.min.js"></script>
11
+ <script type="text/javascript">
12
+ require.config({
13
+ baseUrl: "..",
14
+ packages: [
15
+ {
16
+ name: "react",
17
+ location: "dist",
18
+ main: "react"
19
+ }
20
+ ],
21
+ paths: {
22
+ jQuery: "vendor/jquery/jquery-1.7.1",
23
+ eventemitter2: "dist/react",
24
+ util: "dist/react",
25
+ "ensure-array": "dist/react",
26
+ test: "test"
27
+ }
28
+ });
29
+
30
+ require(["react", "jQuery"],
31
+ function (react, jquery) {
32
+
33
+ require(["react/sprintf", "react/validate", "react/task", "react/cb-task",
34
+ "react/vcon", "react/event-manager", "react/input-parser",
35
+ "react/base-task", "react/ret-task", "util"],
36
+ function (sprintf, validate, taskUtil, CbTask,
37
+ VContext, EventManager, inputParser,
38
+ BaseTask, RetTask, util) {
39
+ // save refs on window for these
40
+ window.react = react;
41
+ window.sprintf = sprintf;
42
+ window.util = util;
43
+ window.validate = validate;
44
+ window.taskUtil = taskUtil;
45
+ window.CbTask = CbTask;
46
+ window.VContext = VContext;
47
+ window.EventManager = EventManager;
48
+ window.inputParser = inputParser;
49
+ window.BaseTask = BaseTask;
50
+ window.RetTask = RetTask;
51
+ window.validate = validate;
52
+ window.Deferred = jQuery.Deferred;
53
+ window.Deferred.when = jQuery.when;
54
+ var testFiles = [
55
+ "test/ast.mocha",
56
+ "test/cb-task.mocha",
57
+ "test/core-deferred.mocha",
58
+ "test/core-when.mocha",
59
+ "test/core.mocha",
60
+ "test/dsl.mocha",
61
+ "test/event-manager.mocha",
62
+ "test/exec-options.mocha",
63
+ "test/finalcb-task.mocha",
64
+ "test/input-parser.mocha",
65
+ "test/module-use.mocha",
66
+ "test/promise-auto-resolve.mocha",
67
+ "test/ret-task.mocha",
68
+ "test/task.mocha",
69
+ "test/validate-cb-task.mocha",
70
+ "test/validate-ret-task.mocha",
71
+ "test/validate.mocha",
72
+ "test/vcon.mocha"
73
+ ];
74
+ require(testFiles, function () {
75
+ var runner = mocha.run().on('end', function () {
76
+ console.log('mocha finished');
77
+ jQuery("#mocha-result").text('completed');
78
+ });
79
+ //mocha.reporters.TAP(runner);
80
+ });
81
+ });
82
+ });
83
+ </script>
84
+ </head>
85
+ <body>
86
+ <div id="mocha"></div>
87
+ <div id="mocha-result"></div>
88
+ </body>
89
+ </html>
@@ -0,0 +1,85 @@
1
+ <html>
2
+ <head>
3
+ <meta charset="utf-8">
4
+ <title>Mocha Tests</title>
5
+ <link rel="stylesheet" href="../vendor/mocha/mocha.css" />
6
+ <script type="text/javascript" src="../vendor/jquery/jquery-1.7.1.js"></script>
7
+ <script type="text/javascript" src="../vendor/chai/chai.js"></script>
8
+ <script type="text/javascript" src="../vendor/mocha/mocha.js"></script>
9
+ <script>mocha.setup('qunit')</script>
10
+ <script src="../vendor/requirejs/require.min.js"></script>
11
+ <script type="text/javascript">
12
+ require.config({
13
+ baseUrl: "..",
14
+ packages: [
15
+ {
16
+ name: "react",
17
+ location: "lib",
18
+ main: "react"
19
+ }
20
+ ],
21
+ paths: {
22
+ jquery: "vendor/jquery/jquery-1.7.1",
23
+ eventemitter2: "node_modules/eventemitter2/lib/eventemitter2",
24
+ util: "vendor/node/util",
25
+ "ensure-array": "node_modules/ensure-array/ensure-array",
26
+ test: "test"
27
+ }
28
+ });
29
+
30
+ require(["react", "react/sprintf", "util", "react/validate", "react/task",
31
+ "react/cb-task", "react/vcon", "react/event-manager", "react/input-parser",
32
+ "react/base-task", "react/ret-task", "react/validate", "jquery"],
33
+ function (react, sprintf, util, validate, taskUtil,
34
+ CbTask, VContext, EventManager, inputParser,
35
+ BaseTask, RetTask, validate, jquery) {
36
+ // save refs on window for these
37
+ window.react = react;
38
+ window.sprintf = sprintf;
39
+ window.util = util;
40
+ window.validate = validate;
41
+ window.taskUtil = taskUtil;
42
+ window.CbTask = CbTask;
43
+ window.VContext = VContext;
44
+ window.EventManager = EventManager;
45
+ window.inputParser = inputParser;
46
+ window.BaseTask = BaseTask;
47
+ window.RetTask = RetTask;
48
+ window.validate = validate;
49
+ window.Deferred = jQuery.Deferred;
50
+ window.Deferred.when = jQuery.when;
51
+ var testFiles = [
52
+ "test/ast.mocha",
53
+ "test/cb-task.mocha",
54
+ "test/core-deferred.mocha",
55
+ "test/core-when.mocha",
56
+ "test/core.mocha",
57
+ "test/dsl.mocha",
58
+ "test/event-manager.mocha",
59
+ "test/exec-options.mocha",
60
+ "test/finalcb-task.mocha",
61
+ "test/input-parser.mocha",
62
+ "test/module-use.mocha",
63
+ "test/promise-auto-resolve.mocha",
64
+ "test/ret-task.mocha",
65
+ "test/task.mocha",
66
+ "test/validate-cb-task.mocha",
67
+ "test/validate-ret-task.mocha",
68
+ "test/validate.mocha",
69
+ "test/vcon.mocha"
70
+ ];
71
+ require(testFiles, function () {
72
+ var runner = mocha.run().on('end', function () {
73
+ console.log('mocha finished');
74
+ jQuery("#mocha-result").text('completed');
75
+ });
76
+ //mocha.reporters.TAP(runner);
77
+ });
78
+ });
79
+ </script>
80
+ </head>
81
+ <body>
82
+ <div id="mocha"></div>
83
+ <div id="mocha-result"></div>
84
+ </body>
85
+ </html>
@@ -0,0 +1,89 @@
1
+ <html>
2
+ <head>
3
+ <meta charset="utf-8">
4
+ <title>Mocha Tests</title>
5
+ <link rel="stylesheet" href="../vendor/mocha/mocha.css" />
6
+ <script type="text/javascript" src="../vendor/jquery/jquery-1.7.1.js"></script>
7
+ <script type="text/javascript" src="../vendor/chai/chai.js"></script>
8
+ <script type="text/javascript" src="../vendor/mocha/mocha.js"></script>
9
+ <script>mocha.setup('qunit')</script>
10
+ <script src="../vendor/requirejs/require.min.js"></script>
11
+ <script type="text/javascript">
12
+ require.config({
13
+ baseUrl: "..",
14
+ packages: [
15
+ {
16
+ name: "react",
17
+ location: "dist",
18
+ main: "react.min"
19
+ }
20
+ ],
21
+ paths: {
22
+ jQuery: "vendor/jquery/jquery-1.7.1.min",
23
+ eventemitter2: "dist/react.min",
24
+ util: "dist/react.min",
25
+ "ensure-array": "dist/react.min",
26
+ test: "test"
27
+ }
28
+ });
29
+
30
+ require(["react", "jQuery"],
31
+ function (react, jquery) {
32
+
33
+ require(["react/sprintf", "react/validate", "react/task", "react/cb-task",
34
+ "react/vcon", "react/event-manager", "react/input-parser",
35
+ "react/base-task", "react/ret-task", "util"],
36
+ function (sprintf, validate, taskUtil, CbTask,
37
+ VContext, EventManager, inputParser,
38
+ BaseTask, RetTask, util) {
39
+ // save refs on window for these
40
+ window.react = react;
41
+ window.sprintf = sprintf;
42
+ window.util = util;
43
+ window.validate = validate;
44
+ window.taskUtil = taskUtil;
45
+ window.CbTask = CbTask;
46
+ window.VContext = VContext;
47
+ window.EventManager = EventManager;
48
+ window.inputParser = inputParser;
49
+ window.BaseTask = BaseTask;
50
+ window.RetTask = RetTask;
51
+ window.validate = validate;
52
+ window.Deferred = jQuery.Deferred;
53
+ window.Deferred.when = jQuery.when;
54
+ var testFiles = [
55
+ "test/ast.mocha",
56
+ "test/cb-task.mocha",
57
+ "test/core-deferred.mocha",
58
+ "test/core-when.mocha",
59
+ "test/core.mocha",
60
+ "test/dsl.mocha",
61
+ "test/event-manager.mocha",
62
+ "test/exec-options.mocha",
63
+ "test/finalcb-task.mocha",
64
+ "test/input-parser.mocha",
65
+ "test/module-use.mocha",
66
+ "test/promise-auto-resolve.mocha",
67
+ "test/ret-task.mocha",
68
+ "test/task.mocha",
69
+ "test/validate-cb-task.mocha",
70
+ "test/validate-ret-task.mocha",
71
+ "test/validate.mocha",
72
+ "test/vcon.mocha"
73
+ ];
74
+ require(testFiles, function () {
75
+ var runner = mocha.run().on('end', function () {
76
+ console.log('mocha finished');
77
+ jQuery("#mocha-result").text('completed');
78
+ });
79
+ //mocha.reporters.TAP(runner);
80
+ });
81
+ });
82
+ });
83
+ </script>
84
+ </head>
85
+ <body>
86
+ <div id="mocha"></div>
87
+ <div id="mocha-result"></div>
88
+ </body>
89
+ </html>
package/dist/react.js ADDED
@@ -0,0 +1,3093 @@
1
+
2
+ ;!function(exports, undefined) {
3
+
4
+ var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
5
+ return Object.prototype.toString.call(obj) === "[object Array]";
6
+ };
7
+ var defaultMaxListeners = 10;
8
+
9
+ function init() {
10
+ this._events = new Object;
11
+ }
12
+
13
+ function configure(conf) {
14
+ if (conf) {
15
+ conf.delimiter && (this.delimiter = conf.delimiter);
16
+ conf.wildcard && (this.wildcard = conf.wildcard);
17
+ if (this.wildcard) {
18
+ this.listenerTree = new Object;
19
+ }
20
+ }
21
+ }
22
+
23
+ function EventEmitter(conf) {
24
+ this._events = new Object;
25
+ configure.call(this, conf);
26
+ }
27
+
28
+ //
29
+ // Attention, function return type now is array, always !
30
+ // It has zero elements if no any matches found and one or more
31
+ // elements (leafs) if there are matches
32
+ //
33
+ function searchListenerTree(handlers, type, tree, i) {
34
+ if (!tree) {
35
+ return [];
36
+ }
37
+ var listeners=[], leaf, len, branch, xTree, xxTree, isolatedBranch, endReached,
38
+ typeLength = type.length, currentType = type[i], nextType = type[i+1];
39
+ if (i === typeLength && tree._listeners) {
40
+ //
41
+ // If at the end of the event(s) list and the tree has listeners
42
+ // invoke those listeners.
43
+ //
44
+ if (typeof tree._listeners === 'function') {
45
+ handlers && handlers.push(tree._listeners);
46
+ return [tree];
47
+ } else {
48
+ for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
49
+ handlers && handlers.push(tree._listeners[leaf]);
50
+ }
51
+ return [tree];
52
+ }
53
+ }
54
+
55
+ if ((currentType === '*' || currentType === '**') || tree[currentType]) {
56
+ //
57
+ // If the event emitted is '*' at this part
58
+ // or there is a concrete match at this patch
59
+ //
60
+ if (currentType === '*') {
61
+ for (branch in tree) {
62
+ if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
63
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+1));
64
+ }
65
+ }
66
+ return listeners;
67
+ } else if(currentType === '**') {
68
+ endReached = (i+1 === typeLength || (i+2 === typeLength && nextType === '*'));
69
+ if(endReached && tree._listeners) {
70
+ // The next element has a _listeners, add it to the handlers.
71
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree, typeLength));
72
+ }
73
+
74
+ for (branch in tree) {
75
+ if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
76
+ if(branch === '*' || branch === '**') {
77
+ if(tree[branch]._listeners && !endReached) {
78
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], typeLength));
79
+ }
80
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
81
+ } else if(branch === nextType) {
82
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+2));
83
+ } else {
84
+ // No match on this one, shift into the tree but not in the type array.
85
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
86
+ }
87
+ }
88
+ }
89
+ return listeners;
90
+ }
91
+
92
+ listeners = listeners.concat(searchListenerTree(handlers, type, tree[currentType], i+1));
93
+ }
94
+
95
+ xTree = tree['*'];
96
+ if (xTree) {
97
+ //
98
+ // If the listener tree will allow any match for this part,
99
+ // then recursively explore all branches of the tree
100
+ //
101
+ searchListenerTree(handlers, type, xTree, i+1);
102
+ }
103
+
104
+ xxTree = tree['**'];
105
+ if(xxTree) {
106
+ if(i < typeLength) {
107
+ if(xxTree._listeners) {
108
+ // If we have a listener on a '**', it will catch all, so add its handler.
109
+ searchListenerTree(handlers, type, xxTree, typeLength);
110
+ }
111
+
112
+ // Build arrays of matching next branches and others.
113
+ for(branch in xxTree) {
114
+ if(branch !== '_listeners' && xxTree.hasOwnProperty(branch)) {
115
+ if(branch === nextType) {
116
+ // We know the next element will match, so jump twice.
117
+ searchListenerTree(handlers, type, xxTree[branch], i+2);
118
+ } else if(branch === currentType) {
119
+ // Current node matches, move into the tree.
120
+ searchListenerTree(handlers, type, xxTree[branch], i+1);
121
+ } else {
122
+ isolatedBranch = {};
123
+ isolatedBranch[branch] = xxTree[branch];
124
+ searchListenerTree(handlers, type, { '**': isolatedBranch }, i+1);
125
+ }
126
+ }
127
+ }
128
+ } else if(xxTree._listeners) {
129
+ // We have reached the end and still on a '**'
130
+ searchListenerTree(handlers, type, xxTree, typeLength);
131
+ } else if(xxTree['*'] && xxTree['*']._listeners) {
132
+ searchListenerTree(handlers, type, xxTree['*'], typeLength);
133
+ }
134
+ }
135
+
136
+ return listeners;
137
+ }
138
+
139
+ function growListenerTree(type, listener) {
140
+
141
+ type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
142
+
143
+ //
144
+ // Looks for two consecutive '**', if so, don't add the event at all.
145
+ //
146
+ for(var i = 0, len = type.length; i+1 < len; i++) {
147
+ if(type[i] === '**' && type[i+1] === '**') {
148
+ return;
149
+ }
150
+ }
151
+
152
+ var tree = this.listenerTree;
153
+ var name = type.shift();
154
+
155
+ while (name) {
156
+
157
+ if (!tree[name]) {
158
+ tree[name] = new Object;
159
+ }
160
+
161
+ tree = tree[name];
162
+
163
+ if (type.length === 0) {
164
+
165
+ if (!tree._listeners) {
166
+ tree._listeners = listener;
167
+ }
168
+ else if(typeof tree._listeners === 'function') {
169
+ tree._listeners = [tree._listeners, listener];
170
+ }
171
+ else if (isArray(tree._listeners)) {
172
+
173
+ tree._listeners.push(listener);
174
+
175
+ if (!tree._listeners.warned) {
176
+
177
+ var m = defaultMaxListeners;
178
+
179
+ if (typeof this._events.maxListeners !== 'undefined') {
180
+ m = this._events.maxListeners;
181
+ }
182
+
183
+ if (m > 0 && tree._listeners.length > m) {
184
+
185
+ tree._listeners.warned = true;
186
+ console.error('(node) warning: possible EventEmitter memory ' +
187
+ 'leak detected. %d listeners added. ' +
188
+ 'Use emitter.setMaxListeners() to increase limit.',
189
+ tree._listeners.length);
190
+ console.trace();
191
+ }
192
+ }
193
+ }
194
+ return true;
195
+ }
196
+ name = type.shift();
197
+ }
198
+ return true;
199
+ };
200
+
201
+ // By default EventEmitters will print a warning if more than
202
+ // 10 listeners are added to it. This is a useful default which
203
+ // helps finding memory leaks.
204
+ //
205
+ // Obviously not all Emitters should be limited to 10. This function allows
206
+ // that to be increased. Set to zero for unlimited.
207
+
208
+ EventEmitter.prototype.delimiter = '.';
209
+
210
+ EventEmitter.prototype.setMaxListeners = function(n) {
211
+ this._events || init.call(this);
212
+ this._events.maxListeners = n;
213
+ };
214
+
215
+ EventEmitter.prototype.event = '';
216
+
217
+ EventEmitter.prototype.once = function(event, fn) {
218
+ this.many(event, 1, fn);
219
+ return this;
220
+ };
221
+
222
+ EventEmitter.prototype.many = function(event, ttl, fn) {
223
+ var self = this;
224
+
225
+ if (typeof fn !== 'function') {
226
+ throw new Error('many only accepts instances of Function');
227
+ }
228
+
229
+ function listener() {
230
+ if (--ttl === 0) {
231
+ self.off(event, listener);
232
+ }
233
+ fn.apply(this, arguments);
234
+ };
235
+
236
+ listener._origin = fn;
237
+
238
+ this.on(event, listener);
239
+
240
+ return self;
241
+ };
242
+
243
+ EventEmitter.prototype.emit = function() {
244
+ this._events || init.call(this);
245
+
246
+ var type = arguments[0];
247
+
248
+ if (type === 'newListener') {
249
+ if (!this._events.newListener) { return false; }
250
+ }
251
+
252
+ // Loop through the *_all* functions and invoke them.
253
+ if (this._all) {
254
+ var l = arguments.length;
255
+ var args = new Array(l - 1);
256
+ for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
257
+ for (i = 0, l = this._all.length; i < l; i++) {
258
+ this.event = type;
259
+ this._all[i].apply(this, args);
260
+ }
261
+ }
262
+
263
+ // If there is no 'error' event listener then throw.
264
+ if (type === 'error') {
265
+
266
+ if (!this._all &&
267
+ !this._events.error &&
268
+ !(this.wildcard && this.listenerTree.error)) {
269
+
270
+ if (arguments[1] instanceof Error) {
271
+ throw arguments[1]; // Unhandled 'error' event
272
+ } else {
273
+ throw new Error("Uncaught, unspecified 'error' event.");
274
+ }
275
+ return false;
276
+ }
277
+ }
278
+
279
+ var handler;
280
+
281
+ if(this.wildcard) {
282
+ handler = [];
283
+ var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
284
+ searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
285
+ }
286
+ else {
287
+ handler = this._events[type];
288
+ }
289
+
290
+ if (typeof handler === 'function') {
291
+ this.event = type;
292
+ if (arguments.length === 1) {
293
+ handler.call(this);
294
+ }
295
+ else if (arguments.length > 1)
296
+ switch (arguments.length) {
297
+ case 2:
298
+ handler.call(this, arguments[1]);
299
+ break;
300
+ case 3:
301
+ handler.call(this, arguments[1], arguments[2]);
302
+ break;
303
+ // slower
304
+ default:
305
+ var l = arguments.length;
306
+ var args = new Array(l - 1);
307
+ for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
308
+ handler.apply(this, args);
309
+ }
310
+ return true;
311
+ }
312
+ else if (handler) {
313
+ var l = arguments.length;
314
+ var args = new Array(l - 1);
315
+ for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
316
+
317
+ var listeners = handler.slice();
318
+ for (var i = 0, l = listeners.length; i < l; i++) {
319
+ this.event = type;
320
+ listeners[i].apply(this, args);
321
+ }
322
+ return (listeners.length > 0) || this._all;
323
+ }
324
+ else {
325
+ return this._all;
326
+ }
327
+
328
+ };
329
+
330
+ EventEmitter.prototype.on = function(type, listener) {
331
+
332
+ if (typeof type === 'function') {
333
+ this.onAny(type);
334
+ return this;
335
+ }
336
+
337
+ if (typeof listener !== 'function') {
338
+ throw new Error('on only accepts instances of Function');
339
+ }
340
+ this._events || init.call(this);
341
+
342
+ // To avoid recursion in the case that type == "newListeners"! Before
343
+ // adding it to the listeners, first emit "newListeners".
344
+ this.emit('newListener', type, listener);
345
+
346
+ if(this.wildcard) {
347
+ growListenerTree.call(this, type, listener);
348
+ return this;
349
+ }
350
+
351
+ if (!this._events[type]) {
352
+ // Optimize the case of one listener. Don't need the extra array object.
353
+ this._events[type] = listener;
354
+ }
355
+ else if(typeof this._events[type] === 'function') {
356
+ // Adding the second element, need to change to array.
357
+ this._events[type] = [this._events[type], listener];
358
+ }
359
+ else if (isArray(this._events[type])) {
360
+ // If we've already got an array, just append.
361
+ this._events[type].push(listener);
362
+
363
+ // Check for listener leak
364
+ if (!this._events[type].warned) {
365
+
366
+ var m = defaultMaxListeners;
367
+
368
+ if (typeof this._events.maxListeners !== 'undefined') {
369
+ m = this._events.maxListeners;
370
+ }
371
+
372
+ if (m > 0 && this._events[type].length > m) {
373
+
374
+ this._events[type].warned = true;
375
+ console.error('(node) warning: possible EventEmitter memory ' +
376
+ 'leak detected. %d listeners added. ' +
377
+ 'Use emitter.setMaxListeners() to increase limit.',
378
+ this._events[type].length);
379
+ console.trace();
380
+ }
381
+ }
382
+ }
383
+ return this;
384
+ };
385
+
386
+ EventEmitter.prototype.onAny = function(fn) {
387
+
388
+ if(!this._all) {
389
+ this._all = [];
390
+ }
391
+
392
+ if (typeof fn !== 'function') {
393
+ throw new Error('onAny only accepts instances of Function');
394
+ }
395
+
396
+ // Add the function to the event listener collection.
397
+ this._all.push(fn);
398
+ return this;
399
+ };
400
+
401
+ EventEmitter.prototype.addListener = EventEmitter.prototype.on;
402
+
403
+ EventEmitter.prototype.off = function(type, listener) {
404
+ if (typeof listener !== 'function') {
405
+ throw new Error('removeListener only takes instances of Function');
406
+ }
407
+
408
+ var handlers,leafs=[];
409
+
410
+ if(this.wildcard) {
411
+ var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
412
+ leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
413
+ }
414
+ else {
415
+ // does not use listeners(), so no side effect of creating _events[type]
416
+ if (!this._events[type]) return this;
417
+ handlers = this._events[type];
418
+ leafs.push({_listeners:handlers});
419
+ }
420
+
421
+ for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
422
+ var leaf = leafs[iLeaf];
423
+ handlers = leaf._listeners;
424
+ if (isArray(handlers)) {
425
+
426
+ var position = -1;
427
+
428
+ for (var i = 0, length = handlers.length; i < length; i++) {
429
+ if (handlers[i] === listener ||
430
+ (handlers[i].listener && handlers[i].listener === listener) ||
431
+ (handlers[i]._origin && handlers[i]._origin === listener)) {
432
+ position = i;
433
+ break;
434
+ }
435
+ }
436
+
437
+ if (position < 0) {
438
+ return this;
439
+ }
440
+
441
+ if(this.wildcard) {
442
+ leaf._listeners.splice(position, 1)
443
+ }
444
+ else {
445
+ this._events[type].splice(position, 1);
446
+ }
447
+
448
+ if (handlers.length === 0) {
449
+ if(this.wildcard) {
450
+ delete leaf._listeners;
451
+ }
452
+ else {
453
+ delete this._events[type];
454
+ }
455
+ }
456
+ }
457
+ else if (handlers === listener ||
458
+ (handlers.listener && handlers.listener === listener) ||
459
+ (handlers._origin && handlers._origin === listener)) {
460
+ if(this.wildcard) {
461
+ delete leaf._listeners;
462
+ }
463
+ else {
464
+ delete this._events[type];
465
+ }
466
+ }
467
+ }
468
+
469
+ return this;
470
+ };
471
+
472
+ EventEmitter.prototype.offAny = function(fn) {
473
+ var i = 0, l = 0, fns;
474
+ if (fn && this._all && this._all.length > 0) {
475
+ fns = this._all;
476
+ for(i = 0, l = fns.length; i < l; i++) {
477
+ if(fn === fns[i]) {
478
+ fns.splice(i, 1);
479
+ return this;
480
+ }
481
+ }
482
+ } else {
483
+ this._all = [];
484
+ }
485
+ return this;
486
+ };
487
+
488
+ EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
489
+
490
+ EventEmitter.prototype.removeAllListeners = function(type) {
491
+ if (arguments.length === 0) {
492
+ !this._events || init.call(this);
493
+ return this;
494
+ }
495
+
496
+ if(this.wildcard) {
497
+ var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
498
+ var leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
499
+
500
+ for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
501
+ var leaf = leafs[iLeaf];
502
+ leaf._listeners = null;
503
+ }
504
+ }
505
+ else {
506
+ if (!this._events[type]) return this;
507
+ this._events[type] = null;
508
+ }
509
+ return this;
510
+ };
511
+
512
+ EventEmitter.prototype.listeners = function(type) {
513
+ if(this.wildcard) {
514
+ var handlers = [];
515
+ var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
516
+ searchListenerTree.call(this, handlers, ns, this.listenerTree, 0);
517
+ return handlers;
518
+ }
519
+
520
+ this._events || init.call(this);
521
+
522
+ if (!this._events[type]) this._events[type] = [];
523
+ if (!isArray(this._events[type])) {
524
+ this._events[type] = [this._events[type]];
525
+ }
526
+ return this._events[type];
527
+ };
528
+
529
+ EventEmitter.prototype.listenersAny = function() {
530
+
531
+ if(this._all) {
532
+ return this._all;
533
+ }
534
+ else {
535
+ return [];
536
+ }
537
+
538
+ };
539
+
540
+ if (typeof define === 'function' && define.amd) {
541
+ define('eventemitter2',[],function() {
542
+ return EventEmitter;
543
+ });
544
+ } else {
545
+ exports.EventEmitter2 = EventEmitter;
546
+ }
547
+
548
+ }(typeof process !== 'undefined' && typeof process.title !== 'undefined' && typeof exports !== 'undefined' ? exports : window);
549
+
550
+
551
+ /*global define:true EventEmitter2:true */
552
+
553
+
554
+
555
+ define('react/eventemitter',['eventemitter2'], function (EventEmitterMod) {
556
+
557
+ /**
558
+ Abstract the details of getting an EventEmitter
559
+ */
560
+
561
+ // EventEmitter doesn't return itself in browser so need to get the global
562
+ // EventEmitter api changed, so accomodate which ever version is available
563
+ var EventEmitter = (EventEmitterMod) ?
564
+ ((EventEmitterMod.EventEmitter2) ? EventEmitterMod.EventEmitter2 : EventEmitterMod) : EventEmitter2;
565
+ return EventEmitter;
566
+
567
+ });
568
+ /*global define:true */
569
+
570
+
571
+
572
+ // Copyright Joyent, Inc. and other Node contributors.
573
+ //
574
+ // Permission is hereby granted, free of charge, to any person obtaining a
575
+ // copy of this software and associated documentation files (the
576
+ // "Software"), to deal in the Software without restriction, including
577
+ // without limitation the rights to use, copy, modify, merge, publish,
578
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
579
+ // persons to whom the Software is furnished to do so, subject to the
580
+ // following conditions:
581
+ //
582
+ // The above copyright notice and this permission notice shall be included
583
+ // in all copies or substantial portions of the Software.
584
+ //
585
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
586
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
587
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
588
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
589
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
590
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
591
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
592
+
593
+ define('util',['require','exports','module'],function (require, exports, module) {
594
+ /*jshint white:false */
595
+
596
+ var formatRegExp = /%[sdj%]/g;
597
+ exports.format = function(f) {
598
+ if (typeof f !== 'string') {
599
+ var objects = [];
600
+ for (var j = 0; j < arguments.length; j++) {
601
+ objects.push(inspect(arguments[j]));
602
+ }
603
+ return objects.join(' ');
604
+ }
605
+
606
+ var i = 1;
607
+ var args = arguments;
608
+ var len = args.length;
609
+ var str = String(f).replace(formatRegExp, function(x) {
610
+ if (x === '%%') return '%';
611
+ if (i >= len) return x;
612
+ switch (x) {
613
+ case '%s': return String(args[i++]);
614
+ case '%d': return Number(args[i++]);
615
+ case '%j': return JSON.stringify(args[i++]);
616
+ default:
617
+ return x;
618
+ }
619
+ });
620
+ for (var x = args[i]; i < len; x = args[++i]) {
621
+ if (x === null || typeof x !== 'object') {
622
+ str += ' ' + x;
623
+ } else {
624
+ str += ' ' + inspect(x);
625
+ }
626
+ }
627
+ return str;
628
+ };
629
+
630
+
631
+ exports.print = function() {
632
+ for (var i = 0, len = arguments.length; i < len; ++i) {
633
+ process.stdout.write(String(arguments[i]));
634
+ }
635
+ };
636
+
637
+
638
+ exports.puts = function() {
639
+ for (var i = 0, len = arguments.length; i < len; ++i) {
640
+ process.stdout.write(arguments[i] + '\n');
641
+ }
642
+ };
643
+
644
+
645
+ exports.debug = function(x) {
646
+ process.stderr.write('DEBUG: ' + x + '\n');
647
+ };
648
+
649
+
650
+ var error = exports.error = function(x) {
651
+ for (var i = 0, len = arguments.length; i < len; ++i) {
652
+ process.stderr.write(arguments[i] + '\n');
653
+ }
654
+ };
655
+
656
+
657
+ /**
658
+ * Echos the value of a value. Trys to print the value out
659
+ * in the best way possible given the different types.
660
+ *
661
+ * @param {Object} obj The object to print out.
662
+ * @param {Boolean} showHidden Flag that shows hidden (not enumerable)
663
+ * properties of objects.
664
+ * @param {Number} depth Depth in which to descend in object. Default is 2.
665
+ * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
666
+ * output. Default is false (no coloring).
667
+ */
668
+ function inspect(obj, showHidden, depth, colors) {
669
+ var ctx = {
670
+ showHidden: showHidden,
671
+ seen: [],
672
+ stylize: colors ? stylizeWithColor : stylizeNoColor
673
+ };
674
+ return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth));
675
+ }
676
+ exports.inspect = inspect;
677
+
678
+
679
+ // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
680
+ var colors = {
681
+ 'bold' : [1, 22],
682
+ 'italic' : [3, 23],
683
+ 'underline' : [4, 24],
684
+ 'inverse' : [7, 27],
685
+ 'white' : [37, 39],
686
+ 'grey' : [90, 39],
687
+ 'black' : [30, 39],
688
+ 'blue' : [34, 39],
689
+ 'cyan' : [36, 39],
690
+ 'green' : [32, 39],
691
+ 'magenta' : [35, 39],
692
+ 'red' : [31, 39],
693
+ 'yellow' : [33, 39]
694
+ };
695
+
696
+ // Don't use 'blue' not visible on cmd.exe
697
+ var styles = {
698
+ 'special': 'cyan',
699
+ 'number': 'yellow',
700
+ 'boolean': 'yellow',
701
+ 'undefined': 'grey',
702
+ 'null': 'bold',
703
+ 'string': 'green',
704
+ 'date': 'magenta',
705
+ // "name": intentionally not styling
706
+ 'regexp': 'red'
707
+ };
708
+
709
+
710
+ function stylizeWithColor(str, styleType) {
711
+ var style = styles[styleType];
712
+
713
+ if (style) {
714
+ return '\033[' + colors[style][0] + 'm' + str +
715
+ '\033[' + colors[style][1] + 'm';
716
+ } else {
717
+ return str;
718
+ }
719
+ }
720
+
721
+
722
+ function stylizeNoColor(str, styleType) {
723
+ return str;
724
+ }
725
+
726
+
727
+ function formatValue(ctx, value, recurseTimes) {
728
+ // Provide a hook for user-specified inspect functions.
729
+ // Check that value is an object with an inspect function on it
730
+ if (value && typeof value.inspect === 'function' &&
731
+ // Filter out the util module, it's inspect function is special
732
+ value.inspect !== exports.inspect &&
733
+ // Also filter out any prototype objects using the circular check.
734
+ !(value.constructor && value.constructor.prototype === value)) {
735
+ return value.inspect(recurseTimes);
736
+ }
737
+
738
+ // Primitive types cannot have properties
739
+ var primitive = formatPrimitive(ctx, value);
740
+ if (primitive) {
741
+ return primitive;
742
+ }
743
+
744
+ // Look up the keys of the object.
745
+ var visibleKeys = Object.keys(value);
746
+ var keys = ctx.showHidden ? Object.getOwnPropertyNames(value) : visibleKeys;
747
+
748
+ // Some type of object without properties can be shortcutted.
749
+ if (keys.length === 0) {
750
+ if (typeof value === 'function') {
751
+ var name = value.name ? ': ' + value.name : '';
752
+ return ctx.stylize('[Function' + name + ']', 'special');
753
+ }
754
+ if (isRegExp(value)) {
755
+ return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
756
+ }
757
+ if (isDate(value)) {
758
+ return ctx.stylize(Date.prototype.toString.call(value), 'date');
759
+ }
760
+ if (isError(value)) {
761
+ return formatError(value);
762
+ }
763
+ }
764
+
765
+ var base = '', array = false, braces = ['{', '}'];
766
+
767
+ // Make Array say that they are Array
768
+ if (isArray(value)) {
769
+ array = true;
770
+ braces = ['[', ']'];
771
+ }
772
+
773
+ // Make functions say that they are functions
774
+ if (typeof value === 'function') {
775
+ var n = value.name ? ': ' + value.name : '';
776
+ base = ' [Function' + n + ']';
777
+ }
778
+
779
+ // Make RegExps say that they are RegExps
780
+ if (isRegExp(value)) {
781
+ base = ' ' + RegExp.prototype.toString.call(value);
782
+ }
783
+
784
+ // Make dates with properties first say the date
785
+ if (isDate(value)) {
786
+ base = ' ' + Date.prototype.toUTCString.call(value);
787
+ }
788
+
789
+ // Make error with message first say the error
790
+ if (isError(value)) {
791
+ base = ' ' + formatError(value);
792
+ }
793
+
794
+ if (keys.length === 0 && (!array || value.length === 0)) {
795
+ return braces[0] + base + braces[1];
796
+ }
797
+
798
+ if (recurseTimes < 0) {
799
+ if (isRegExp(value)) {
800
+ return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
801
+ } else {
802
+ return ctx.stylize('[Object]', 'special');
803
+ }
804
+ }
805
+
806
+ ctx.seen.push(value);
807
+
808
+ var output;
809
+ if (array) {
810
+ output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
811
+ } else {
812
+ output = keys.map(function(key) {
813
+ return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
814
+ });
815
+ }
816
+
817
+ ctx.seen.pop();
818
+
819
+ return reduceToSingleString(output, base, braces);
820
+ }
821
+
822
+
823
+ function formatPrimitive(ctx, value) {
824
+ switch (typeof value) {
825
+ case 'undefined':
826
+ return ctx.stylize('undefined', 'undefined');
827
+
828
+ case 'string':
829
+ var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
830
+ .replace(/'/g, "\\'")
831
+ .replace(/\\"/g, '"') + '\'';
832
+ return ctx.stylize(simple, 'string');
833
+
834
+ case 'number':
835
+ return ctx.stylize('' + value, 'number');
836
+
837
+ case 'boolean':
838
+ return ctx.stylize('' + value, 'boolean');
839
+ }
840
+ // For some reason typeof null is "object", so special case here.
841
+ if (value === null) {
842
+ return ctx.stylize('null', 'null');
843
+ }
844
+ }
845
+
846
+
847
+ function formatError(value) {
848
+ return '[' + Error.prototype.toString.call(value) + ']';
849
+ }
850
+
851
+
852
+ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
853
+ var output = [];
854
+ for (var i = 0, l = value.length; i < l; ++i) {
855
+ if (Object.prototype.hasOwnProperty.call(value, String(i))) {
856
+ output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
857
+ String(i), true));
858
+ } else {
859
+ output.push('');
860
+ }
861
+ }
862
+ keys.forEach(function(key) {
863
+ if (!key.match(/^\d+$/)) {
864
+ output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
865
+ key, true));
866
+ }
867
+ });
868
+ return output;
869
+ }
870
+
871
+
872
+ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
873
+ var name, str, desc;
874
+ desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
875
+ if (desc.get) {
876
+ if (desc.set) {
877
+ str = ctx.stylize('[Getter/Setter]', 'special');
878
+ } else {
879
+ str = ctx.stylize('[Getter]', 'special');
880
+ }
881
+ } else {
882
+ if (desc.set) {
883
+ str = ctx.stylize('[Setter]', 'special');
884
+ }
885
+ }
886
+ if (visibleKeys.indexOf(key) < 0) {
887
+ name = '[' + key + ']';
888
+ }
889
+ if (!str) {
890
+ if (ctx.seen.indexOf(desc.value) < 0) {
891
+ if (recurseTimes === null) {
892
+ str = formatValue(ctx, desc.value, null);
893
+ } else {
894
+ str = formatValue(ctx, desc.value, recurseTimes - 1);
895
+ }
896
+ if (str.indexOf('\n') > -1) {
897
+ if (array) {
898
+ str = str.split('\n').map(function(line) {
899
+ return ' ' + line;
900
+ }).join('\n').substr(2);
901
+ } else {
902
+ str = '\n' + str.split('\n').map(function(line) {
903
+ return ' ' + line;
904
+ }).join('\n');
905
+ }
906
+ }
907
+ } else {
908
+ str = ctx.stylize('[Circular]', 'special');
909
+ }
910
+ }
911
+ if (typeof name === 'undefined') {
912
+ if (array && key.match(/^\d+$/)) {
913
+ return str;
914
+ }
915
+ name = JSON.stringify('' + key);
916
+ if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
917
+ name = name.substr(1, name.length - 2);
918
+ name = ctx.stylize(name, 'name');
919
+ } else {
920
+ name = name.replace(/'/g, "\\'")
921
+ .replace(/\\"/g, '"')
922
+ .replace(/(^"|"$)/g, "'");
923
+ name = ctx.stylize(name, 'string');
924
+ }
925
+ }
926
+
927
+ return name + ': ' + str;
928
+ }
929
+
930
+
931
+ function reduceToSingleString(output, base, braces) {
932
+ var numLinesEst = 0;
933
+ var length = output.reduce(function(prev, cur) {
934
+ numLinesEst++;
935
+ if (cur.indexOf('\n') >= 0) numLinesEst++;
936
+ return prev + cur.length + 1;
937
+ }, 0);
938
+
939
+ if (length > 60) {
940
+ return braces[0] +
941
+ (base === '' ? '' : base + '\n ') +
942
+ ' ' +
943
+ output.join(',\n ') +
944
+ ' ' +
945
+ braces[1];
946
+ }
947
+
948
+ return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
949
+ }
950
+
951
+
952
+ // NOTE: These type checking functions intentionally don't use `instanceof`
953
+ // because it is fragile and can be easily faked with `Object.create()`.
954
+ function isArray(ar) {
955
+ return Array.isArray(ar) ||
956
+ (typeof ar === 'object' && objectToString(ar) === '[object Array]');
957
+ }
958
+ exports.isArray = isArray;
959
+
960
+
961
+ function isRegExp(re) {
962
+ return typeof re === 'object' && objectToString(re) === '[object RegExp]';
963
+ }
964
+ exports.isRegExp = isRegExp;
965
+
966
+
967
+ function isDate(d) {
968
+ return typeof d === 'object' && objectToString(d) === '[object Date]';
969
+ }
970
+ exports.isDate = isDate;
971
+
972
+
973
+ function isError(e) {
974
+ return typeof e === 'object' && objectToString(e) === '[object Error]';
975
+ }
976
+ exports.isError = isError;
977
+
978
+
979
+ function objectToString(o) {
980
+ return Object.prototype.toString.call(o);
981
+ }
982
+
983
+
984
+ // exports.p = function() {
985
+ // for (var i = 0, len = arguments.length; i < len; ++i) {
986
+ // error(exports.inspect(arguments[i]));
987
+ // }
988
+ // };
989
+ // module.deprecate('p', 'Use `util.puts(util.inspect())` instead.');
990
+
991
+
992
+ function pad(n) {
993
+ return n < 10 ? '0' + n.toString(10) : n.toString(10);
994
+ }
995
+
996
+
997
+ var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
998
+ 'Oct', 'Nov', 'Dec'];
999
+
1000
+ // 26 Feb 16:19:34
1001
+ function timestamp() {
1002
+ var d = new Date();
1003
+ var time = [pad(d.getHours()),
1004
+ pad(d.getMinutes()),
1005
+ pad(d.getSeconds())].join(':');
1006
+ return [d.getDate(), months[d.getMonth()], time].join(' ');
1007
+ }
1008
+
1009
+
1010
+ exports.log = function(msg) {
1011
+ exports.puts(timestamp() + ' - ' + msg.toString());
1012
+ };
1013
+
1014
+
1015
+ // exports.exec = function() {
1016
+ // return require('child_process').exec.apply(this, arguments);
1017
+ // };
1018
+ // module.deprecate('exec', 'It is now called `child_process.exec`.');
1019
+
1020
+
1021
+ exports.pump = function(readStream, writeStream, callback) {
1022
+ var callbackCalled = false;
1023
+
1024
+ function call(a, b, c) {
1025
+ if (callback && !callbackCalled) {
1026
+ callback(a, b, c);
1027
+ callbackCalled = true;
1028
+ }
1029
+ }
1030
+
1031
+ readStream.addListener('data', function(chunk) {
1032
+ if (writeStream.write(chunk) === false) readStream.pause();
1033
+ });
1034
+
1035
+ writeStream.addListener('drain', function() {
1036
+ readStream.resume();
1037
+ });
1038
+
1039
+ readStream.addListener('end', function() {
1040
+ writeStream.end();
1041
+ });
1042
+
1043
+ readStream.addListener('close', function() {
1044
+ call();
1045
+ });
1046
+
1047
+ readStream.addListener('error', function(err) {
1048
+ writeStream.end();
1049
+ call(err);
1050
+ });
1051
+
1052
+ writeStream.addListener('error', function(err) {
1053
+ readStream.destroy();
1054
+ call(err);
1055
+ });
1056
+ };
1057
+
1058
+
1059
+ /**
1060
+ * Inherit the prototype methods from one constructor into another.
1061
+ *
1062
+ * The Function.prototype.inherits from lang.js rewritten as a standalone
1063
+ * function (not on Function.prototype). NOTE: If this file is to be loaded
1064
+ * during bootstrapping this function needs to be rewritten using some native
1065
+ * functions as prototype setup using normal JavaScript does not work as
1066
+ * expected during bootstrapping (see mirror.js in r114903).
1067
+ *
1068
+ * @param {function} ctor Constructor function which needs to inherit the
1069
+ * prototype.
1070
+ * @param {function} superCtor Constructor function to inherit prototype from.
1071
+ */
1072
+ exports.inherits = function(ctor, superCtor) {
1073
+ ctor.super_ = superCtor;
1074
+ ctor.prototype = Object.create(superCtor.prototype, {
1075
+ constructor: {
1076
+ value: ctor,
1077
+ enumerable: false,
1078
+ writable: true,
1079
+ configurable: true
1080
+ }
1081
+ });
1082
+ };
1083
+
1084
+ exports._extend = function(origin, add) {
1085
+ // Don't do anything if add isn't an object
1086
+ if (!add) return origin;
1087
+
1088
+ var keys = Object.keys(add);
1089
+ var i = keys.length;
1090
+ while (i--) {
1091
+ origin[keys[i]] = add[keys[i]];
1092
+ }
1093
+ return origin;
1094
+ };
1095
+
1096
+ });
1097
+
1098
+ /*global define:true */
1099
+
1100
+
1101
+
1102
+ define('react/error',['util'], function (util) {
1103
+
1104
+ function ensureStackTraceLimitSet(stackTraceLimit) {
1105
+ if (!Error.stackTraceLimit || Error.stackTraceLimit < stackTraceLimit) {
1106
+ Error.stackTraceLimit = stackTraceLimit;
1107
+ }
1108
+ }
1109
+
1110
+ function fName(fn) {
1111
+ if (!fn) return 'undefined';
1112
+ return (fn && fn.name) ? fn.name : fn;
1113
+ }
1114
+
1115
+ function formatErrorMeta(err) {
1116
+ if (!err.meta) return;
1117
+ var vcon = err.meta.vcon;
1118
+ var task = err.meta.task;
1119
+ var errString = '\n\n';
1120
+ if (task && task.f && task.a) {
1121
+ errString += ('Error occurs in Task function: ' + fName(task.f) + '(' + task.a.join(',') + ')\n\n');
1122
+ }
1123
+ if (vcon) {
1124
+ errString += 'Variable Context: \n';
1125
+ errString += util.inspect(vcon);
1126
+ errString += '\n\n';
1127
+ }
1128
+ if (task && task.f) {
1129
+ errString += 'Task Source:\n\n';
1130
+ errString += task.f.toString(); //TODO need to pretty print function, gets collapsed
1131
+ errString += '\n\n';
1132
+ }
1133
+ return errString;
1134
+ }
1135
+
1136
+ function augmentError(err, meta) {
1137
+ if (typeof(err) === 'string') { err = new Error(err); } //props will be lost on non-objects
1138
+ var origMsg = err.toString();
1139
+ err.meta = meta;
1140
+ err.toString = function () { return origMsg + formatErrorMeta(err); };
1141
+ return err;
1142
+ }
1143
+
1144
+ return {
1145
+ ensureStackTraceLimitSet: ensureStackTraceLimitSet,
1146
+ augmentError: augmentError
1147
+ };
1148
+
1149
+ });
1150
+ (function (root, factory) {
1151
+ /*global define:true */
1152
+
1153
+ if (typeof exports === 'object') {
1154
+ // Node. Does not work with strict CommonJS, but
1155
+ // only CommonJS-like enviroments that support module.exports,
1156
+ // like Node.
1157
+ module.exports = factory();
1158
+ } else if (typeof define === 'function' && define.amd) {
1159
+ // AMD. Register as an anonymous module.
1160
+ define('ensure-array',[], factory);
1161
+ } else {
1162
+ // Browser globals
1163
+ root.ensureArray = factory();
1164
+ }
1165
+ }(this, function () {
1166
+
1167
+
1168
+ function ensureArray(a, b, n) {
1169
+ if (arguments.length === 0) return []; //no args, ret []
1170
+ if (arguments.length === 1) { //single argument
1171
+ if (a === undefined || a === null) return []; // undefined or null, ret []
1172
+ if (Array.isArray(a)) return a; // isArray, return it
1173
+ }
1174
+ return Array.prototype.slice.call(arguments); //return array with copy of all arguments
1175
+ }
1176
+
1177
+ return ensureArray;
1178
+ }));
1179
+
1180
+
1181
+
1182
+ /*global define:true */
1183
+
1184
+
1185
+
1186
+ define('react/status',[], function () {
1187
+
1188
+ var STATUS = { READY: 'ready', RUNNING: 'running', ERRORED: 'errored', COMPLETE: 'complete' };
1189
+
1190
+ return STATUS;
1191
+
1192
+ });
1193
+
1194
+ /*global define:true */
1195
+
1196
+
1197
+
1198
+ define('react/vcon',[], function () {
1199
+
1200
+ var LAST_RESULTS_KEY = ':LAST_RESULTS';
1201
+
1202
+ function VContext() {
1203
+ }
1204
+
1205
+ VContext.prototype.getLastResults = function () { return this.getVar(LAST_RESULTS_KEY); };
1206
+ VContext.prototype.setLastResults = function (args) { this.setVar(LAST_RESULTS_KEY, args); };
1207
+
1208
+ VContext.prototype.getVar = function (name) { //name might be simple or obj.prop, also literals
1209
+ /*jshint regexp: false */
1210
+ var vConValues = this.values;
1211
+ if (typeof(name) !== 'string') return name; // literal boolean or number
1212
+ name = name.trim();
1213
+ // literal checks need to match what is in validate.js
1214
+ if (name === 'true') return true;
1215
+ if (name === 'false') return false;
1216
+ if (name === 'null') return null;
1217
+ if (/^-?[0-9]+$/.test(name)) return parseInt(name, 10); //int
1218
+ if (/^-?[0-9.]+$/.test(name)) return parseFloat(name); //float
1219
+ var m = /^("|')([^\1]*)\1$/.exec(name); //check for quoted string " or '
1220
+ if (m) return m[2]; // if is quoted str, return inside of the quotes
1221
+ var nameAndProps = name.split('.');
1222
+ return nameAndProps.reduce(function (accObj, prop) {
1223
+ if (accObj === undefined || accObj === null) return undefined; // prevent exception
1224
+ return accObj[prop];
1225
+ }, vConValues); // vCon['foo']['bar']
1226
+ };
1227
+
1228
+ /**
1229
+ Saves all the results from a task as a unit, also sets special
1230
+ variable :LAST_RESULTS which keeps an array of the last values
1231
+ which can be used for chaining and testing last results, etc.
1232
+ */
1233
+ VContext.prototype.saveResults = function (paramArr, valuesArr) { // set values for params
1234
+ var self = this;
1235
+ paramArr.forEach(function (k, idx) { //save values to v context
1236
+ self.setVar(k, (valuesArr[idx] !== undefined) ? valuesArr[idx] : null); //upgrade any undefined to null
1237
+ });
1238
+ this.setLastResults(valuesArr);
1239
+ };
1240
+
1241
+ VContext.prototype.setVar = function (name, value) { //name might be simple or obj.prop
1242
+ if (!name) return; // if name is undefined or null, then discard
1243
+ var vConValues = this.values;
1244
+ var nameAndProps = name.split('.');
1245
+ var lastProp = nameAndProps.pop();
1246
+ var obj = nameAndProps.reduce(function (accObj, prop) {
1247
+ var o = accObj[prop];
1248
+ if (o === undefined || o === null) { // if doesn't exist create it
1249
+ o = accObj[prop] = { };
1250
+ }
1251
+ return o;
1252
+ }, vConValues); // vCon['foo']['bar']
1253
+ obj[lastProp] = value;
1254
+ };
1255
+
1256
+
1257
+ /**
1258
+ Create Variable Context using arguments passed in.
1259
+ Ignore extra arguments passed in. Locals can be
1260
+ passed into seed the VContext otherwise empty {}
1261
+ will be used
1262
+ @param self used to pass 'this' context in
1263
+ */
1264
+ VContext.create = function (args, inParams, locals, self) {
1265
+ var initValues = {};
1266
+ if (self) initValues['this'] = self;
1267
+ if (locals) Object.keys(locals).forEach(function (k) { initValues[k] = locals[k]; }); // copy over keys
1268
+ var vContext = new VContext();
1269
+ vContext.values = args.reduce(function (vcon, x, idx) { // create vCon start with input args
1270
+ var param = inParams[idx];
1271
+ if (param) vcon[param] = (x !== undefined) ? x : null; // upgrade undefined to null
1272
+ return vcon;
1273
+ }, initValues);
1274
+ return vContext;
1275
+ };
1276
+
1277
+
1278
+ return VContext;
1279
+
1280
+ });
1281
+
1282
+ /*global define:true process:false*/
1283
+
1284
+
1285
+
1286
+ define('react/event-manager',['./eventemitter'], function (EventEmitter) {
1287
+ /*jshint regexp:false */
1288
+
1289
+ var EVENT_EMITTER2_CONFIG = {
1290
+ wildcard: true, // should the event emitter use wildcards.
1291
+ delimiter: '.', // the delimiter used to segment namespaces, defaults to `.`.
1292
+ maxListeners: 30 // the max number of listeners that can be assigned to an event, defaults to 10.
1293
+ };
1294
+
1295
+ var PASS_EVENTS_PROCESS_RE = /^ast.defined$/; // events to pass up to global process
1296
+
1297
+ var TYPES = {
1298
+ // Flow monitoring events and their params
1299
+ AST_DEFINED: 'ast.defined', // ast
1300
+ FLOW_BEGIN: 'flow.begin', // env
1301
+ TASK_BEGIN: 'task.begin', // task
1302
+ TASK_COMPLETE: 'task.complete', // task
1303
+ TASK_ERRORED: 'task.errored', // task
1304
+ FLOW_COMPLETE: 'flow.complete', // env
1305
+ FLOW_ERRORED: 'flow.errored', // env
1306
+
1307
+ // Internal Hooks
1308
+ EXEC_FLOW_START: 'exec.flow.start', // env
1309
+ EXEC_INPUT_PREPROCESS: 'exec.input.preprocess', // parsedInput
1310
+ EXEC_TASKS_PRECREATE: 'exec.tasks.precreate', // env
1311
+ EXEC_OUTTASK_CREATE: 'exec.outTask.create', // outTaskOptions
1312
+ EXEC_TASK_START: 'exec.task.start', // task
1313
+ EXEC_TASK_COMPLETE: 'exec.task.complete', // task
1314
+ EXEC_TASK_ERRORED: 'exec.task.errored', // task
1315
+ EXEC_FLOW_COMPLETE: 'exec.flow.complete', // env
1316
+ EXEC_FLOW_ERRORED: 'exec.flow.errored' // env
1317
+ };
1318
+
1319
+ /**
1320
+ Event manager which emits events up to its parent if exists.
1321
+ Allows a hierarchy of emitters, which communicate up the
1322
+ chain.
1323
+ */
1324
+ function EventManager() {
1325
+ }
1326
+
1327
+ EventManager.create = function () { return new EventManager(); };
1328
+
1329
+ EventManager.TYPES = TYPES;
1330
+ EventManager.prototype.TYPES = TYPES;
1331
+
1332
+ EventManager.prototype.isEnabled = function () { // if has listener or an ancestor has listener
1333
+ return !!(this.emitter || (this.parent && this.parent.isEnabled()));
1334
+ };
1335
+
1336
+ /**
1337
+ Add listener. Wildcard events are allowed like 'foo.*'
1338
+ Use '*' to listen to any event
1339
+ */
1340
+ EventManager.prototype.on = function (event, listener) {
1341
+ if (!this.emitter) this.emitter = new EventEmitter(EVENT_EMITTER2_CONFIG);
1342
+ if (event === '*') this.emitter.onAny(listener);
1343
+ else this.emitter.on(event, listener);
1344
+ };
1345
+
1346
+ EventManager.prototype.emit = function (event, arg1, arg2, argN) {
1347
+ if (event === undefined) throw new Error('event is undefined');
1348
+ if (this.emitter) this.emitter.emit.apply(this.emitter, arguments);
1349
+ if (this.parent && this.parent.isEnabled()) this.parent.emit.apply(this.parent, arguments);
1350
+ if (PASS_EVENTS_PROCESS_RE.test(event) && typeof(process) !== 'undefined' && process.emit) {
1351
+ process.emit.apply(process, arguments); // pass some to process
1352
+ }
1353
+ };
1354
+
1355
+ EventManager.prototype.removeListener = function (event, listener) {
1356
+ if (this.emitter) this.emitter.removeListener.apply(this.emitter, arguments);
1357
+ };
1358
+
1359
+
1360
+ EventManager.global = EventManager.create(); // create one top level emitter
1361
+ return EventManager;
1362
+
1363
+ });
1364
+
1365
+ /*global define:true */
1366
+
1367
+
1368
+
1369
+ define('react/base-task',['ensure-array', './status', './event-manager'],
1370
+ function (array, STATUS, EventManager) {
1371
+
1372
+ function BaseTask() {
1373
+ }
1374
+
1375
+ /**
1376
+ Getter Fn to retrieveAn array of the output param names for this task.
1377
+ */
1378
+ BaseTask.prototype.getOutParams = function () {
1379
+ return array(this.out); // ensure array
1380
+ };
1381
+
1382
+ BaseTask.prototype.isComplete = function () {
1383
+ return (this.status === STATUS.COMPLETE);
1384
+ };
1385
+
1386
+ BaseTask.prototype.start = function (args) { // mark task as started with args and note time
1387
+ /*jshint validthis: true */
1388
+ this.args = args;
1389
+ this.env.currentTask = this;
1390
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_START, this);
1391
+ };
1392
+
1393
+ BaseTask.prototype.complete = function (args) { //args that were used are available
1394
+ /*jshint validthis: true */
1395
+ this.status = STATUS.COMPLETE;
1396
+ this.results = args;
1397
+ this.env.currentTask = this;
1398
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_COMPLETE, this);
1399
+ };
1400
+
1401
+ BaseTask.prototype.functionExists = function (vCon) {
1402
+ var fn = this.f;
1403
+ if (!fn) return false;
1404
+ if (fn instanceof Function) return true;
1405
+ if (typeof(fn) === 'string') {
1406
+ var f = vCon.getVar(fn); // fn/method by string
1407
+ if (f && f instanceof Function) return true;
1408
+ }
1409
+ return false;
1410
+ };
1411
+
1412
+ BaseTask.prototype.areAllDepArgsDefined = function (vCon) {
1413
+ return this.a.every(function (k) { return (vCon.getVar(k) !== undefined); });
1414
+ };
1415
+
1416
+ BaseTask.prototype.depTasksAreDone = function (tasksByName) {
1417
+ return (!this.after || !this.after.length || // no dep tasks OR
1418
+ this.after.every(function (n) { return tasksByName[n].isComplete(); })); //all done
1419
+ };
1420
+
1421
+ function isObjProperty(str) { return (str.indexOf('.') !== -1); }
1422
+
1423
+ /**
1424
+ check that obj parent is def and not null so writing to obj.prop
1425
+ will not fail. ex: 'b.c' checks that b is def and not null.
1426
+ Also returns true if not obj.prop but simple var ex: 'b'.
1427
+ Tasks will implement outParentsExist() passing each out str
1428
+ to this if they want to do this check.
1429
+ */
1430
+ BaseTask.prototype.parentExists = function (objPropStr, vCon) {
1431
+ if (!isObjProperty(objPropStr)) return true; // NOT obj prop, just simple arg, ret true
1432
+ var nameAndProps = objPropStr.split('.');
1433
+ nameAndProps.pop(); // pop off final prop
1434
+ var parent = nameAndProps.reduce(function (accObj, prop) {
1435
+ if (accObj === undefined || accObj === null) return undefined; // prevent exception
1436
+ return accObj[prop];
1437
+ }, vCon.values); // vCon['foo']['bar']
1438
+ return (parent !== undefined && parent !== null);
1439
+ };
1440
+
1441
+ /**
1442
+ If params are obj property writes make sure the dst objects
1443
+ are defined and not null. cb: ['b.c'] -> b is def and not null.
1444
+ If null is specified then param is valid and will be ignored.
1445
+ @returns true if all obj prop parents are def and non null
1446
+ */
1447
+ BaseTask.prototype.outParentsExist = function (vCon) {
1448
+ var self = this;
1449
+ return this.getOutParams().every(function (x) {
1450
+ if (x === null) return true;
1451
+ return self.parentExists(x, vCon);
1452
+ });
1453
+ };
1454
+
1455
+ BaseTask.prototype.isReady = function (vCon, tasksByName) {
1456
+ return !this.status && // not started AND
1457
+ this.functionExists(vCon) && // function/method exists AND
1458
+ this.areAllDepArgsDefined(vCon) && // all dep vars defined AND
1459
+ this.depTasksAreDone(tasksByName) && // all dep tasks are done AND
1460
+ (!this.outParentsExist || // (task does not implement outParentsExist method OR
1461
+ this.outParentsExist(vCon)); // output parents exists (for obj property writes)
1462
+ };
1463
+
1464
+ BaseTask.prototype.isMethodCall = function () {
1465
+ /*jshint regexp: false */
1466
+ return (typeof(this.f) === 'string' && /^.*\..*$/.test(this.f)); //str contains .
1467
+ };
1468
+
1469
+ BaseTask.prototype.getMethodObj = function (vCon) { //obj.prop.prop2, returns obj.prop or undefined
1470
+ var name = this.f;
1471
+ if (!name) return undefined;
1472
+ var nameAndProps = name.split('.');
1473
+ nameAndProps.pop(); // pop off last one
1474
+ if (!nameAndProps.length) return undefined;
1475
+ var result = nameAndProps.reduce(function (accObj, prop) {
1476
+ if (accObj === undefined || accObj === null) return undefined; // prevent exception
1477
+ return accObj[prop];
1478
+ }, vCon.values); // vCon['foo']['bar']
1479
+ return result;
1480
+ };
1481
+
1482
+ return BaseTask;
1483
+
1484
+ });
1485
+
1486
+ /*global define:true */
1487
+
1488
+
1489
+
1490
+ define('react/input-parser',['./event-manager'], function (EventManager) {
1491
+
1492
+ var defaultExecOptions = {
1493
+ reactExecOptions: true,
1494
+ outputStyle: 'cb',
1495
+ };
1496
+
1497
+ var OUTPUT_STYLES = {
1498
+ CALLBACK: 'cb',
1499
+ NONE: 'none'
1500
+ };
1501
+
1502
+ function isExecOptions(x) { return (x && x.reactExecOptions); }
1503
+ function execOptionsFilter(x) { return isExecOptions(x); }
1504
+ function nonExecOptionsFilter(x) { return !isExecOptions(x); }
1505
+ function mergeExecOptions(accum, options) {
1506
+ Object.keys(options).forEach(function (k) { accum[k] = options[k]; });
1507
+ return accum;
1508
+ }
1509
+
1510
+ function splitArgs(args, inParams, style) {
1511
+ var result = { };
1512
+ result.args = inParams.map(function (p) { return args.shift(); }); // take args for input params first
1513
+ if (style === OUTPUT_STYLES.CALLBACK && args.length) result.cb = args.shift(); // next take the cb
1514
+ result.extra = args; // these remaining were after the callback
1515
+ return result;
1516
+ }
1517
+
1518
+ function inputParser(inputArgs, ast) {
1519
+ var parsedInput = { };
1520
+ var execOptionsArr = inputArgs.filter(execOptionsFilter);
1521
+ execOptionsArr.unshift(defaultExecOptions);
1522
+ parsedInput.options = execOptionsArr.reduce(mergeExecOptions, {});
1523
+
1524
+ var args = inputArgs.filter(nonExecOptionsFilter);
1525
+ var splitResult = splitArgs(args, ast.inParams, parsedInput.options.outputStyle);
1526
+ parsedInput.args = splitResult.args;
1527
+ parsedInput.cb = splitResult.cb;
1528
+ if (splitResult.outputStyle) parsedInput.options.outputStyle = splitResult.outputStyle;
1529
+ if (splitResult.extra) parsedInput.extraArgs = splitResult.extra;
1530
+ EventManager.global.emit(EventManager.TYPES.EXEC_INPUT_PREPROCESS, parsedInput); // hook
1531
+ return parsedInput;
1532
+ }
1533
+
1534
+
1535
+ inputParser.defaultExecOptions = defaultExecOptions;
1536
+ return inputParser;
1537
+
1538
+ });
1539
+
1540
+ /*global define:true */
1541
+
1542
+
1543
+
1544
+ define('react/id',[], function () {
1545
+
1546
+ var startingId = 0;
1547
+
1548
+ function createUniqueId() {
1549
+ startingId += 1;
1550
+ if (startingId === Number.MAX_VALUE) startingId = 0; // if hits this start over //TODO need something better?
1551
+ return startingId;
1552
+ }
1553
+
1554
+ return {
1555
+ createUniqueId: createUniqueId
1556
+ };
1557
+
1558
+ });
1559
+
1560
+ /*global define:true sprint:true */
1561
+
1562
+
1563
+
1564
+ define('react/sprintf',['util'], function (util) {
1565
+
1566
+ /**
1567
+ Abstract the details of getting a sprintf function.
1568
+ Currently using the simple format capabilities of node's util.format
1569
+ */
1570
+
1571
+ var sprintf = util.format;
1572
+ return sprintf;
1573
+
1574
+ });
1575
+
1576
+ /*global define:true */
1577
+
1578
+
1579
+
1580
+ define('react/cb-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1581
+
1582
+ function format_error(errmsg, obj) {
1583
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1584
+ }
1585
+
1586
+ var REQ = 'cbTask requires f, a, out';
1587
+ var FN_REQ = 'cbTask requires f to be a function or string';
1588
+ var A_REQ = 'cbTask requires a to be an array of string param names';
1589
+ var CB_REQ = 'cbTask requires out to be an array of string param names';
1590
+
1591
+ function CbTask(taskDef) {
1592
+ var self = this;
1593
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1594
+ }
1595
+
1596
+ CbTask.prototype = new BaseTask();
1597
+ CbTask.prototype.constructor = CbTask;
1598
+
1599
+ CbTask.validate = function (taskDef) {
1600
+ var errors = [];
1601
+ if (!taskDef.f || !taskDef.a || !taskDef.out) {
1602
+ errors.push(format_error(REQ, taskDef));
1603
+ } else {
1604
+ var ftype = typeof(taskDef.f);
1605
+ if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
1606
+ errors.push(format_error(FN_REQ, taskDef));
1607
+ }
1608
+ if (! (Array.isArray(taskDef.a) &&
1609
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1610
+ errors.push(format_error(A_REQ, taskDef));
1611
+ }
1612
+ if (! (Array.isArray(taskDef.out) &&
1613
+ taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
1614
+ errors.push(format_error(CB_REQ, taskDef));
1615
+ }
1616
+ }
1617
+ return errors;
1618
+ };
1619
+
1620
+ CbTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
1621
+ var self = this;
1622
+ this.cbFun = function (err, arg0, arg1, argn) {
1623
+ var args = Array.prototype.slice.call(arguments, 1);
1624
+ if (err) { handleTaskError(self, err); return; } //handle error and return, we are done
1625
+
1626
+ //no error, save callback args to vCon context, then continue execution
1627
+ vCon.saveResults(self.out, args);
1628
+ self.complete(args);
1629
+ contExec();
1630
+ };
1631
+ };
1632
+
1633
+ CbTask.prototype.exec = function exec(vCon, handleError, contExec) {
1634
+ try {
1635
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1636
+ //console.error('CbTask.exec.args=', args);
1637
+ //console.error('CbTask.exec.vCon=', vCon);
1638
+ this.start(args); //note the start time, args
1639
+ args.push(this.cbFun); // push callback fn on end
1640
+ var func = this.f;
1641
+ var bindObj = vCon.getVar('this'); //global space or the original this
1642
+ if (this.isMethodCall()) { //if method call then reset func and bindObj
1643
+ func = vCon.getVar(this.f);
1644
+ bindObj = this.getMethodObj(vCon);
1645
+ } else if (typeof(func) === 'string') {
1646
+ func = vCon.getVar(func); // we want the actual fn from this string
1647
+ }
1648
+ func.apply(bindObj, args);
1649
+ } catch (err) { //catch and handle the task error, calling final cb
1650
+ handleError(this, err);
1651
+ }
1652
+ };
1653
+
1654
+ return CbTask;
1655
+
1656
+ });
1657
+
1658
+
1659
+ /*global define:true */
1660
+
1661
+
1662
+
1663
+ define('react/promise-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1664
+
1665
+ /**
1666
+ PromiseTask is a task which executes a fn that returns a promise
1667
+ and when it completes it sets the values in vCon
1668
+ */
1669
+
1670
+ function format_error(errmsg, obj) {
1671
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1672
+ }
1673
+
1674
+ var REQ = 'promiseTask requires f, a, out';
1675
+ var FN_REQ = 'promiseTask requires f to be a function or string';
1676
+ var A_REQ = 'promiseTask requires a to be an array of string param names';
1677
+ var OUT_REQ = 'promiseTask requires out to be an array[1] of string param names';
1678
+
1679
+ function PromiseTask(taskDef) {
1680
+ var self = this;
1681
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1682
+ }
1683
+
1684
+ PromiseTask.prototype = new BaseTask();
1685
+ PromiseTask.prototype.constructor = PromiseTask;
1686
+
1687
+ PromiseTask.validate = function (taskDef) {
1688
+ var errors = [];
1689
+ if (!taskDef.f || !taskDef.a || !taskDef.out) {
1690
+ errors.push(format_error(REQ, taskDef));
1691
+ } else {
1692
+ var ftype = typeof(taskDef.f);
1693
+ if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
1694
+ errors.push(format_error(FN_REQ, taskDef));
1695
+ }
1696
+ if (! (Array.isArray(taskDef.a) &&
1697
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1698
+ errors.push(format_error(A_REQ, taskDef));
1699
+ }
1700
+ if (! (Array.isArray(taskDef.out) && taskDef.out.length <= 1 &&
1701
+ taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
1702
+ errors.push(format_error(OUT_REQ, taskDef));
1703
+ }
1704
+ }
1705
+ return errors;
1706
+ };
1707
+
1708
+ PromiseTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
1709
+ var self = this;
1710
+ this.nextFn = function (arg) {
1711
+ var args = Array.prototype.slice.call(arguments);
1712
+ vCon.saveResults(self.out, args);
1713
+ self.complete(args);
1714
+ contExec();
1715
+ };
1716
+ this.failFn = function (err) {
1717
+ handleTaskError(self, err);
1718
+ };
1719
+ };
1720
+
1721
+ PromiseTask.prototype.exec = function exec(vCon, handleError, contExec) {
1722
+ try {
1723
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1724
+ //console.error('PromiseTask.exec.args=', args);
1725
+ //console.error('PromiseTask.exec.vCon=', vCon);
1726
+ this.start(args); //note the start time, args
1727
+ var func = this.f;
1728
+ var bindObj = vCon.getVar('this'); //global space or the original this
1729
+ if (this.isMethodCall()) { //if method call then reset func and bindObj
1730
+ func = vCon.getVar(this.f);
1731
+ bindObj = this.getMethodObj(vCon);
1732
+ } else if (typeof(func) === 'string') {
1733
+ func = vCon.getVar(func); // we want the actual fn from this string
1734
+ }
1735
+ var retValue = func.apply(bindObj, args);
1736
+ if (retValue && typeof(retValue.then) === 'function') { // is a promise
1737
+ retValue.then(this.nextFn, this.failFn);
1738
+ } else { // just a value, proceed now
1739
+ this.nextFn(retValue);
1740
+ }
1741
+ } catch (err) { //catch and handle the task error, calling final cb
1742
+ handleError(this, err);
1743
+ }
1744
+ };
1745
+
1746
+ return PromiseTask;
1747
+
1748
+ });
1749
+
1750
+ /*global define:true */
1751
+
1752
+
1753
+
1754
+ define('react/ret-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1755
+
1756
+ function format_error(errmsg, obj) {
1757
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1758
+ }
1759
+
1760
+ var REQ = 'retTask requires f, a, out';
1761
+ var FN_REQ = 'retTask requires f to be a function or string';
1762
+ var A_REQ = 'retTask requires a to be an array of string param names';
1763
+ var RET_REQ = 'retTask requires out to be an array with single string param name or []';
1764
+
1765
+ function RetTask(taskDef) {
1766
+ var self = this;
1767
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1768
+ }
1769
+
1770
+ RetTask.prototype = new BaseTask();
1771
+ RetTask.prototype.constructor = RetTask;
1772
+
1773
+ RetTask.validate = function (taskDef) {
1774
+ var errors = [];
1775
+ if (!taskDef.f || !taskDef.a || !taskDef.out) {
1776
+ errors.push(format_error(REQ, taskDef));
1777
+ } else {
1778
+ var ftype = typeof(taskDef.f);
1779
+ if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
1780
+ errors.push(format_error(FN_REQ, taskDef));
1781
+ }
1782
+ if (! (Array.isArray(taskDef.a) &&
1783
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1784
+ errors.push(format_error(A_REQ, taskDef));
1785
+ }
1786
+
1787
+ if (! (Array.isArray(taskDef.out) &&
1788
+ (taskDef.out.length === 0 ||
1789
+ (taskDef.out.length === 1 && typeof(taskDef.out[0] === 'string'))))) {
1790
+ errors.push(format_error(RET_REQ, taskDef));
1791
+ }
1792
+ }
1793
+ return errors;
1794
+ };
1795
+
1796
+ RetTask.prototype.exec = function exec(vCon, handleError, contExec) {
1797
+ try {
1798
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1799
+ this.start(args); //note the start time, args
1800
+ var func = this.f;
1801
+ var bindObj = vCon.getVar('this'); //global space or the original this
1802
+ if (this.isMethodCall()) { //if method call then reset func and bindObj
1803
+ func = vCon.getVar(this.f);
1804
+ bindObj = this.getMethodObj(vCon);
1805
+ } else if (typeof(func) === 'string') {
1806
+ func = vCon.getVar(func); // we want the actual fn from this string
1807
+ }
1808
+ var results = [func.apply(bindObj, args)];
1809
+ vCon.saveResults(this.out, results); // save retval, takes arrays
1810
+ this.complete(results);
1811
+ contExec(); // continue since no callback to run this
1812
+ } catch (err) { handleError(this, err); } // catch and handle the task error, calling final cb
1813
+ };
1814
+
1815
+ return RetTask;
1816
+
1817
+ });
1818
+
1819
+ /*global define:true */
1820
+
1821
+
1822
+
1823
+ define('react/when-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1824
+
1825
+ /**
1826
+ When task which checks if is a promise (has a then method)
1827
+ and waits for it to resolve.
1828
+
1829
+ If argument does not have a then method, it resolves immediately
1830
+ */
1831
+
1832
+ function format_error(errmsg, obj) {
1833
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1834
+ }
1835
+
1836
+ var REQ = 'whenTask requires a, out';
1837
+ var A_REQ = 'whenTask requires a to be an array[1] of string param names';
1838
+ var OUT_REQ = 'whenTask requires out to be an array[1] of string param names';
1839
+
1840
+ function WhenTask(taskDef) {
1841
+ var self = this;
1842
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1843
+ }
1844
+
1845
+ WhenTask.prototype = new BaseTask();
1846
+ WhenTask.prototype.constructor = WhenTask;
1847
+
1848
+ WhenTask.prototype.f = function when() { // just here to keep validations happy
1849
+ }
1850
+
1851
+ WhenTask.validate = function (taskDef) {
1852
+ var errors = [];
1853
+ if (!taskDef.a || !taskDef.out) {
1854
+ errors.push(format_error(REQ, taskDef));
1855
+ } else {
1856
+ if (! (Array.isArray(taskDef.a) && taskDef.a.length === 1 &&
1857
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1858
+ errors.push(format_error(A_REQ, taskDef));
1859
+ }
1860
+ if (! (Array.isArray(taskDef.out) && taskDef.out.length <= 1 &&
1861
+ taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
1862
+ errors.push(format_error(OUT_REQ, taskDef));
1863
+ }
1864
+ }
1865
+ return errors;
1866
+ };
1867
+
1868
+ WhenTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
1869
+ var self = this;
1870
+ this.nextFn = function (arg) {
1871
+ var args = Array.prototype.slice.call(arguments);
1872
+ vCon.saveResults(self.out, args);
1873
+ self.complete(args);
1874
+ contExec();
1875
+ };
1876
+ this.failFn = function (err) {
1877
+ handleTaskError(self, err);
1878
+ };
1879
+ };
1880
+
1881
+ WhenTask.prototype.exec = function exec(vCon, handleError, contExec) {
1882
+ try {
1883
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1884
+ //console.error('WhenTask.exec.args=', args);
1885
+ //console.error('WhenTask.exec.vCon=', vCon);
1886
+ this.start(args); //note the start time, args
1887
+ var arg = args[0]; // one value allowed
1888
+ if (arg && typeof(arg.then) === 'function') { // is a promise
1889
+ arg.then(this.nextFn, this.failFn);
1890
+ } else { // not a promise continue immediately
1891
+ this.nextFn(arg);
1892
+ }
1893
+ } catch (err) { //catch and handle the task error, calling final cb
1894
+ handleError(this, err);
1895
+ }
1896
+ };
1897
+
1898
+ return WhenTask;
1899
+
1900
+ });
1901
+
1902
+ /*global define:true */
1903
+
1904
+
1905
+
1906
+ define('react/finalcb-task',['./sprintf', 'util', './status', './event-manager'],
1907
+ function (sprintf, util, STATUS, EventManager) {
1908
+
1909
+ var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
1910
+
1911
+ function FinalCbTask(outTaskOptions) {
1912
+ var taskDef = outTaskOptions.taskDef;
1913
+ if (typeof(outTaskOptions.cbFunc) !== 'function') throw new Error('callback is not a function');
1914
+ var self = this;
1915
+ for (var k in taskDef) {
1916
+ if (true) self[k] = taskDef[k]; // if to make jshint happy
1917
+ }
1918
+ this.f = outTaskOptions.cbFunc;
1919
+ this.tasks = outTaskOptions.tasks;
1920
+ this.vCon = outTaskOptions.vCon;
1921
+ this.retValue = outTaskOptions.retValue;
1922
+ this.execOptions = outTaskOptions.execOptions;
1923
+ this.env = outTaskOptions.env;
1924
+ }
1925
+
1926
+ function format_error(errmsg, obj) {
1927
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1928
+ }
1929
+
1930
+
1931
+ FinalCbTask.validate = function (taskDef) {
1932
+ var errors = [];
1933
+ if (! (Array.isArray(taskDef.a) &&
1934
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1935
+ errors.push(format_error(OUTTASK_A_REQ, taskDef));
1936
+ }
1937
+ return errors;
1938
+ };
1939
+
1940
+ FinalCbTask.prototype.isReady = function () {
1941
+ return (this.tasks.every(function (t) { return (t.status === STATUS.COMPLETE); }));
1942
+ };
1943
+
1944
+ FinalCbTask.prototype.exec = function (err) {
1945
+ if (!this.f) return; //must have already been called
1946
+ if (err) {
1947
+ this.env.error = err;
1948
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_ERRORED, this.env);
1949
+ this.f.call(null, err); //call the final callback with the first error hit
1950
+ } else { // no error, call with args
1951
+ var vCon = this.vCon;
1952
+ var finalArgs = this.a.map(function (k) { return vCon.getVar(k); });
1953
+ finalArgs.unshift(null); //unshift err=null to front
1954
+ this.env.results = finalArgs;
1955
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_COMPLETE, this.env);
1956
+ this.f.apply(null, finalArgs);
1957
+ }
1958
+ this.f = null; // prevent multiple calls
1959
+ };
1960
+
1961
+ return FinalCbTask;
1962
+
1963
+ });
1964
+
1965
+ /*global define:true */
1966
+
1967
+
1968
+
1969
+ define('react/finalcb-first-task',['./sprintf', 'util', './status', './vcon', './event-manager'],
1970
+ function (sprintf, util, STATUS, VContext, EventManager) {
1971
+
1972
+ var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
1973
+
1974
+ function FinalCbFirstSuccTask(outTaskOptions) {
1975
+ var taskDef = outTaskOptions.taskDef;
1976
+ if (typeof(outTaskOptions.cbFunc) !== 'function') throw new Error('callback is not a function');
1977
+ var self = this;
1978
+ for (var k in taskDef) {
1979
+ if (true) self[k] = taskDef[k]; // if to make jshint happy
1980
+ }
1981
+ this.f = outTaskOptions.cbFunc;
1982
+ this.tasks = outTaskOptions.tasks;
1983
+ this.vCon = outTaskOptions.vCon;
1984
+ this.retValue = outTaskOptions.retValue;
1985
+ this.env = outTaskOptions.env;
1986
+ }
1987
+
1988
+ function format_error(errmsg, obj) {
1989
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1990
+ }
1991
+
1992
+ FinalCbFirstSuccTask.validate = function (taskDef) {
1993
+ var errors = [];
1994
+ if (! (Array.isArray(taskDef.a) &&
1995
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1996
+ errors.push(format_error(OUTTASK_A_REQ, taskDef));
1997
+ }
1998
+ return errors;
1999
+ };
2000
+
2001
+ /**
2002
+ is ready to exit when any task comes back with non-null defined value
2003
+ */
2004
+ FinalCbFirstSuccTask.prototype.isReady = function () {
2005
+ var lastres = this.vCon.getLastResults();
2006
+ if (!lastres) return false; // no results yet
2007
+ return (lastres.some(function (v) { return (v !== undefined && v !== null); }));
2008
+ };
2009
+
2010
+ FinalCbFirstSuccTask.prototype.exec = function (err) {
2011
+ if (!this.f) return; //must have already been called
2012
+ if (err) {
2013
+ this.env.error = err;
2014
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_ERRORED, this.env);
2015
+ this.f.call(null, err); //call the final callback with the first error hit
2016
+ } else { // no error, call with args
2017
+ var vCon = this.vCon;
2018
+ var finalArgs = this.a.map(function (k) { return vCon.getVar(k); });
2019
+ finalArgs.unshift(null); //unshift err=null to front
2020
+ this.env.results = finalArgs;
2021
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_COMPLETE, this.env);
2022
+ this.f.apply(null, finalArgs);
2023
+ }
2024
+ this.f = null; // prevent multiple calls
2025
+ };
2026
+
2027
+ return FinalCbFirstSuccTask;
2028
+
2029
+ });
2030
+
2031
+ /*global define:true */
2032
+
2033
+
2034
+
2035
+ define('react/task',['util', './sprintf', 'ensure-array', './cb-task', './promise-task',
2036
+ './ret-task', './when-task', './finalcb-task', './finalcb-first-task',
2037
+ './status', './error', './vcon', './event-manager'],
2038
+ function (util, sprintf, array, CbTask, PromiseTask,
2039
+ RetTask, WhenTask, FinalCbTask, FinalCbFirstSuccTask,
2040
+ STATUS, error, VContext, EventManager) {
2041
+
2042
+ var TASK_TYPES = {
2043
+ cb: CbTask,
2044
+ ret: RetTask,
2045
+ promise: PromiseTask,
2046
+ when: WhenTask
2047
+ };
2048
+
2049
+ var DEFAULT_TASK_NAME = 'task_%s'; // for unnamed tasks use task_idx, like task_0
2050
+
2051
+ function taskTypeKeys() { return Object.keys(TASK_TYPES); }
2052
+
2053
+ var OUT_TASK_TYPES = {
2054
+ finalcb: FinalCbTask, //first task is the default if no type specified in taskDef
2055
+ finalcbFirst: FinalCbFirstSuccTask
2056
+ };
2057
+ function outTaskTypeKeys() { return Object.keys(OUT_TASK_TYPES); }
2058
+
2059
+ var LOCAL_FN_MISSING = 'function: %s not found in locals or input params - task[%s]';
2060
+ var TASKDEF_IS_OBJECT = 'task must be an object';
2061
+ var NO_TASKS_RUNNING_WONT_COMPLETE = 'no tasks running, flow will not complete, remaining tasks: %s';
2062
+ var TASK_TYPE_SHOULD_MATCH = 'task.type should match one of ' +
2063
+ Object.keys(TASK_TYPES).join(', ');
2064
+
2065
+ function format_error(errmsg, obj) {
2066
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
2067
+ }
2068
+
2069
+ /**
2070
+ guess the missing types from params.
2071
+ Augments in place but also returns taskDef.
2072
+ If not specified then is 'cb'
2073
+ */
2074
+ function setMissingType(taskDef) {
2075
+ if (taskDef.type) return taskDef; //already set, return
2076
+ taskDef.type = 'cb';
2077
+ return taskDef;
2078
+ }
2079
+
2080
+ function setMissingOutTaskType(taskDef) {
2081
+ if (!taskDef.type) taskDef.type = Object.keys(OUT_TASK_TYPES)[0]; //use first outTask type as default
2082
+ }
2083
+
2084
+ function ensureAfterArrStrings(taskDef) { // convert any fn to str, and make sure is array
2085
+ if (!taskDef.after) return;
2086
+ var afterArr = array(taskDef.after); // ensure is array, null becomes []
2087
+ afterArr = afterArr.map(function (a) { return (typeof(a) === 'function') ? a.name : a; });
2088
+ taskDef.after = afterArr;
2089
+ }
2090
+
2091
+ /**
2092
+ @returns array of errors for taskDef, could be empty
2093
+ */
2094
+ function validate(taskDef) {
2095
+ if (!taskDef || typeof(taskDef) !== 'object') {
2096
+ return [format_error(TASKDEF_IS_OBJECT, taskDef)];
2097
+ }
2098
+ setMissingType(taskDef);
2099
+ ensureAfterArrStrings(taskDef);
2100
+ var errors = [];
2101
+ errors = errors.concat(validateTaskType(taskDef));
2102
+ errors = errors.concat(validateTask(taskDef));
2103
+ return errors;
2104
+ }
2105
+
2106
+ function validateTaskType(taskDef) {
2107
+ var errors = [];
2108
+ if (!Object.keys(TASK_TYPES).some(
2109
+ function (type) { return (taskDef.type === type); })) {
2110
+ errors.push(format_error(TASK_TYPE_SHOULD_MATCH, taskDef));
2111
+ }
2112
+ return errors;
2113
+ }
2114
+
2115
+ function validateTask(taskDef) {
2116
+ var errors = [];
2117
+ var taskCons = TASK_TYPES[taskDef.type];
2118
+ if (taskCons) {
2119
+ errors = errors.concat(taskCons.validate(taskDef));
2120
+ }
2121
+ return errors;
2122
+ }
2123
+
2124
+ function validateOutTask(taskDef) {
2125
+ var errors = [];
2126
+ setMissingOutTaskType(taskDef);
2127
+ var taskCons = OUT_TASK_TYPES[taskDef.type];
2128
+ errors = errors.concat(taskCons.validate(taskDef));
2129
+ return errors;
2130
+ }
2131
+
2132
+
2133
+ function validateLocalFunctions(inParams, taskDefs, locals) {
2134
+ var errors = [];
2135
+ function foo() { } //used to mock args as fns for validation check
2136
+ var mock_args = inParams.map(function (p) { return foo; }); //mock args with fns
2137
+ var vCon = VContext.create(mock_args, inParams, locals);
2138
+ var tasks = taskDefs.map(create);
2139
+ var tasksWFunctions = tasks.filter(function (t) { return (t.type !== 'when'); }); // non-when tasks need f
2140
+ tasksWFunctions.forEach(function (t, idx) {
2141
+ if (!t.functionExists(vCon)) { // error if function doesnt exist AND
2142
+ if (!t.isMethodCall()) errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx)); // not method OR
2143
+ else {
2144
+ var obj = t.getMethodObj(vCon);
2145
+ if (obj && obj !== foo) { // (has parent but not our mock)
2146
+ errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx));
2147
+ }
2148
+ }
2149
+ }
2150
+ });
2151
+ return errors;
2152
+ }
2153
+
2154
+ function fName(fn) {
2155
+ if (typeof(fn) === 'function') {
2156
+ return fn.name;
2157
+ }
2158
+ return (fn) ? fn : '';
2159
+ }
2160
+
2161
+ /**
2162
+ Name tasks that are not already named. Prenamed task uniquness validation
2163
+ will be done in validate.
2164
+
2165
+ This modifies the tasks with the new names.
2166
+
2167
+ @returns map of names to tasks
2168
+ */
2169
+ function nameTasks(tasks) { //name tasks that are not already named, validation done elsewhere, ret map
2170
+ var namesMap = tasks.reduce(function (map, t) {
2171
+ if (t.name) { map[t.name] = t; }
2172
+ return map;
2173
+ }, {});
2174
+ tasks.forEach(function (t, idx) {
2175
+ if (!t.name) { //not already named
2176
+ var name = fName(t.f);
2177
+ if (!name) name = sprintf(DEFAULT_TASK_NAME, idx);
2178
+ if (!name || namesMap[name]) {
2179
+ name = sprintf('%s_%s', name, idx); //if empty or already used, postfix with _idx
2180
+ }
2181
+ t.name = name;
2182
+ namesMap[name] = t;
2183
+ }
2184
+ });
2185
+ return namesMap;
2186
+ }
2187
+
2188
+ function create(taskDef) {
2189
+ var TaskConstructor = TASK_TYPES[taskDef.type];
2190
+ return new TaskConstructor(taskDef);
2191
+ }
2192
+
2193
+ function createOutTask(taskDef, cbFunc, tasks, vCon, execOptions, env) {
2194
+ setMissingOutTaskType(taskDef);
2195
+ var outTaskOptions = {
2196
+ taskDef: taskDef,
2197
+ cbFunc: cbFunc,
2198
+ tasks: tasks,
2199
+ vCon: vCon,
2200
+ execOptions: execOptions,
2201
+ env: env,
2202
+ TaskConstructor: OUT_TASK_TYPES[taskDef.type]
2203
+ };
2204
+ EventManager.global.emit(EventManager.TYPES.EXEC_OUTTASK_CREATE, outTaskOptions); // hook
2205
+ var TaskConstructor = outTaskOptions.TaskConstructor; // hook could have changed
2206
+ return new TaskConstructor(outTaskOptions);
2207
+ }
2208
+
2209
+ function createErrorHandler(vCon, outTask) {
2210
+ return function handleError(task, err) {
2211
+ task.status = STATUS.ERRORED;
2212
+ task.error = err;
2213
+ outTask.env.currentTask = task;
2214
+ outTask.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_ERRORED, task);
2215
+ var errWithMeta = error.augmentError(err, {task: task, vcon: vCon});
2216
+ outTask.exec(errWithMeta); //call the final callback with the first error hit
2217
+ };
2218
+ }
2219
+
2220
+ function findTasksReady(vCon, tasks, tasksByName) {
2221
+ return tasks.filter(function (t) { return t.isReady(vCon, tasksByName); });
2222
+ }
2223
+
2224
+ function execTasks(tasksReady, vCon, handleError, contExec) {
2225
+ tasksReady.forEach(function (t) { t.status = STATUS.READY; }); //set ready first, no double exec
2226
+ tasksReady.forEach(function (t) { t.exec(vCon, handleError, contExec); });
2227
+ }
2228
+
2229
+ /**
2230
+ this will be called if there are no tasks found to run,
2231
+ and it will check if there are still tasks running or ready
2232
+ (which means they will be running shortly), in which
2233
+ case everything is fine. If no tasks are running then
2234
+ call handleError since this will never complete.
2235
+ */
2236
+ function checkIfTasksRunning(vCon, tasks, handleError, env) {
2237
+ var tasksRunning = tasks.filter(function (t) {
2238
+ return (t.status === STATUS.RUNNING || t.status === STATUS.READY);
2239
+ });
2240
+ if (!tasksRunning.length) {
2241
+ var remainingTasks = tasks.filter(function (t) { return (!t.status); });
2242
+ var remainingTNames = remainingTasks.map(function (t) { return t.name; });
2243
+ var errMsg = sprintf(NO_TASKS_RUNNING_WONT_COMPLETE, remainingTNames.join(', '));
2244
+ var emptyTask = { env: env };
2245
+ handleError(emptyTask, new Error(errMsg));
2246
+ }
2247
+ }
2248
+
2249
+ function findReadyAndExec(vCon, tasks, tasksByName, handleError, contExec, env) {
2250
+ var tasksReady = findTasksReady(vCon, tasks, tasksByName);
2251
+ if (!tasksReady.length) checkIfTasksRunning(vCon, tasks, handleError, env); // no tasks to run, check if ok
2252
+ execTasks(tasksReady, vCon, handleError, contExec);
2253
+ }
2254
+
2255
+ function serializeTasks(tasks) { // conveniently set after for each task idx > 0
2256
+ nameTasks(tasks);
2257
+ tasks.forEach(function (t, idx, arr) { if (idx !== 0) t.after = [arr[idx - 1].name]; });
2258
+ return tasks;
2259
+ }
2260
+
2261
+ return {
2262
+ serializeTasks: serializeTasks,
2263
+ TASK_TYPES: TASK_TYPES,
2264
+ taskTypeKeys: taskTypeKeys,
2265
+ OUT_TASK_TYPES: OUT_TASK_TYPES,
2266
+ outTaskTypeKeys: outTaskTypeKeys,
2267
+ setMissingType: setMissingType,
2268
+ validate: validate,
2269
+ validateOutTask: validateOutTask,
2270
+ validateLocalFunctions: validateLocalFunctions,
2271
+ nameTasks: nameTasks,
2272
+ create: create,
2273
+ createOutTask: createOutTask,
2274
+ createErrorHandler: createErrorHandler,
2275
+ findReadyAndExec: findReadyAndExec
2276
+ };
2277
+
2278
+ });
2279
+
2280
+ /*global define:true */
2281
+
2282
+
2283
+
2284
+ define('react/validate',['util', './sprintf', 'ensure-array', './task'], function (util, sprintf, array, taskUtil) {
2285
+ /*jshint latedef:false */
2286
+
2287
+ var AST_IS_OBJECT = 'ast must be an object with inParams, tasks, and outTask';
2288
+ var INPARAMS_ARR_STR = 'ast.inParams must be an array of strings';
2289
+ var TASKS_ARR = 'ast.tasks must be an array of tasks';
2290
+ var NAMES_UNIQUE = 'ast.tasks that specify name need to be unique, duplicate:';
2291
+ var LOCALS_NOTNULL = 'ast.locals should not be null';
2292
+ var DUP_OUTPUTS = 'multiple tasks output the same param, must be unique. param';
2293
+ var MISSING_INPUTS = 'missing or mispelled variable referenced in flow definition: %s';
2294
+
2295
+ // match any of our literals true, false, int, float, quoted strings, or is property (has dot), match vcon.js
2296
+ var LITERAL_OR_PROP_RE = /^(true|false|this|null|\-?[0-9\.]+)$|'|"|\./i;
2297
+
2298
+ function format_error(errmsg, obj) {
2299
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
2300
+ }
2301
+
2302
+ /**
2303
+ true if is a literal name
2304
+ */
2305
+ function isLiteralOrProp(name) { // need to match what is in vcon.js, TODO consolidate?
2306
+ return LITERAL_OR_PROP_RE.test(name);
2307
+ }
2308
+
2309
+ /**
2310
+ validate the AST return Errors
2311
+ @example
2312
+ var validate = require('./validate');
2313
+ var errors = validate(ast);
2314
+ @returns array of errors, could be empty
2315
+ */
2316
+ function validate(ast) {
2317
+ if (!ast || !ast.inParams || !ast.tasks || !ast.outTask) return [AST_IS_OBJECT];
2318
+ var errors = [];
2319
+ errors = errors.concat(validateInParams(ast.inParams));
2320
+ errors = errors.concat(validateTasks(ast.tasks));
2321
+ errors = errors.concat(validateTaskNamesUnique(ast.tasks));
2322
+ errors = errors.concat(taskUtil.validateOutTask(ast.outTask));
2323
+ errors = errors.concat(validateLocals(ast.locals));
2324
+ if (errors.length === 0) { // if no errors do additional validation
2325
+ if (ast.outTask.type !== 'finalcbFirst') errors = errors.concat(validateOuputsUnique(ast.tasks));
2326
+ errors = errors.concat(taskUtil.validateLocalFunctions(ast.inParams, ast.tasks, ast.locals));
2327
+ errors = errors.concat(validateNoMissingNames(ast));
2328
+ }
2329
+ return errors;
2330
+ }
2331
+
2332
+ /**
2333
+ @returns array of errors, could be empty
2334
+ */
2335
+ function validateInParams(inParams) {
2336
+ if (!Array.isArray(inParams) ||
2337
+ !inParams.every(function (x) { return (typeof(x) === 'string'); })) {
2338
+ return [INPARAMS_ARR_STR];
2339
+ }
2340
+ return [];
2341
+ }
2342
+
2343
+ /**
2344
+ @returns array of errors, could be empty
2345
+ */
2346
+ function validateTasks(tasks) {
2347
+ if (!Array.isArray(tasks)) return [TASKS_ARR];
2348
+ var errors = [];
2349
+ tasks.forEach(function (t) {
2350
+ errors = errors.concat(taskUtil.validate(t));
2351
+ });
2352
+ return errors;
2353
+ }
2354
+
2355
+ function validateTaskNamesUnique(tasks) {
2356
+ if (!Array.isArray(tasks)) return [];
2357
+ var errors = [];
2358
+ var namedTasks = tasks.filter(function (t) { return (t.name); });
2359
+ var names = namedTasks.map(function (t) { return t.name; });
2360
+ names.reduce(function (accum, name) {
2361
+ if (accum[name]) errors.push(sprintf('%s %s', NAMES_UNIQUE, name));
2362
+ else accum[name] = true;
2363
+ return accum;
2364
+ }, {});
2365
+ return errors;
2366
+ }
2367
+
2368
+ function validateLocals(locals) {
2369
+ var errors = [];
2370
+ if (locals === null) errors.push(LOCALS_NOTNULL);
2371
+ return errors;
2372
+ }
2373
+
2374
+ function getOutputParams(taskDef) {
2375
+ return array(taskDef.out); //ensure array
2376
+ }
2377
+
2378
+ function validateOuputsUnique(taskDefs) {
2379
+ var errors = [];
2380
+ taskDefs.reduce(function (accum, t) {
2381
+ getOutputParams(t).forEach(function (param) {
2382
+ if (accum[param] !== undefined) errors.push(sprintf('%s: %s', DUP_OUTPUTS, param));
2383
+ else accum[param] = true;
2384
+ });
2385
+ return accum;
2386
+ }, {});
2387
+ return errors;
2388
+ }
2389
+
2390
+
2391
+ /**
2392
+ validate there are no missing or mispelled param names in any task inputs
2393
+ or the final task output
2394
+
2395
+ @return array of errors, or empty array if none
2396
+ */
2397
+ function validateNoMissingNames(ast) {
2398
+ var errors = [];
2399
+ var names = {};
2400
+ if (ast.locals) {
2401
+ names = Object.keys(ast.locals).reduce(function (accum, k) { // start with locals
2402
+ accum[k] = true;
2403
+ return accum;
2404
+ }, names);
2405
+ }
2406
+ ast.inParams.reduce(function (accum, p) { // add input params
2407
+ accum[p] = true;
2408
+ return accum;
2409
+ }, names);
2410
+ ast.tasks.reduce(function (accum, t) { // add task outputs
2411
+ return t.out.reduce(function (innerAccum, p) {
2412
+ innerAccum[p] = true;
2413
+ return innerAccum;
2414
+ }, accum);
2415
+ }, names);
2416
+
2417
+ // now we have all possible provided vars, check task inputs are accounted for
2418
+ ast.tasks.reduce(function (accum, t) { // for all tasks
2419
+ return t.a.reduce(function (innerAccum, p) { // for all in params, except property
2420
+ if (!isLiteralOrProp(p) && !names[p]) innerAccum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
2421
+ return innerAccum;
2422
+ }, accum);
2423
+ }, errors);
2424
+
2425
+ // now check the final task outputs
2426
+ ast.outTask.a.reduce(function (accum, p) { // for final task out params
2427
+ if (!isLiteralOrProp(p) && !names[p]) accum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
2428
+ return accum;
2429
+ }, errors);
2430
+ return errors;
2431
+ }
2432
+
2433
+ return validate;
2434
+
2435
+ });
2436
+
2437
+ /*global define:true */
2438
+
2439
+
2440
+
2441
+ define('react/core',['./eventemitter', './error', './validate', './task', './status',
2442
+ './vcon', './event-manager', './input-parser', './id', './sprintf'],
2443
+ function (EventEmitter, error, validate, taskUtil, STATUS,
2444
+ VContext, EventManager, inputParser, idGenerator, sprintf) {
2445
+
2446
+ var reactOptions = {
2447
+ stackTraceLimitMin: 30
2448
+ };
2449
+
2450
+ var reactEmitter = EventManager.global; // the top emitter
2451
+
2452
+ /**
2453
+ merge global react options with parsed options
2454
+ */
2455
+ function mergeOptions(parsedOptions) {
2456
+ return Object.keys(reactOptions).reduce(function (accum, k) {
2457
+ if (!accum[k]) accum[k] = reactOptions[k];
2458
+ return accum;
2459
+ }, parsedOptions);
2460
+ }
2461
+
2462
+ /**
2463
+ generate a flow name when one is not provided
2464
+ */
2465
+ function generateFlowName() {
2466
+ return sprintf('flow_%s', idGenerator.createUniqueId());
2467
+ }
2468
+
2469
+ /**
2470
+ Creates react function which the AST can be manipulated and then
2471
+ is ready to be executed. Can be used directly or a DSL can wrap this
2472
+ to provide the AST.
2473
+
2474
+ @example
2475
+ var react = require('react');
2476
+ var fn = react();
2477
+ var valid2 = fn.setAndValidateAST({
2478
+ name: 'optionalName',
2479
+ inParams: ['a', 'b'],
2480
+ tasks: [
2481
+ { type: 'cb', f: multiply, a: ['a', 'b'], out: ['c'] }
2482
+ ],
2483
+ outTask: { a: ['c'] }
2484
+ });
2485
+ console.log(fn.ast); // view
2486
+ fn(123, 456, cb);
2487
+ */
2488
+ function reactFactory() {
2489
+ if (arguments.length) throw new Error('react() takes no args, check API');
2490
+
2491
+ error.ensureStackTraceLimitSet(reactOptions.stackTraceLimitMin);
2492
+ var flowEmitter = EventManager.create();
2493
+ flowEmitter.parent = reactEmitter;
2494
+
2495
+ var ast = {
2496
+ name: undefined,
2497
+ inParams: [],
2498
+ tasks: [],
2499
+ outTask: {},
2500
+ locals: {}
2501
+ };
2502
+
2503
+ function setAndValidateAST(newAST) { //set AST then validate, ret error[]
2504
+ Object.keys(newAST).forEach(function (k) { ast[k] = newAST[k]; }); // copy all properties
2505
+ var errors = validate(ast);
2506
+ if (!errors.length) {
2507
+ if (!ast.name) ast.name = generateFlowName();
2508
+ taskUtil.nameTasks(ast.tasks); //run this so names can be checked in ast
2509
+ }
2510
+ if (Object.freeze) { //lets freeze the AST so plugin writers don't accidentally manip the ast
2511
+ Object.keys(newAST).forEach(function (k) {
2512
+ if (typeof(newAST[k]) === 'object') Object.freeze(newAST[k]);
2513
+ });
2514
+ Object.freeze(newAST);
2515
+ }
2516
+ flowEmitter.emit(EventManager.TYPES.AST_DEFINED, ast);
2517
+ return errors;
2518
+ }
2519
+
2520
+ function exec(arg1, arg2, argN, cb) { // called to execute the flow
2521
+ /*jshint validthis: true */
2522
+ var args = Array.prototype.slice.call(arguments);
2523
+ var env = {
2524
+ execId: idGenerator.createUniqueId(),
2525
+ args: args,
2526
+ ast: ast,
2527
+ flowEmitter: flowEmitter
2528
+ };
2529
+ env.name = ast.name || env.execId;
2530
+ flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_START, env); // hook
2531
+ var parsedInput = inputParser(args, ast);
2532
+ var vCon = VContext.create(parsedInput.args, ast.inParams, ast.locals, this); // create var ctx with in args & locals
2533
+
2534
+ env.parsedInput = parsedInput;
2535
+ env.options = mergeOptions(parsedInput.options);
2536
+ env.vCon = vCon;
2537
+ env.taskDefs = ast.tasks.slice(); // create copy
2538
+ env.outTaskDef = Object.create(ast.outTask); // create copy
2539
+ reactEmitter.emit(EventManager.TYPES.EXEC_TASKS_PRECREATE, env); // hook
2540
+
2541
+ var tasks = env.taskDefs.map(taskUtil.create);
2542
+ var tasksByName = taskUtil.nameTasks(tasks); // map names to working tasks
2543
+ var outTask = taskUtil.createOutTask(env.outTaskDef, parsedInput.cb, tasks, vCon, env.options, env);
2544
+ var handleError = taskUtil.createErrorHandler(vCon, outTask);
2545
+
2546
+ function contExec() {
2547
+ if (!outTask.f) { return; } //stop execution, we already hit an error, f was cleared
2548
+ if (outTask.isReady()) return outTask.exec(); // all tasks done, exec cb, return
2549
+ taskUtil.findReadyAndExec(vCon, tasks, tasksByName, handleError, contExec, env); //exec tasks that ready to run
2550
+ }
2551
+
2552
+ tasks.forEach(function (t) {
2553
+ t.id = idGenerator.createUniqueId();
2554
+ t.env = env;
2555
+ if (t.prepare) t.prepare(handleError, vCon, contExec, flowEmitter);
2556
+ }); // create callbacks
2557
+ contExec(); // start things off
2558
+ return outTask.retValue; // could return promise
2559
+ }
2560
+
2561
+ var reactFn = exec; // make the exec() the function returned
2562
+ reactFn.ast = ast; // put AST hanging off the fn so it can be inspected
2563
+ reactFn.setAndValidateAST = setAndValidateAST; // call to set AST and then validate
2564
+ reactFn.events = flowEmitter; // used to listen to execution events for this flow
2565
+ return reactFn;
2566
+ }
2567
+
2568
+ reactFactory.options = reactOptions; // global react options
2569
+ reactFactory.events = reactEmitter; // global react emitter
2570
+ return reactFactory; // module returns reactFactory to create a react fn
2571
+ });
2572
+
2573
+ /*global define:true */
2574
+
2575
+
2576
+
2577
+ define('react/parse',['./sprintf'], function (sprintf) {
2578
+
2579
+ function splitTrimFilterArgs(commaSepArgs) { //parse 'one, two' into ['one', 'two']
2580
+ if (!commaSepArgs) return [];
2581
+ return commaSepArgs.split(',') //split on commas
2582
+ .map(function (s) { return s.trim(); }) //trim
2583
+ .filter(function (s) { return (s); }); //filter out empty strings
2584
+ }
2585
+
2586
+ /**
2587
+ @param patternFn regex + fn or splitStr + fn
2588
+ */
2589
+ function parseReduce(accum, patternFn) {
2590
+ if (typeof(accum) !== 'string') return accum; // already matched
2591
+ var m = (patternFn.regex) ? patternFn.regex.exec(accum) : accum.split(patternFn.splitStr);
2592
+ if (m) return patternFn.fn(m, accum); // pass in matches and origStr, return result obj
2593
+ return accum; // no match, return str, will try next matcher
2594
+ }
2595
+
2596
+ function parseStr(str, parseMatchers, errStr) {
2597
+ var result = parseMatchers.reduce(parseReduce, str);
2598
+ if (typeof(result) !== 'string') { // matched
2599
+ return result;
2600
+ } else { // no match
2601
+ throw new Error(sprintf(errStr, str));
2602
+ }
2603
+ }
2604
+
2605
+ return {
2606
+ splitTrimFilterArgs: splitTrimFilterArgs,
2607
+ parseStr: parseStr
2608
+ };
2609
+
2610
+ });
2611
+
2612
+
2613
+ /*global define:true */
2614
+
2615
+
2616
+
2617
+ define('react/dsl',['./sprintf', './core', './parse', './task'],
2618
+ function (sprintf, core, parse, taskUtil) {
2619
+ /*jshint regexp: false */
2620
+
2621
+ var MISSING_NAME = 'first flow parameter should be the flow name, but found in/out def: %s';
2622
+ var INOUT_PARAMS_NO_MATCH = 'params in wrong format, wanted "foo, bar, cb -> err, baz" - found: %s';
2623
+ var MISSING_ERR = 'callback specified, but first out param was not "err", use for clarity. Found in/out def: %s';
2624
+ var MISSING_CB = 'found err param, but cb/callback is not specified, is this cb-style async or sync function? Found in/out def: %s';
2625
+ var EXTRA_TASKARG = 'extra unmatched task arg: %s';
2626
+
2627
+ var INOUT_RE = /\->/; // used to detect missing name, in/out as first arg
2628
+ var CB_NAMES_RE = /^cb|callback$/i; //cb, Cb, CB, callback, Callback
2629
+ var ERR_NAMES_RE = /^err$/i; // err, ERR, Err, ...
2630
+
2631
+ function filterOutTrailingCbParam(args) { // if has trailing cb | callback param, filter it out
2632
+ if (args.length && args[args.length - 1].match(CB_NAMES_RE)) args.pop();
2633
+ return args;
2634
+ }
2635
+
2636
+ function filterOutLeadingErrParam(args) { // if leading err param, filter it out
2637
+ if (args.length && args[0].match(ERR_NAMES_RE)) args.shift();
2638
+ return args;
2639
+ }
2640
+
2641
+ var inOutDefParse = {
2642
+ splitStr: '->',
2643
+ fn: function (m, origStr) {
2644
+ var inParams = parse.splitTrimFilterArgs(m[0]);
2645
+ var lastParam = inParams[inParams.length - 1];
2646
+ var type = (lastParam && CB_NAMES_RE.test(lastParam)) ? 'cb' : 'ret';
2647
+ var outParams = parse.splitTrimFilterArgs(m[1]);
2648
+ var firstOutParam = outParams[0];
2649
+ if (type === 'cb' && (!firstOutParam || !ERR_NAMES_RE.test(firstOutParam))) {
2650
+ throw new Error(sprintf(MISSING_ERR, origStr)); // found cb, but no err param
2651
+ } else if (type === 'ret' && firstOutParam && ERR_NAMES_RE.test(firstOutParam)) {
2652
+ throw new Error(sprintf(MISSING_CB, origStr)); // found err but not cb param
2653
+ }
2654
+ return {
2655
+ type: type,
2656
+ inDef: filterOutTrailingCbParam(inParams),
2657
+ outDef: filterOutLeadingErrParam(outParams)
2658
+ };
2659
+ }
2660
+ };
2661
+
2662
+ function parseInOutParams(str) {
2663
+ var objDef = parse.parseStr(str, [inOutDefParse], INOUT_PARAMS_NO_MATCH);
2664
+ objDef.inDef = filterOutTrailingCbParam(objDef.inDef);
2665
+ return objDef;
2666
+ }
2667
+
2668
+ function parseTasks(arr) {
2669
+ var tasks = [];
2670
+ var fn, obj, result;
2671
+ while (arr.length >= 2) {
2672
+ obj = {};
2673
+ fn = arr.shift();
2674
+ result = parseInOutParams(arr.shift());
2675
+ if (typeof(arr[0]) === 'object') obj = arr.shift(); // has options, use as obj
2676
+ obj.f = fn;
2677
+ obj.a = result.inDef;
2678
+ var type = result.type;
2679
+ obj.out = result.outDef;
2680
+ obj.type = type;
2681
+ tasks.push(obj);
2682
+ }
2683
+ if (arr.length) throw new Error(sprintf(EXTRA_TASKARG, arr[0]));
2684
+ return tasks;
2685
+ }
2686
+
2687
+ /**
2688
+ Parse the variable arguments into in/out params, options, tasks
2689
+ */
2690
+ function parseVargs(vargs) {
2691
+ var inOutParamStr = vargs.shift() || '';
2692
+ // if next arg is object, shift it off as options
2693
+ var options = (vargs.length && typeof(vargs[0]) === 'object') ? vargs.shift() : { };
2694
+ var taskDefArr = vargs; // rest are for the tasks
2695
+ var defObj = {
2696
+ inOutParamStr: inOutParamStr,
2697
+ taskDefArr: taskDefArr,
2698
+ options: options
2699
+ };
2700
+ return defObj;
2701
+ }
2702
+
2703
+
2704
+ function dslDefine(name, arg1, arg2, argN) {
2705
+ var reactFn = core();
2706
+ if (name && INOUT_RE.test(name)) throw new Error(sprintf(MISSING_NAME, name));
2707
+ var defObj = parseVargs(Array.prototype.slice.call(arguments, 1)); // name, already used
2708
+ var inOutDef = parseInOutParams(defObj.inOutParamStr);
2709
+ var ast = {
2710
+ name: name,
2711
+ inParams: inOutDef.inDef,
2712
+ tasks: parseTasks(defObj.taskDefArr),
2713
+ outTask: { a: inOutDef.outDef }
2714
+ };
2715
+ if (defObj.options) Object.keys(defObj.options).forEach(function (k) { ast[k] = defObj.options[k]; });
2716
+ var errors = reactFn.setAndValidateAST(ast);
2717
+ if (errors.length) {
2718
+ var errorStr = errors.join('\n');
2719
+ throw new Error(errorStr);
2720
+ }
2721
+ return reactFn;
2722
+ }
2723
+
2724
+ function selectFirst(name, arg1, arg2, argN) {
2725
+ var reactFn = core();
2726
+ var defObj = parseVargs(Array.prototype.slice.call(arguments, 1)); // name, already used
2727
+ var inOutDef = parseInOutParams(defObj.inOutParamStr);
2728
+ var tasks = taskUtil.serializeTasks(parseTasks(defObj.taskDefArr));
2729
+ var ast = {
2730
+ name: name,
2731
+ inParams: inOutDef.inDef,
2732
+ tasks: tasks,
2733
+ outTask: { type: 'finalcbFirst', a: inOutDef.outDef },
2734
+ };
2735
+ if (defObj.options) Object.keys(defObj.options).forEach(function (k) { ast[k] = defObj.options[k]; });
2736
+ var errors = reactFn.setAndValidateAST(ast);
2737
+ if (errors.length) {
2738
+ var errorStr = errors.join('\n');
2739
+ throw new Error(errorStr);
2740
+ }
2741
+ return reactFn;
2742
+ }
2743
+
2744
+ dslDefine.selectFirst = selectFirst;
2745
+ return dslDefine;
2746
+
2747
+ });
2748
+
2749
+ /*global define:true */
2750
+
2751
+
2752
+
2753
+ define('react/track-tasks',[], function () {
2754
+
2755
+ /**
2756
+ Track the tasks, start, complete, args, results, elapsed time
2757
+ Emits events that can be monitored
2758
+
2759
+ - track start and complete
2760
+ - record args each task was called with
2761
+ - record results at completion
2762
+ - record start, end, and calc elapsed time
2763
+ - emits flow.begin with flowEnv
2764
+ - emits task.begin with task
2765
+ - emits task.complete with task
2766
+ - emits flow complete with flowEnv
2767
+ - emits flow errored with flowEnv
2768
+
2769
+ @example
2770
+ var react = require('react');
2771
+ react.trackTasks(); // enable task and flow tracking
2772
+ */
2773
+
2774
+
2775
+ var trackingTasks = false;
2776
+
2777
+ function trackTasks(react) {
2778
+ if (trackingTasks) return; // already tracking
2779
+ trackingTasks = true;
2780
+
2781
+ react.events.on(react.events.TYPES.EXEC_FLOW_START, function (env) {
2782
+ env.startTime = Date.now();
2783
+ env.flowEmitter.emit(react.events.TYPES.FLOW_BEGIN, env); //fire public ev
2784
+ });
2785
+
2786
+ react.events.on(react.events.TYPES.EXEC_TASK_START, function (task) {
2787
+ task.startTime = Date.now();
2788
+ task.env.flowEmitter.emit(react.events.TYPES.TASK_BEGIN, task); //fire public ev
2789
+ });
2790
+
2791
+ react.events.on(react.events.TYPES.EXEC_TASK_COMPLETE, function (task) {
2792
+ task.endTime = Date.now();
2793
+ task.elapsedTime = task.endTime - task.startTime;
2794
+ task.env.flowEmitter.emit(react.events.TYPES.TASK_COMPLETE, task); // fire public ev
2795
+ });
2796
+
2797
+ react.events.on(react.events.TYPES.EXEC_TASK_ERRORED, function (task) {
2798
+ task.endTime = Date.now();
2799
+ task.elapsedTime = task.endTime - task.startTime;
2800
+ task.env.flowEmitter.emit(react.events.TYPES.TASK_ERRORED, task); // fire public ev
2801
+ });
2802
+
2803
+ react.events.on(react.events.TYPES.EXEC_FLOW_COMPLETE, function (env) {
2804
+ env.endTime = Date.now();
2805
+ env.elapsedTime = env.endTime - env.startTime;
2806
+ env.flowEmitter.emit(react.events.TYPES.FLOW_COMPLETE, env); //fire public ev
2807
+ });
2808
+
2809
+ react.events.on(react.events.TYPES.EXEC_FLOW_ERRORED, function (env) {
2810
+ env.endTime = Date.now();
2811
+ env.elapsedTime = env.endTime - env.startTime;
2812
+ env.flowEmitter.emit(react.events.TYPES.FLOW_ERRORED, env); //fire public ev
2813
+ });
2814
+
2815
+ }
2816
+
2817
+ return trackTasks;
2818
+
2819
+ });
2820
+
2821
+ /*global define:true */
2822
+
2823
+
2824
+
2825
+ define('react/log-events',['util'], function (util) { // TODO replace util.inspect with something portable to browser
2826
+
2827
+ var logEventsMod = { };
2828
+
2829
+ /**
2830
+ Log events to console.error
2831
+
2832
+ @example
2833
+ var react = require('react');
2834
+ react.logEvents(); // log all task and flow events on all react functions
2835
+ react.logEvents('task.*'); // log all task events on all react functions
2836
+ react.logEvents(flowFn); // log all task and flow events on flowFn only
2837
+ react.logEvents(flowFn, 'flow.*'); // log all flow events on flowFn only
2838
+ */
2839
+
2840
+ var ALL_FLOW_EVENTS = 'flow.*';
2841
+ var ALL_TASK_EVENTS = 'task.*';
2842
+ var FLOW_RE = /^flow\./;
2843
+
2844
+ function flowLog(obj) {
2845
+ /*jshint validthis: true */
2846
+ var time = new Date();
2847
+ time.setTime(obj.time);
2848
+ var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
2849
+ var eventTimeStr = time.toISOString();
2850
+ if (this.event === 'flow.complete') {
2851
+ var env = obj;
2852
+ console.error('%s: %s \tmsecs: %s \n\targs: %s \n\tresults: %s\n',
2853
+ this.event, env.name, env.elapsedTime, util.inspect(argsNoCb), util.inspect(env.results));
2854
+ } else {
2855
+ var name = obj.name;
2856
+ var args = obj.args;
2857
+ console.error('%s: %s \n\targs: %s\n', this.event, name, util.inspect(argsNoCb));
2858
+ }
2859
+ }
2860
+
2861
+ function taskLog(obj) {
2862
+ /*jshint validthis: true */
2863
+ var time = new Date();
2864
+ time.setTime(obj.time);
2865
+ var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
2866
+ var eventTimeStr = time.toISOString();
2867
+ if (this.event === 'task.complete') {
2868
+ var task = obj;
2869
+ console.error('%s: %s:%s \tmsecs: %s \n\targs: %s \n\tresults: %s\n',
2870
+ this.event, task.env.name, task.name, task.elapsedTime, util.inspect(argsNoCb), util.inspect(task.results));
2871
+ } else {
2872
+ var name = obj.name;
2873
+ var args = obj.args;
2874
+ console.error('%s: %s:%s \n\targs: %s\n', this.event, obj.env.name, obj.name, util.inspect(argsNoCb));
2875
+ }
2876
+
2877
+ }
2878
+
2879
+ /**
2880
+ Log flow and task events for a flowFn or all of react.
2881
+ If called multiple times, remove previous listener (if any) before
2882
+ adding.
2883
+
2884
+ @example
2885
+ var react = require('react');
2886
+ react.logEvents(flowFn, eventWildcard); //log events on flowfn matching wildcard
2887
+
2888
+ @param flowFn Flow function or global react object
2889
+ @param eventWildcard wildcarded event type, if not provided use flow.* and task.*
2890
+ */
2891
+ function logEvents(flowFn, eventWildcard) {
2892
+ if (!flowFn) throw new Error('flowFn is required');
2893
+ if (eventWildcard && eventWildcard !== '*') {
2894
+ var logFn = (FLOW_RE.test(eventWildcard)) ? flowLog : taskLog;
2895
+ flowFn.events.removeListener(eventWildcard, logFn);
2896
+ flowFn.events.on(eventWildcard, logFn);
2897
+ } else { // none provided, use flow.* and task.*
2898
+ //output events as tasks start and complete
2899
+ flowFn.events.removeListener(ALL_FLOW_EVENTS, flowLog);
2900
+ flowFn.events.on(ALL_FLOW_EVENTS, flowLog);
2901
+ flowFn.events.removeListener(ALL_TASK_EVENTS, taskLog);
2902
+ flowFn.events.on(ALL_TASK_EVENTS, taskLog);
2903
+ }
2904
+ }
2905
+
2906
+ logEventsMod.logEvents = logEvents;
2907
+ return logEventsMod;
2908
+
2909
+ });
2910
+
2911
+ /*global define:true */
2912
+
2913
+
2914
+
2915
+ define('react/promise-resolve',[], function () {
2916
+
2917
+ /**
2918
+ Auto resolve promises passed in as arguments to the flow
2919
+
2920
+ - Detects promises by checking for .then()
2921
+ - Create promise name for param (param__promise)
2922
+ - moves existing vCon promise into the param__promise
2923
+ - creates WhenTask which resolves param__promise into param
2924
+ */
2925
+
2926
+
2927
+ var PROMISE_SUFFIX = '__promise'; // added to param names that are promises
2928
+
2929
+ var resolvingPromises = false;
2930
+
2931
+ function resolvePromises(react) {
2932
+ if (resolvingPromises) return; // already resolving
2933
+ resolvingPromises = true;
2934
+
2935
+ react.events.on(react.events.TYPES.EXEC_TASKS_PRECREATE, function (env) {
2936
+ var vConValues = env.vCon.values;
2937
+ var promiseParams = env.ast.inParams.filter(function (p) {
2938
+ var value = vConValues[p];
2939
+ return (value && typeof(value.then) === 'function');
2940
+ });
2941
+ promiseParams.forEach(function (p) {
2942
+ var promiseName = p + PROMISE_SUFFIX;
2943
+ vConValues[promiseName] = vConValues[p];
2944
+ vConValues[p] = undefined;
2945
+ env.taskDefs.push({
2946
+ type: 'when',
2947
+ a: [promiseName],
2948
+ out: [p]
2949
+ });
2950
+ });
2951
+ });
2952
+
2953
+ }
2954
+
2955
+ return resolvePromises;
2956
+
2957
+ });
2958
+
2959
+ /*global define:true */
2960
+
2961
+
2962
+
2963
+ define('react/event-collector',[], function () {
2964
+
2965
+ /**
2966
+ create an instance of the event collector
2967
+ */
2968
+ function instantiate(react) {
2969
+ react.trackTasks(); // enable task tracking
2970
+
2971
+ var AST_EVENTS_RE = /^ast\./;
2972
+ var TASK_EVENTS_RE = /^task\./;
2973
+ var FLOW_EVENTS_RE = /^flow\./;
2974
+
2975
+ /**
2976
+ Accumulator to make it easy to capture events
2977
+
2978
+ @example
2979
+ var react = require('react');
2980
+ var collector = react.createEventCollector();
2981
+ collector.capture(); // capture all flow and task events for all react flows
2982
+ collector.capture('flow.*'); // capture all flow events for all react flows
2983
+ collector.capture(flowFn, 'task.*'); // capture task events on a flow
2984
+ collector.capture(flowFn, 'flow.*'); // add capture flow events on a flow
2985
+ var events = collector.list(); // retrieve the list of events
2986
+ collector.clear(); // clear the list of events;
2987
+ */
2988
+ function EventCollector() {
2989
+ this.events = [];
2990
+ }
2991
+
2992
+ /**
2993
+ register listener to capture events for a specific flow
2994
+ @param flowFn the react flow function or can pass global react
2995
+ @param eventId event id or wildcarded id
2996
+ */
2997
+ EventCollector.prototype.capture = function (flowFn, eventId) {
2998
+ /*jshint validthis: true */
2999
+ if (!eventId && typeof(flowFn) === 'string') { // only eventId provided
3000
+ eventId = flowFn;
3001
+ flowFn = react; // global react
3002
+ } else if (!flowFn) flowFn = react; // global react
3003
+ if (!eventId) eventId = '*'; // default to all
3004
+ var emitter = flowFn.events;
3005
+ var self = this;
3006
+ function accumEvents(obj) {
3007
+ var eventObject = {
3008
+ event: this.event,
3009
+ time: Date.now()
3010
+ };
3011
+ if (FLOW_EVENTS_RE.test(this.event)) {
3012
+ eventObject.env = obj;
3013
+ } else if (TASK_EVENTS_RE.test(this.event)) {
3014
+ eventObject.task = obj;
3015
+ } else if (AST_EVENTS_RE.test(this.event)) {
3016
+ eventObject.ast = obj;
3017
+ }
3018
+ self.events.push(eventObject);
3019
+ }
3020
+ emitter.on(eventId, accumEvents);
3021
+ };
3022
+
3023
+ EventCollector.prototype.list = function () {
3024
+ return this.events;
3025
+ };
3026
+
3027
+ EventCollector.prototype.clear = function () {
3028
+ this.events = []; // clear
3029
+ };
3030
+
3031
+ return new EventCollector();
3032
+ }
3033
+
3034
+ return instantiate; // return the factory for creating EventCollector
3035
+
3036
+ });
3037
+
3038
+ /*global define:true */
3039
+
3040
+
3041
+
3042
+ define('react',['./core', './dsl', './track-tasks', './log-events', './promise-resolve', './event-collector'],
3043
+ function (core, dsl, trackTasksFn, logEventsMod, resolvePromisesFn, eventCollectorFactory) {
3044
+
3045
+ var react = dsl; // core + default dsl
3046
+
3047
+ /**
3048
+ Enable detection of promises and resolution
3049
+ */
3050
+ function resolvePromises() {
3051
+ resolvePromisesFn(react);
3052
+ }
3053
+
3054
+ /**
3055
+ Enable tracking of tasks and flow execution, emitting events and
3056
+ tracking start, end, elapsed time
3057
+ */
3058
+ function trackTasks() {
3059
+ trackTasksFn(react);
3060
+ }
3061
+
3062
+ /**
3063
+ If called, load the built-in plugin for log events and invoke
3064
+
3065
+ @param flowFn [function] if not provided uses global react
3066
+ @param eventWildcard [string] pattern to log events for
3067
+ */
3068
+ function logEvents(flowFn, eventWildcard) {
3069
+ if (!eventWildcard && typeof(flowFn) === 'string') { // only wildcard provided
3070
+ eventWildcard = flowFn;
3071
+ flowFn = undefined;
3072
+ }
3073
+ if (!flowFn) flowFn = react; // use global
3074
+ trackTasks();
3075
+ return logEventsMod.logEvents(flowFn, eventWildcard);
3076
+ }
3077
+
3078
+ /**
3079
+ Instantiate an event collector
3080
+ */
3081
+ function createEventCollector() {
3082
+ return eventCollectorFactory(react);
3083
+ }
3084
+
3085
+ react.options = core.options; // global react options
3086
+ react.events = core.events; // global react event emitter
3087
+ react.logEvents = logEvents; // enable event logging
3088
+ react.resolvePromises = resolvePromises; // enable promise resolution
3089
+ react.trackTasks = trackTasks; // enable tracking of tasks
3090
+ react.createEventCollector = createEventCollector; // create instance of EventCollector
3091
+ return react;
3092
+
3093
+ });