react 0.5.1 → 0.6.2

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 (88) hide show
  1. package/.travis.yml +5 -0
  2. package/Jakefile.js +39 -0
  3. package/README.md +17 -24
  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 +3094 -0
  8. package/dist/react.min.js +22 -0
  9. package/doc/advanced.md +2 -2
  10. package/doc/simple.dot +25 -0
  11. package/doc/simple.png +0 -0
  12. package/examples/using-events1.js +1 -1
  13. package/lib/base-task.js +116 -110
  14. package/lib/cb-task.js +71 -67
  15. package/lib/core.js +116 -117
  16. package/lib/dsl.js +120 -115
  17. package/lib/error.js +44 -36
  18. package/lib/event-collector.js +69 -56
  19. package/lib/event-manager.js +69 -56
  20. package/lib/eventemitter.js +20 -0
  21. package/lib/finalcb-first-task.js +56 -53
  22. package/lib/finalcb-task.js +55 -51
  23. package/lib/id.js +18 -7
  24. package/lib/input-parser.js +49 -41
  25. package/lib/log-events.js +79 -73
  26. package/lib/parse.js +34 -25
  27. package/lib/promise-resolve.js +42 -27
  28. package/lib/promise-task.js +78 -74
  29. package/lib/react.js +59 -0
  30. package/lib/ret-task.js +59 -55
  31. package/lib/sprintf.js +18 -0
  32. package/lib/status.js +11 -2
  33. package/lib/task.js +215 -217
  34. package/lib/track-tasks.js +72 -58
  35. package/lib/validate.js +136 -136
  36. package/lib/vcon.js +78 -69
  37. package/lib/when-task.js +69 -65
  38. package/package.json +10 -9
  39. package/src/dist.build.requirejs +20 -0
  40. package/test/ast.mocha.js +136 -0
  41. package/test/cb-task.mocha.js +220 -0
  42. package/test/core-deferred.mocha.js +143 -0
  43. package/test/core-when.mocha.js +96 -0
  44. package/test/core.mocha.js +589 -0
  45. package/test/dsl.mocha.js +350 -0
  46. package/test/event-manager.mocha.js +119 -0
  47. package/test/exec-options.mocha.js +48 -0
  48. package/test/finalcb-task.mocha.js +58 -0
  49. package/test/input-parser.mocha.js +86 -0
  50. package/test/mocha.opts +2 -0
  51. package/test/module-use.mocha.js +147 -0
  52. package/test/promise-auto-resolve.mocha.js +68 -0
  53. package/test/ret-task.mocha.js +220 -0
  54. package/test/task.mocha.js +42 -0
  55. package/test/validate-cb-task.mocha.js +100 -0
  56. package/test/validate-ret-task.mocha.js +110 -0
  57. package/test/validate.mocha.js +324 -0
  58. package/test/vcon.mocha.js +193 -0
  59. package/vendor/chai/chai.js +2038 -0
  60. package/vendor/jquery/jquery-1.7.1.js +9266 -0
  61. package/vendor/jquery/jquery-1.7.1.min.js +4 -0
  62. package/vendor/mocha/mocha.css +135 -0
  63. package/vendor/mocha/mocha.js +3589 -0
  64. package/vendor/node/util.js +531 -0
  65. package/vendor/requirejs/require.js +2053 -0
  66. package/vendor/requirejs/require.min.js +33 -0
  67. package/doc/default-simple.dot +0 -19
  68. package/doc/default-simple.dot.png +0 -0
  69. package/react.js +0 -40
  70. package/test/ast.test.js +0 -95
  71. package/test/cb-task.test.js +0 -197
  72. package/test/core-deferred.test.js +0 -134
  73. package/test/core-promised.test.js +0 -132
  74. package/test/core-when.test.js +0 -84
  75. package/test/core.test.js +0 -593
  76. package/test/dsl.test.js +0 -330
  77. package/test/event-manager.test.js +0 -102
  78. package/test/exec-options.test.js +0 -33
  79. package/test/finalcb-task.test.js +0 -38
  80. package/test/input-parser.test.js +0 -66
  81. package/test/module-use.test.js +0 -134
  82. package/test/promise-auto-resolve.test.js +0 -52
  83. package/test/ret-task.test.js +0 -199
  84. package/test/task.test.js +0 -21
  85. package/test/validate-cb-task.test.js +0 -74
  86. package/test/validate-ret-task.test.js +0 -83
  87. package/test/validate.test.js +0 -295
  88. package/test/vcon.test.js +0 -173
package/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: node_js
2
+ node_js:
3
+ - 0.6
4
+ - 0.7
5
+ - 0.8
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.
@@ -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,6 +186,12 @@ See the [Advanced React](https://github.com/jeffbski/react/blob/master/doc/advan
182
186
 
183
187
  ## Status
184
188
 
189
+ - 2012-09-12 - Upgrade RequireJS@2.0.6, mocha@1.4.2, chai@1.2.0, jake@0.3.16. Update travis config to include Node 0.8 (v0.6.2)
190
+ - 2012-04-25 - Browser compatibility issue with process check, (v0.6.1)
191
+ - 2012-04-05 - Remove dependency on sprint, use util.format
192
+ - 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)
193
+ - 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
194
+ - 2012-03-12 - Pass ast.define events to process (v0.5.2)
185
195
  - 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
196
  - 2012-01-17 - Additional documentation (v0.3.5)
187
197
  - 2012-01-16 - Refine events and create logging plugin (v0.3.3)
@@ -193,29 +203,12 @@ See the [Advanced React](https://github.com/jeffbski/react/blob/master/doc/advan
193
203
 
194
204
  ## Test Results
195
205
 
206
+ [![Build Status](https://secure.travis-ci.org/jeffbski/react.png?branch=master)](http://travis-ci.org/jeffbski/react)
207
+
196
208
  ```bash
197
- ok ast.test.js .................... 15/15
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 ................. 108/108
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 ........................... 466/466
217
-
218
- ok
209
+ mocha
210
+
211
+ 178 tests complete
219
212
  ```
220
213
 
221
214
  ## 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,3094 @@
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
+
1151
+ /*global define:true sprint:true */
1152
+
1153
+
1154
+
1155
+ define('react/sprintf',['util'], function (util) {
1156
+
1157
+ /**
1158
+ Abstract the details of getting a sprintf function.
1159
+ Currently using the simple format capabilities of node's util.format
1160
+ */
1161
+
1162
+ var sprintf = util.format;
1163
+ return sprintf;
1164
+
1165
+ });
1166
+ (function (root, factory) {
1167
+ /*global define:true */
1168
+
1169
+ if (typeof exports === 'object') {
1170
+ // Node. Does not work with strict CommonJS, but
1171
+ // only CommonJS-like enviroments that support module.exports,
1172
+ // like Node.
1173
+ module.exports = factory();
1174
+ } else if (typeof define === 'function' && define.amd) {
1175
+ // AMD. Register as an anonymous module.
1176
+ define('ensure-array',[], factory);
1177
+ } else {
1178
+ // Browser globals
1179
+ root.ensureArray = factory();
1180
+ }
1181
+ }(this, function () {
1182
+
1183
+
1184
+ function ensureArray(a, b, n) {
1185
+ if (arguments.length === 0) return []; //no args, ret []
1186
+ if (arguments.length === 1) { //single argument
1187
+ if (a === undefined || a === null) return []; // undefined or null, ret []
1188
+ if (Array.isArray(a)) return a; // isArray, return it
1189
+ }
1190
+ return Array.prototype.slice.call(arguments); //return array with copy of all arguments
1191
+ }
1192
+
1193
+ return ensureArray;
1194
+ }));
1195
+
1196
+
1197
+
1198
+ /*global define:true */
1199
+
1200
+
1201
+
1202
+ define('react/status',[], function () {
1203
+
1204
+ var STATUS = { READY: 'ready', RUNNING: 'running', ERRORED: 'errored', COMPLETE: 'complete' };
1205
+
1206
+ return STATUS;
1207
+
1208
+ });
1209
+
1210
+ /*global define:true process:false*/
1211
+
1212
+
1213
+
1214
+ define('react/event-manager',['./eventemitter'], function (EventEmitter) {
1215
+ /*jshint regexp:false */
1216
+
1217
+ var EVENT_EMITTER2_CONFIG = {
1218
+ wildcard: true, // should the event emitter use wildcards.
1219
+ delimiter: '.', // the delimiter used to segment namespaces, defaults to `.`.
1220
+ maxListeners: 30 // the max number of listeners that can be assigned to an event, defaults to 10.
1221
+ };
1222
+
1223
+ var PASS_EVENTS_PROCESS_RE = /^ast.defined$/; // events to pass up to global process
1224
+
1225
+ var TYPES = {
1226
+ // Flow monitoring events and their params
1227
+ AST_DEFINED: 'ast.defined', // ast
1228
+ FLOW_BEGIN: 'flow.begin', // env
1229
+ TASK_BEGIN: 'task.begin', // task
1230
+ TASK_COMPLETE: 'task.complete', // task
1231
+ TASK_ERRORED: 'task.errored', // task
1232
+ FLOW_COMPLETE: 'flow.complete', // env
1233
+ FLOW_ERRORED: 'flow.errored', // env
1234
+
1235
+ // Internal Hooks
1236
+ EXEC_FLOW_START: 'exec.flow.start', // env
1237
+ EXEC_INPUT_PREPROCESS: 'exec.input.preprocess', // parsedInput
1238
+ EXEC_TASKS_PRECREATE: 'exec.tasks.precreate', // env
1239
+ EXEC_OUTTASK_CREATE: 'exec.outTask.create', // outTaskOptions
1240
+ EXEC_TASK_START: 'exec.task.start', // task
1241
+ EXEC_TASK_COMPLETE: 'exec.task.complete', // task
1242
+ EXEC_TASK_ERRORED: 'exec.task.errored', // task
1243
+ EXEC_FLOW_COMPLETE: 'exec.flow.complete', // env
1244
+ EXEC_FLOW_ERRORED: 'exec.flow.errored' // env
1245
+ };
1246
+
1247
+ /**
1248
+ Event manager which emits events up to its parent if exists.
1249
+ Allows a hierarchy of emitters, which communicate up the
1250
+ chain.
1251
+ */
1252
+ function EventManager() {
1253
+ }
1254
+
1255
+ EventManager.create = function () { return new EventManager(); };
1256
+
1257
+ EventManager.TYPES = TYPES;
1258
+ EventManager.prototype.TYPES = TYPES;
1259
+
1260
+ EventManager.prototype.isEnabled = function () { // if has listener or an ancestor has listener
1261
+ return !!(this.emitter || (this.parent && this.parent.isEnabled()));
1262
+ };
1263
+
1264
+ /**
1265
+ Add listener. Wildcard events are allowed like 'foo.*'
1266
+ Use '*' to listen to any event
1267
+ */
1268
+ EventManager.prototype.on = function (event, listener) {
1269
+ if (!this.emitter) this.emitter = new EventEmitter(EVENT_EMITTER2_CONFIG);
1270
+ if (event === '*') this.emitter.onAny(listener);
1271
+ else this.emitter.on(event, listener);
1272
+ };
1273
+
1274
+ EventManager.prototype.emit = function (event, arg1, arg2, argN) {
1275
+ if (event === undefined) throw new Error('event is undefined');
1276
+ if (this.emitter) this.emitter.emit.apply(this.emitter, arguments);
1277
+ if (this.parent && this.parent.isEnabled()) this.parent.emit.apply(this.parent, arguments);
1278
+ if (PASS_EVENTS_PROCESS_RE.test(event) && typeof(process) !== 'undefined' && process.emit) {
1279
+ process.emit.apply(process, arguments); // pass some to process
1280
+ }
1281
+ };
1282
+
1283
+ EventManager.prototype.removeListener = function (event, listener) {
1284
+ if (this.emitter) this.emitter.removeListener.apply(this.emitter, arguments);
1285
+ };
1286
+
1287
+
1288
+ EventManager.global = EventManager.create(); // create one top level emitter
1289
+ return EventManager;
1290
+
1291
+ });
1292
+
1293
+ /*global define:true */
1294
+
1295
+
1296
+
1297
+ define('react/base-task',['ensure-array', './status', './event-manager'],
1298
+ function (array, STATUS, EventManager) {
1299
+
1300
+ function BaseTask() {
1301
+ }
1302
+
1303
+ /**
1304
+ Getter Fn to retrieveAn array of the output param names for this task.
1305
+ */
1306
+ BaseTask.prototype.getOutParams = function () {
1307
+ return array(this.out); // ensure array
1308
+ };
1309
+
1310
+ BaseTask.prototype.isComplete = function () {
1311
+ return (this.status === STATUS.COMPLETE);
1312
+ };
1313
+
1314
+ BaseTask.prototype.start = function (args) { // mark task as started with args and note time
1315
+ /*jshint validthis: true */
1316
+ this.args = args;
1317
+ this.env.currentTask = this;
1318
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_START, this);
1319
+ };
1320
+
1321
+ BaseTask.prototype.complete = function (args) { //args that were used are available
1322
+ /*jshint validthis: true */
1323
+ this.status = STATUS.COMPLETE;
1324
+ this.results = args;
1325
+ this.env.currentTask = this;
1326
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_COMPLETE, this);
1327
+ };
1328
+
1329
+ BaseTask.prototype.functionExists = function (vCon) {
1330
+ var fn = this.f;
1331
+ if (!fn) return false;
1332
+ if (fn instanceof Function) return true;
1333
+ if (typeof(fn) === 'string') {
1334
+ var f = vCon.getVar(fn); // fn/method by string
1335
+ if (f && f instanceof Function) return true;
1336
+ }
1337
+ return false;
1338
+ };
1339
+
1340
+ BaseTask.prototype.areAllDepArgsDefined = function (vCon) {
1341
+ return this.a.every(function (k) { return (vCon.getVar(k) !== undefined); });
1342
+ };
1343
+
1344
+ BaseTask.prototype.depTasksAreDone = function (tasksByName) {
1345
+ return (!this.after || !this.after.length || // no dep tasks OR
1346
+ this.after.every(function (n) { return tasksByName[n].isComplete(); })); //all done
1347
+ };
1348
+
1349
+ function isObjProperty(str) { return (str.indexOf('.') !== -1); }
1350
+
1351
+ /**
1352
+ check that obj parent is def and not null so writing to obj.prop
1353
+ will not fail. ex: 'b.c' checks that b is def and not null.
1354
+ Also returns true if not obj.prop but simple var ex: 'b'.
1355
+ Tasks will implement outParentsExist() passing each out str
1356
+ to this if they want to do this check.
1357
+ */
1358
+ BaseTask.prototype.parentExists = function (objPropStr, vCon) {
1359
+ if (!isObjProperty(objPropStr)) return true; // NOT obj prop, just simple arg, ret true
1360
+ var nameAndProps = objPropStr.split('.');
1361
+ nameAndProps.pop(); // pop off final prop
1362
+ var parent = nameAndProps.reduce(function (accObj, prop) {
1363
+ if (accObj === undefined || accObj === null) return undefined; // prevent exception
1364
+ return accObj[prop];
1365
+ }, vCon.values); // vCon['foo']['bar']
1366
+ return (parent !== undefined && parent !== null);
1367
+ };
1368
+
1369
+ /**
1370
+ If params are obj property writes make sure the dst objects
1371
+ are defined and not null. cb: ['b.c'] -> b is def and not null.
1372
+ If null is specified then param is valid and will be ignored.
1373
+ @returns true if all obj prop parents are def and non null
1374
+ */
1375
+ BaseTask.prototype.outParentsExist = function (vCon) {
1376
+ var self = this;
1377
+ return this.getOutParams().every(function (x) {
1378
+ if (x === null) return true;
1379
+ return self.parentExists(x, vCon);
1380
+ });
1381
+ };
1382
+
1383
+ BaseTask.prototype.isReady = function (vCon, tasksByName) {
1384
+ return !this.status && // not started AND
1385
+ this.functionExists(vCon) && // function/method exists AND
1386
+ this.areAllDepArgsDefined(vCon) && // all dep vars defined AND
1387
+ this.depTasksAreDone(tasksByName) && // all dep tasks are done AND
1388
+ (!this.outParentsExist || // (task does not implement outParentsExist method OR
1389
+ this.outParentsExist(vCon)); // output parents exists (for obj property writes)
1390
+ };
1391
+
1392
+ BaseTask.prototype.isMethodCall = function () {
1393
+ /*jshint regexp: false */
1394
+ return (typeof(this.f) === 'string' && /^.*\..*$/.test(this.f)); //str contains .
1395
+ };
1396
+
1397
+ BaseTask.prototype.getMethodObj = function (vCon) { //obj.prop.prop2, returns obj.prop or undefined
1398
+ var name = this.f;
1399
+ if (!name) return undefined;
1400
+ var nameAndProps = name.split('.');
1401
+ nameAndProps.pop(); // pop off last one
1402
+ if (!nameAndProps.length) return undefined;
1403
+ var result = nameAndProps.reduce(function (accObj, prop) {
1404
+ if (accObj === undefined || accObj === null) return undefined; // prevent exception
1405
+ return accObj[prop];
1406
+ }, vCon.values); // vCon['foo']['bar']
1407
+ return result;
1408
+ };
1409
+
1410
+ return BaseTask;
1411
+
1412
+ });
1413
+
1414
+ /*global define:true */
1415
+
1416
+
1417
+
1418
+ define('react/cb-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1419
+
1420
+ function format_error(errmsg, obj) {
1421
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1422
+ }
1423
+
1424
+ var REQ = 'cbTask requires f, a, out';
1425
+ var FN_REQ = 'cbTask requires f to be a function or string';
1426
+ var A_REQ = 'cbTask requires a to be an array of string param names';
1427
+ var CB_REQ = 'cbTask requires out to be an array of string param names';
1428
+
1429
+ function CbTask(taskDef) {
1430
+ var self = this;
1431
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1432
+ }
1433
+
1434
+ CbTask.prototype = new BaseTask();
1435
+ CbTask.prototype.constructor = CbTask;
1436
+
1437
+ CbTask.validate = function (taskDef) {
1438
+ var errors = [];
1439
+ if (!taskDef.f || !taskDef.a || !taskDef.out) {
1440
+ errors.push(format_error(REQ, taskDef));
1441
+ } else {
1442
+ var ftype = typeof(taskDef.f);
1443
+ if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
1444
+ errors.push(format_error(FN_REQ, taskDef));
1445
+ }
1446
+ if (! (Array.isArray(taskDef.a) &&
1447
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1448
+ errors.push(format_error(A_REQ, taskDef));
1449
+ }
1450
+ if (! (Array.isArray(taskDef.out) &&
1451
+ taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
1452
+ errors.push(format_error(CB_REQ, taskDef));
1453
+ }
1454
+ }
1455
+ return errors;
1456
+ };
1457
+
1458
+ CbTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
1459
+ var self = this;
1460
+ this.cbFun = function (err, arg0, arg1, argn) {
1461
+ var args = Array.prototype.slice.call(arguments, 1);
1462
+ if (err) { handleTaskError(self, err); return; } //handle error and return, we are done
1463
+
1464
+ //no error, save callback args to vCon context, then continue execution
1465
+ vCon.saveResults(self.out, args);
1466
+ self.complete(args);
1467
+ contExec();
1468
+ };
1469
+ };
1470
+
1471
+ CbTask.prototype.exec = function exec(vCon, handleError, contExec) {
1472
+ try {
1473
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1474
+ //console.error('CbTask.exec.args=', args);
1475
+ //console.error('CbTask.exec.vCon=', vCon);
1476
+ this.start(args); //note the start time, args
1477
+ args.push(this.cbFun); // push callback fn on end
1478
+ var func = this.f;
1479
+ var bindObj = vCon.getVar('this'); //global space or the original this
1480
+ if (this.isMethodCall()) { //if method call then reset func and bindObj
1481
+ func = vCon.getVar(this.f);
1482
+ bindObj = this.getMethodObj(vCon);
1483
+ } else if (typeof(func) === 'string') {
1484
+ func = vCon.getVar(func); // we want the actual fn from this string
1485
+ }
1486
+ func.apply(bindObj, args);
1487
+ } catch (err) { //catch and handle the task error, calling final cb
1488
+ handleError(this, err);
1489
+ }
1490
+ };
1491
+
1492
+ return CbTask;
1493
+
1494
+ });
1495
+
1496
+
1497
+ /*global define:true */
1498
+
1499
+
1500
+
1501
+ define('react/promise-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1502
+
1503
+ /**
1504
+ PromiseTask is a task which executes a fn that returns a promise
1505
+ and when it completes it sets the values in vCon
1506
+ */
1507
+
1508
+ function format_error(errmsg, obj) {
1509
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1510
+ }
1511
+
1512
+ var REQ = 'promiseTask requires f, a, out';
1513
+ var FN_REQ = 'promiseTask requires f to be a function or string';
1514
+ var A_REQ = 'promiseTask requires a to be an array of string param names';
1515
+ var OUT_REQ = 'promiseTask requires out to be an array[1] of string param names';
1516
+
1517
+ function PromiseTask(taskDef) {
1518
+ var self = this;
1519
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1520
+ }
1521
+
1522
+ PromiseTask.prototype = new BaseTask();
1523
+ PromiseTask.prototype.constructor = PromiseTask;
1524
+
1525
+ PromiseTask.validate = function (taskDef) {
1526
+ var errors = [];
1527
+ if (!taskDef.f || !taskDef.a || !taskDef.out) {
1528
+ errors.push(format_error(REQ, taskDef));
1529
+ } else {
1530
+ var ftype = typeof(taskDef.f);
1531
+ if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
1532
+ errors.push(format_error(FN_REQ, taskDef));
1533
+ }
1534
+ if (! (Array.isArray(taskDef.a) &&
1535
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1536
+ errors.push(format_error(A_REQ, taskDef));
1537
+ }
1538
+ if (! (Array.isArray(taskDef.out) && taskDef.out.length <= 1 &&
1539
+ taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
1540
+ errors.push(format_error(OUT_REQ, taskDef));
1541
+ }
1542
+ }
1543
+ return errors;
1544
+ };
1545
+
1546
+ PromiseTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
1547
+ var self = this;
1548
+ this.nextFn = function (arg) {
1549
+ var args = Array.prototype.slice.call(arguments);
1550
+ vCon.saveResults(self.out, args);
1551
+ self.complete(args);
1552
+ contExec();
1553
+ };
1554
+ this.failFn = function (err) {
1555
+ handleTaskError(self, err);
1556
+ };
1557
+ };
1558
+
1559
+ PromiseTask.prototype.exec = function exec(vCon, handleError, contExec) {
1560
+ try {
1561
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1562
+ //console.error('PromiseTask.exec.args=', args);
1563
+ //console.error('PromiseTask.exec.vCon=', vCon);
1564
+ this.start(args); //note the start time, args
1565
+ var func = this.f;
1566
+ var bindObj = vCon.getVar('this'); //global space or the original this
1567
+ if (this.isMethodCall()) { //if method call then reset func and bindObj
1568
+ func = vCon.getVar(this.f);
1569
+ bindObj = this.getMethodObj(vCon);
1570
+ } else if (typeof(func) === 'string') {
1571
+ func = vCon.getVar(func); // we want the actual fn from this string
1572
+ }
1573
+ var retValue = func.apply(bindObj, args);
1574
+ if (retValue && typeof(retValue.then) === 'function') { // is a promise
1575
+ retValue.then(this.nextFn, this.failFn);
1576
+ } else { // just a value, proceed now
1577
+ this.nextFn(retValue);
1578
+ }
1579
+ } catch (err) { //catch and handle the task error, calling final cb
1580
+ handleError(this, err);
1581
+ }
1582
+ };
1583
+
1584
+ return PromiseTask;
1585
+
1586
+ });
1587
+
1588
+ /*global define:true */
1589
+
1590
+
1591
+
1592
+ define('react/ret-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1593
+
1594
+ function format_error(errmsg, obj) {
1595
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1596
+ }
1597
+
1598
+ var REQ = 'retTask requires f, a, out';
1599
+ var FN_REQ = 'retTask requires f to be a function or string';
1600
+ var A_REQ = 'retTask requires a to be an array of string param names';
1601
+ var RET_REQ = 'retTask requires out to be an array with single string param name or []';
1602
+
1603
+ function RetTask(taskDef) {
1604
+ var self = this;
1605
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1606
+ }
1607
+
1608
+ RetTask.prototype = new BaseTask();
1609
+ RetTask.prototype.constructor = RetTask;
1610
+
1611
+ RetTask.validate = function (taskDef) {
1612
+ var errors = [];
1613
+ if (!taskDef.f || !taskDef.a || !taskDef.out) {
1614
+ errors.push(format_error(REQ, taskDef));
1615
+ } else {
1616
+ var ftype = typeof(taskDef.f);
1617
+ if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
1618
+ errors.push(format_error(FN_REQ, taskDef));
1619
+ }
1620
+ if (! (Array.isArray(taskDef.a) &&
1621
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1622
+ errors.push(format_error(A_REQ, taskDef));
1623
+ }
1624
+
1625
+ if (! (Array.isArray(taskDef.out) &&
1626
+ (taskDef.out.length === 0 ||
1627
+ (taskDef.out.length === 1 && typeof(taskDef.out[0] === 'string'))))) {
1628
+ errors.push(format_error(RET_REQ, taskDef));
1629
+ }
1630
+ }
1631
+ return errors;
1632
+ };
1633
+
1634
+ RetTask.prototype.exec = function exec(vCon, handleError, contExec) {
1635
+ try {
1636
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1637
+ this.start(args); //note the start time, args
1638
+ var func = this.f;
1639
+ var bindObj = vCon.getVar('this'); //global space or the original this
1640
+ if (this.isMethodCall()) { //if method call then reset func and bindObj
1641
+ func = vCon.getVar(this.f);
1642
+ bindObj = this.getMethodObj(vCon);
1643
+ } else if (typeof(func) === 'string') {
1644
+ func = vCon.getVar(func); // we want the actual fn from this string
1645
+ }
1646
+ var results = [func.apply(bindObj, args)];
1647
+ vCon.saveResults(this.out, results); // save retval, takes arrays
1648
+ this.complete(results);
1649
+ contExec(); // continue since no callback to run this
1650
+ } catch (err) { handleError(this, err); } // catch and handle the task error, calling final cb
1651
+ };
1652
+
1653
+ return RetTask;
1654
+
1655
+ });
1656
+
1657
+ /*global define:true */
1658
+
1659
+
1660
+
1661
+ define('react/when-task',['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
1662
+
1663
+ /**
1664
+ When task which checks if is a promise (has a then method)
1665
+ and waits for it to resolve.
1666
+
1667
+ If argument does not have a then method, it resolves immediately
1668
+ */
1669
+
1670
+ function format_error(errmsg, obj) {
1671
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1672
+ }
1673
+
1674
+ var REQ = 'whenTask requires a, out';
1675
+ var A_REQ = 'whenTask requires a to be an array[1] of string param names';
1676
+ var OUT_REQ = 'whenTask requires out to be an array[1] of string param names';
1677
+
1678
+ function WhenTask(taskDef) {
1679
+ var self = this;
1680
+ Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
1681
+ }
1682
+
1683
+ WhenTask.prototype = new BaseTask();
1684
+ WhenTask.prototype.constructor = WhenTask;
1685
+
1686
+ WhenTask.prototype.f = function when() { // just here to keep validations happy
1687
+ }
1688
+
1689
+ WhenTask.validate = function (taskDef) {
1690
+ var errors = [];
1691
+ if (!taskDef.a || !taskDef.out) {
1692
+ errors.push(format_error(REQ, taskDef));
1693
+ } else {
1694
+ if (! (Array.isArray(taskDef.a) && taskDef.a.length === 1 &&
1695
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1696
+ errors.push(format_error(A_REQ, taskDef));
1697
+ }
1698
+ if (! (Array.isArray(taskDef.out) && taskDef.out.length <= 1 &&
1699
+ taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
1700
+ errors.push(format_error(OUT_REQ, taskDef));
1701
+ }
1702
+ }
1703
+ return errors;
1704
+ };
1705
+
1706
+ WhenTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
1707
+ var self = this;
1708
+ this.nextFn = function (arg) {
1709
+ var args = Array.prototype.slice.call(arguments);
1710
+ vCon.saveResults(self.out, args);
1711
+ self.complete(args);
1712
+ contExec();
1713
+ };
1714
+ this.failFn = function (err) {
1715
+ handleTaskError(self, err);
1716
+ };
1717
+ };
1718
+
1719
+ WhenTask.prototype.exec = function exec(vCon, handleError, contExec) {
1720
+ try {
1721
+ var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
1722
+ //console.error('WhenTask.exec.args=', args);
1723
+ //console.error('WhenTask.exec.vCon=', vCon);
1724
+ this.start(args); //note the start time, args
1725
+ var arg = args[0]; // one value allowed
1726
+ if (arg && typeof(arg.then) === 'function') { // is a promise
1727
+ arg.then(this.nextFn, this.failFn);
1728
+ } else { // not a promise continue immediately
1729
+ this.nextFn(arg);
1730
+ }
1731
+ } catch (err) { //catch and handle the task error, calling final cb
1732
+ handleError(this, err);
1733
+ }
1734
+ };
1735
+
1736
+ return WhenTask;
1737
+
1738
+ });
1739
+
1740
+ /*global define:true */
1741
+
1742
+
1743
+
1744
+ define('react/finalcb-task',['./sprintf', 'util', './status', './event-manager'],
1745
+ function (sprintf, util, STATUS, EventManager) {
1746
+
1747
+ var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
1748
+
1749
+ function FinalCbTask(outTaskOptions) {
1750
+ var taskDef = outTaskOptions.taskDef;
1751
+ if (typeof(outTaskOptions.cbFunc) !== 'function') throw new Error('callback is not a function');
1752
+ var self = this;
1753
+ for (var k in taskDef) {
1754
+ if (true) self[k] = taskDef[k]; // if to make jshint happy
1755
+ }
1756
+ this.f = outTaskOptions.cbFunc;
1757
+ this.tasks = outTaskOptions.tasks;
1758
+ this.vCon = outTaskOptions.vCon;
1759
+ this.retValue = outTaskOptions.retValue;
1760
+ this.execOptions = outTaskOptions.execOptions;
1761
+ this.env = outTaskOptions.env;
1762
+ }
1763
+
1764
+ function format_error(errmsg, obj) {
1765
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1766
+ }
1767
+
1768
+
1769
+ FinalCbTask.validate = function (taskDef) {
1770
+ var errors = [];
1771
+ if (! (Array.isArray(taskDef.a) &&
1772
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1773
+ errors.push(format_error(OUTTASK_A_REQ, taskDef));
1774
+ }
1775
+ return errors;
1776
+ };
1777
+
1778
+ FinalCbTask.prototype.isReady = function () {
1779
+ return (this.tasks.every(function (t) { return (t.status === STATUS.COMPLETE); }));
1780
+ };
1781
+
1782
+ FinalCbTask.prototype.exec = function (err) {
1783
+ if (!this.f) return; //must have already been called
1784
+ if (err) {
1785
+ this.env.error = err;
1786
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_ERRORED, this.env);
1787
+ this.f.call(null, err); //call the final callback with the first error hit
1788
+ } else { // no error, call with args
1789
+ var vCon = this.vCon;
1790
+ var finalArgs = this.a.map(function (k) { return vCon.getVar(k); });
1791
+ finalArgs.unshift(null); //unshift err=null to front
1792
+ this.env.results = finalArgs;
1793
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_COMPLETE, this.env);
1794
+ this.f.apply(null, finalArgs);
1795
+ }
1796
+ this.f = null; // prevent multiple calls
1797
+ };
1798
+
1799
+ return FinalCbTask;
1800
+
1801
+ });
1802
+
1803
+ /*global define:true */
1804
+
1805
+
1806
+
1807
+ define('react/vcon',[], function () {
1808
+
1809
+ var LAST_RESULTS_KEY = ':LAST_RESULTS';
1810
+
1811
+ function VContext() {
1812
+ }
1813
+
1814
+ VContext.prototype.getLastResults = function () { return this.getVar(LAST_RESULTS_KEY); };
1815
+ VContext.prototype.setLastResults = function (args) { this.setVar(LAST_RESULTS_KEY, args); };
1816
+
1817
+ VContext.prototype.getVar = function (name) { //name might be simple or obj.prop, also literals
1818
+ /*jshint regexp: false */
1819
+ var vConValues = this.values;
1820
+ if (typeof(name) !== 'string') return name; // literal boolean or number
1821
+ name = name.trim();
1822
+ // literal checks need to match what is in validate.js
1823
+ if (name === 'true') return true;
1824
+ if (name === 'false') return false;
1825
+ if (name === 'null') return null;
1826
+ if (/^-?[0-9]+$/.test(name)) return parseInt(name, 10); //int
1827
+ if (/^-?[0-9.]+$/.test(name)) return parseFloat(name); //float
1828
+ var m = /^("|')([^\1]*)\1$/.exec(name); //check for quoted string " or '
1829
+ if (m) return m[2]; // if is quoted str, return inside of the quotes
1830
+ var nameAndProps = name.split('.');
1831
+ return nameAndProps.reduce(function (accObj, prop) {
1832
+ if (accObj === undefined || accObj === null) return undefined; // prevent exception
1833
+ return accObj[prop];
1834
+ }, vConValues); // vCon['foo']['bar']
1835
+ };
1836
+
1837
+ /**
1838
+ Saves all the results from a task as a unit, also sets special
1839
+ variable :LAST_RESULTS which keeps an array of the last values
1840
+ which can be used for chaining and testing last results, etc.
1841
+ */
1842
+ VContext.prototype.saveResults = function (paramArr, valuesArr) { // set values for params
1843
+ var self = this;
1844
+ paramArr.forEach(function (k, idx) { //save values to v context
1845
+ self.setVar(k, (valuesArr[idx] !== undefined) ? valuesArr[idx] : null); //upgrade any undefined to null
1846
+ });
1847
+ this.setLastResults(valuesArr);
1848
+ };
1849
+
1850
+ VContext.prototype.setVar = function (name, value) { //name might be simple or obj.prop
1851
+ if (!name) return; // if name is undefined or null, then discard
1852
+ var vConValues = this.values;
1853
+ var nameAndProps = name.split('.');
1854
+ var lastProp = nameAndProps.pop();
1855
+ var obj = nameAndProps.reduce(function (accObj, prop) {
1856
+ var o = accObj[prop];
1857
+ if (o === undefined || o === null) { // if doesn't exist create it
1858
+ o = accObj[prop] = { };
1859
+ }
1860
+ return o;
1861
+ }, vConValues); // vCon['foo']['bar']
1862
+ obj[lastProp] = value;
1863
+ };
1864
+
1865
+
1866
+ /**
1867
+ Create Variable Context using arguments passed in.
1868
+ Ignore extra arguments passed in. Locals can be
1869
+ passed into seed the VContext otherwise empty {}
1870
+ will be used
1871
+ @param self used to pass 'this' context in
1872
+ */
1873
+ VContext.create = function (args, inParams, locals, self) {
1874
+ var initValues = {};
1875
+ if (self) initValues['this'] = self;
1876
+ if (locals) Object.keys(locals).forEach(function (k) { initValues[k] = locals[k]; }); // copy over keys
1877
+ var vContext = new VContext();
1878
+ vContext.values = args.reduce(function (vcon, x, idx) { // create vCon start with input args
1879
+ var param = inParams[idx];
1880
+ if (param) vcon[param] = (x !== undefined) ? x : null; // upgrade undefined to null
1881
+ return vcon;
1882
+ }, initValues);
1883
+ return vContext;
1884
+ };
1885
+
1886
+
1887
+ return VContext;
1888
+
1889
+ });
1890
+
1891
+ /*global define:true */
1892
+
1893
+
1894
+
1895
+ define('react/finalcb-first-task',['./sprintf', 'util', './status', './vcon', './event-manager'],
1896
+ function (sprintf, util, STATUS, VContext, EventManager) {
1897
+
1898
+ var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
1899
+
1900
+ function FinalCbFirstSuccTask(outTaskOptions) {
1901
+ var taskDef = outTaskOptions.taskDef;
1902
+ if (typeof(outTaskOptions.cbFunc) !== 'function') throw new Error('callback is not a function');
1903
+ var self = this;
1904
+ for (var k in taskDef) {
1905
+ if (true) self[k] = taskDef[k]; // if to make jshint happy
1906
+ }
1907
+ this.f = outTaskOptions.cbFunc;
1908
+ this.tasks = outTaskOptions.tasks;
1909
+ this.vCon = outTaskOptions.vCon;
1910
+ this.retValue = outTaskOptions.retValue;
1911
+ this.env = outTaskOptions.env;
1912
+ }
1913
+
1914
+ function format_error(errmsg, obj) {
1915
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1916
+ }
1917
+
1918
+ FinalCbFirstSuccTask.validate = function (taskDef) {
1919
+ var errors = [];
1920
+ if (! (Array.isArray(taskDef.a) &&
1921
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
1922
+ errors.push(format_error(OUTTASK_A_REQ, taskDef));
1923
+ }
1924
+ return errors;
1925
+ };
1926
+
1927
+ /**
1928
+ is ready to exit when any task comes back with non-null defined value
1929
+ */
1930
+ FinalCbFirstSuccTask.prototype.isReady = function () {
1931
+ var lastres = this.vCon.getLastResults();
1932
+ if (!lastres) return false; // no results yet
1933
+ return (lastres.some(function (v) { return (v !== undefined && v !== null); }));
1934
+ };
1935
+
1936
+ FinalCbFirstSuccTask.prototype.exec = function (err) {
1937
+ if (!this.f) return; //must have already been called
1938
+ if (err) {
1939
+ this.env.error = err;
1940
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_ERRORED, this.env);
1941
+ this.f.call(null, err); //call the final callback with the first error hit
1942
+ } else { // no error, call with args
1943
+ var vCon = this.vCon;
1944
+ var finalArgs = this.a.map(function (k) { return vCon.getVar(k); });
1945
+ finalArgs.unshift(null); //unshift err=null to front
1946
+ this.env.results = finalArgs;
1947
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_COMPLETE, this.env);
1948
+ this.f.apply(null, finalArgs);
1949
+ }
1950
+ this.f = null; // prevent multiple calls
1951
+ };
1952
+
1953
+ return FinalCbFirstSuccTask;
1954
+
1955
+ });
1956
+
1957
+ /*global define:true */
1958
+
1959
+
1960
+
1961
+ define('react/task',['util', './sprintf', 'ensure-array', './cb-task', './promise-task',
1962
+ './ret-task', './when-task', './finalcb-task', './finalcb-first-task',
1963
+ './status', './error', './vcon', './event-manager'],
1964
+ function (util, sprintf, array, CbTask, PromiseTask,
1965
+ RetTask, WhenTask, FinalCbTask, FinalCbFirstSuccTask,
1966
+ STATUS, error, VContext, EventManager) {
1967
+
1968
+ var TASK_TYPES = {
1969
+ cb: CbTask,
1970
+ ret: RetTask,
1971
+ promise: PromiseTask,
1972
+ when: WhenTask
1973
+ };
1974
+
1975
+ var DEFAULT_TASK_NAME = 'task_%s'; // for unnamed tasks use task_idx, like task_0
1976
+
1977
+ function taskTypeKeys() { return Object.keys(TASK_TYPES); }
1978
+
1979
+ var OUT_TASK_TYPES = {
1980
+ finalcb: FinalCbTask, //first task is the default if no type specified in taskDef
1981
+ finalcbFirst: FinalCbFirstSuccTask
1982
+ };
1983
+ function outTaskTypeKeys() { return Object.keys(OUT_TASK_TYPES); }
1984
+
1985
+ var LOCAL_FN_MISSING = 'function: %s not found in locals or input params - task[%s]';
1986
+ var TASKDEF_IS_OBJECT = 'task must be an object';
1987
+ var NO_TASKS_RUNNING_WONT_COMPLETE = 'no tasks running, flow will not complete, remaining tasks: %s';
1988
+ var TASK_TYPE_SHOULD_MATCH = 'task.type should match one of ' +
1989
+ Object.keys(TASK_TYPES).join(', ');
1990
+
1991
+ function format_error(errmsg, obj) {
1992
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
1993
+ }
1994
+
1995
+ /**
1996
+ guess the missing types from params.
1997
+ Augments in place but also returns taskDef.
1998
+ If not specified then is 'cb'
1999
+ */
2000
+ function setMissingType(taskDef) {
2001
+ if (taskDef.type) return taskDef; //already set, return
2002
+ taskDef.type = 'cb';
2003
+ return taskDef;
2004
+ }
2005
+
2006
+ function setMissingOutTaskType(taskDef) {
2007
+ if (!taskDef.type) taskDef.type = Object.keys(OUT_TASK_TYPES)[0]; //use first outTask type as default
2008
+ }
2009
+
2010
+ function ensureAfterArrStrings(taskDef) { // convert any fn to str, and make sure is array
2011
+ if (!taskDef.after) return;
2012
+ var afterArr = array(taskDef.after); // ensure is array, null becomes []
2013
+ afterArr = afterArr.map(function (a) { return (typeof(a) === 'function') ? a.name : a; });
2014
+ taskDef.after = afterArr;
2015
+ }
2016
+
2017
+ /**
2018
+ @returns array of errors for taskDef, could be empty
2019
+ */
2020
+ function validate(taskDef) {
2021
+ if (!taskDef || typeof(taskDef) !== 'object') {
2022
+ return [format_error(TASKDEF_IS_OBJECT, taskDef)];
2023
+ }
2024
+ setMissingType(taskDef);
2025
+ ensureAfterArrStrings(taskDef);
2026
+ var errors = [];
2027
+ errors = errors.concat(validateTaskType(taskDef));
2028
+ errors = errors.concat(validateTask(taskDef));
2029
+ return errors;
2030
+ }
2031
+
2032
+ function validateTaskType(taskDef) {
2033
+ var errors = [];
2034
+ if (!Object.keys(TASK_TYPES).some(
2035
+ function (type) { return (taskDef.type === type); })) {
2036
+ errors.push(format_error(TASK_TYPE_SHOULD_MATCH, taskDef));
2037
+ }
2038
+ return errors;
2039
+ }
2040
+
2041
+ function validateTask(taskDef) {
2042
+ var errors = [];
2043
+ var taskCons = TASK_TYPES[taskDef.type];
2044
+ if (taskCons) {
2045
+ errors = errors.concat(taskCons.validate(taskDef));
2046
+ }
2047
+ return errors;
2048
+ }
2049
+
2050
+ function validateOutTask(taskDef) {
2051
+ var errors = [];
2052
+ setMissingOutTaskType(taskDef);
2053
+ var taskCons = OUT_TASK_TYPES[taskDef.type];
2054
+ errors = errors.concat(taskCons.validate(taskDef));
2055
+ return errors;
2056
+ }
2057
+
2058
+
2059
+ function validateLocalFunctions(inParams, taskDefs, locals) {
2060
+ var errors = [];
2061
+ function foo() { } //used to mock args as fns for validation check
2062
+ var mock_args = inParams.map(function (p) { return foo; }); //mock args with fns
2063
+ var vCon = VContext.create(mock_args, inParams, locals);
2064
+ var tasks = taskDefs.map(create);
2065
+ var tasksWFunctions = tasks.filter(function (t) { return (t.type !== 'when'); }); // non-when tasks need f
2066
+ tasksWFunctions.forEach(function (t, idx) {
2067
+ if (!t.functionExists(vCon)) { // error if function doesnt exist AND
2068
+ if (!t.isMethodCall()) errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx)); // not method OR
2069
+ else {
2070
+ var obj = t.getMethodObj(vCon);
2071
+ if (obj && obj !== foo) { // (has parent but not our mock)
2072
+ errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx));
2073
+ }
2074
+ }
2075
+ }
2076
+ });
2077
+ return errors;
2078
+ }
2079
+
2080
+ function fName(fn) {
2081
+ if (typeof(fn) === 'function') {
2082
+ return fn.name;
2083
+ }
2084
+ return (fn) ? fn : '';
2085
+ }
2086
+
2087
+ /**
2088
+ Name tasks that are not already named. Prenamed task uniquness validation
2089
+ will be done in validate.
2090
+
2091
+ This modifies the tasks with the new names.
2092
+
2093
+ @returns map of names to tasks
2094
+ */
2095
+ function nameTasks(tasks) { //name tasks that are not already named, validation done elsewhere, ret map
2096
+ var namesMap = tasks.reduce(function (map, t) {
2097
+ if (t.name) { map[t.name] = t; }
2098
+ return map;
2099
+ }, {});
2100
+ tasks.forEach(function (t, idx) {
2101
+ if (!t.name) { //not already named
2102
+ var name = fName(t.f);
2103
+ if (!name) name = sprintf(DEFAULT_TASK_NAME, idx);
2104
+ if (!name || namesMap[name]) {
2105
+ name = sprintf('%s_%s', name, idx); //if empty or already used, postfix with _idx
2106
+ }
2107
+ t.name = name;
2108
+ namesMap[name] = t;
2109
+ }
2110
+ });
2111
+ return namesMap;
2112
+ }
2113
+
2114
+ function create(taskDef) {
2115
+ var TaskConstructor = TASK_TYPES[taskDef.type];
2116
+ return new TaskConstructor(taskDef);
2117
+ }
2118
+
2119
+ function createOutTask(taskDef, cbFunc, tasks, vCon, execOptions, env) {
2120
+ setMissingOutTaskType(taskDef);
2121
+ var outTaskOptions = {
2122
+ taskDef: taskDef,
2123
+ cbFunc: cbFunc,
2124
+ tasks: tasks,
2125
+ vCon: vCon,
2126
+ execOptions: execOptions,
2127
+ env: env,
2128
+ TaskConstructor: OUT_TASK_TYPES[taskDef.type]
2129
+ };
2130
+ EventManager.global.emit(EventManager.TYPES.EXEC_OUTTASK_CREATE, outTaskOptions); // hook
2131
+ var TaskConstructor = outTaskOptions.TaskConstructor; // hook could have changed
2132
+ return new TaskConstructor(outTaskOptions);
2133
+ }
2134
+
2135
+ function createErrorHandler(vCon, outTask) {
2136
+ return function handleError(task, err) {
2137
+ task.status = STATUS.ERRORED;
2138
+ task.error = err;
2139
+ outTask.env.currentTask = task;
2140
+ outTask.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_ERRORED, task);
2141
+ var errWithMeta = error.augmentError(err, {task: task, vcon: vCon});
2142
+ outTask.exec(errWithMeta); //call the final callback with the first error hit
2143
+ };
2144
+ }
2145
+
2146
+ function findTasksReady(vCon, tasks, tasksByName) {
2147
+ return tasks.filter(function (t) { return t.isReady(vCon, tasksByName); });
2148
+ }
2149
+
2150
+ function execTasks(tasksReady, vCon, handleError, contExec) {
2151
+ tasksReady.forEach(function (t) { t.status = STATUS.READY; }); //set ready first, no double exec
2152
+ tasksReady.forEach(function (t) { t.exec(vCon, handleError, contExec); });
2153
+ }
2154
+
2155
+ /**
2156
+ this will be called if there are no tasks found to run,
2157
+ and it will check if there are still tasks running or ready
2158
+ (which means they will be running shortly), in which
2159
+ case everything is fine. If no tasks are running then
2160
+ call handleError since this will never complete.
2161
+ */
2162
+ function checkIfTasksRunning(vCon, tasks, handleError, env) {
2163
+ var tasksRunning = tasks.filter(function (t) {
2164
+ return (t.status === STATUS.RUNNING || t.status === STATUS.READY);
2165
+ });
2166
+ if (!tasksRunning.length) {
2167
+ var remainingTasks = tasks.filter(function (t) { return (!t.status); });
2168
+ var remainingTNames = remainingTasks.map(function (t) { return t.name; });
2169
+ var errMsg = sprintf(NO_TASKS_RUNNING_WONT_COMPLETE, remainingTNames.join(', '));
2170
+ var emptyTask = { env: env };
2171
+ handleError(emptyTask, new Error(errMsg));
2172
+ }
2173
+ }
2174
+
2175
+ function findReadyAndExec(vCon, tasks, tasksByName, handleError, contExec, env) {
2176
+ var tasksReady = findTasksReady(vCon, tasks, tasksByName);
2177
+ if (!tasksReady.length) checkIfTasksRunning(vCon, tasks, handleError, env); // no tasks to run, check if ok
2178
+ execTasks(tasksReady, vCon, handleError, contExec);
2179
+ }
2180
+
2181
+ function serializeTasks(tasks) { // conveniently set after for each task idx > 0
2182
+ nameTasks(tasks);
2183
+ tasks.forEach(function (t, idx, arr) { if (idx !== 0) t.after = [arr[idx - 1].name]; });
2184
+ return tasks;
2185
+ }
2186
+
2187
+ return {
2188
+ serializeTasks: serializeTasks,
2189
+ TASK_TYPES: TASK_TYPES,
2190
+ taskTypeKeys: taskTypeKeys,
2191
+ OUT_TASK_TYPES: OUT_TASK_TYPES,
2192
+ outTaskTypeKeys: outTaskTypeKeys,
2193
+ setMissingType: setMissingType,
2194
+ validate: validate,
2195
+ validateOutTask: validateOutTask,
2196
+ validateLocalFunctions: validateLocalFunctions,
2197
+ nameTasks: nameTasks,
2198
+ create: create,
2199
+ createOutTask: createOutTask,
2200
+ createErrorHandler: createErrorHandler,
2201
+ findReadyAndExec: findReadyAndExec
2202
+ };
2203
+
2204
+ });
2205
+
2206
+ /*global define:true */
2207
+
2208
+
2209
+
2210
+ define('react/validate',['util', './sprintf', 'ensure-array', './task'], function (util, sprintf, array, taskUtil) {
2211
+ /*jshint latedef:false */
2212
+
2213
+ var AST_IS_OBJECT = 'ast must be an object with inParams, tasks, and outTask';
2214
+ var INPARAMS_ARR_STR = 'ast.inParams must be an array of strings';
2215
+ var TASKS_ARR = 'ast.tasks must be an array of tasks';
2216
+ var NAMES_UNIQUE = 'ast.tasks that specify name need to be unique, duplicate:';
2217
+ var LOCALS_NOTNULL = 'ast.locals should not be null';
2218
+ var DUP_OUTPUTS = 'multiple tasks output the same param, must be unique. param';
2219
+ var MISSING_INPUTS = 'missing or mispelled variable referenced in flow definition: %s';
2220
+
2221
+ // match any of our literals true, false, int, float, quoted strings, or is property (has dot), match vcon.js
2222
+ var LITERAL_OR_PROP_RE = /^(true|false|this|null|\-?[0-9\.]+)$|'|"|\./i;
2223
+
2224
+ function format_error(errmsg, obj) {
2225
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
2226
+ }
2227
+
2228
+ /**
2229
+ true if is a literal name
2230
+ */
2231
+ function isLiteralOrProp(name) { // need to match what is in vcon.js, TODO consolidate?
2232
+ return LITERAL_OR_PROP_RE.test(name);
2233
+ }
2234
+
2235
+ /**
2236
+ validate the AST return Errors
2237
+ @example
2238
+ var validate = require('./validate');
2239
+ var errors = validate(ast);
2240
+ @returns array of errors, could be empty
2241
+ */
2242
+ function validate(ast) {
2243
+ if (!ast || !ast.inParams || !ast.tasks || !ast.outTask) return [AST_IS_OBJECT];
2244
+ var errors = [];
2245
+ errors = errors.concat(validateInParams(ast.inParams));
2246
+ errors = errors.concat(validateTasks(ast.tasks));
2247
+ errors = errors.concat(validateTaskNamesUnique(ast.tasks));
2248
+ errors = errors.concat(taskUtil.validateOutTask(ast.outTask));
2249
+ errors = errors.concat(validateLocals(ast.locals));
2250
+ if (errors.length === 0) { // if no errors do additional validation
2251
+ if (ast.outTask.type !== 'finalcbFirst') errors = errors.concat(validateOuputsUnique(ast.tasks));
2252
+ errors = errors.concat(taskUtil.validateLocalFunctions(ast.inParams, ast.tasks, ast.locals));
2253
+ errors = errors.concat(validateNoMissingNames(ast));
2254
+ }
2255
+ return errors;
2256
+ }
2257
+
2258
+ /**
2259
+ @returns array of errors, could be empty
2260
+ */
2261
+ function validateInParams(inParams) {
2262
+ if (!Array.isArray(inParams) ||
2263
+ !inParams.every(function (x) { return (typeof(x) === 'string'); })) {
2264
+ return [INPARAMS_ARR_STR];
2265
+ }
2266
+ return [];
2267
+ }
2268
+
2269
+ /**
2270
+ @returns array of errors, could be empty
2271
+ */
2272
+ function validateTasks(tasks) {
2273
+ if (!Array.isArray(tasks)) return [TASKS_ARR];
2274
+ var errors = [];
2275
+ tasks.forEach(function (t) {
2276
+ errors = errors.concat(taskUtil.validate(t));
2277
+ });
2278
+ return errors;
2279
+ }
2280
+
2281
+ function validateTaskNamesUnique(tasks) {
2282
+ if (!Array.isArray(tasks)) return [];
2283
+ var errors = [];
2284
+ var namedTasks = tasks.filter(function (t) { return (t.name); });
2285
+ var names = namedTasks.map(function (t) { return t.name; });
2286
+ names.reduce(function (accum, name) {
2287
+ if (accum[name]) errors.push(sprintf('%s %s', NAMES_UNIQUE, name));
2288
+ else accum[name] = true;
2289
+ return accum;
2290
+ }, {});
2291
+ return errors;
2292
+ }
2293
+
2294
+ function validateLocals(locals) {
2295
+ var errors = [];
2296
+ if (locals === null) errors.push(LOCALS_NOTNULL);
2297
+ return errors;
2298
+ }
2299
+
2300
+ function getOutputParams(taskDef) {
2301
+ return array(taskDef.out); //ensure array
2302
+ }
2303
+
2304
+ function validateOuputsUnique(taskDefs) {
2305
+ var errors = [];
2306
+ taskDefs.reduce(function (accum, t) {
2307
+ getOutputParams(t).forEach(function (param) {
2308
+ if (accum[param] !== undefined) errors.push(sprintf('%s: %s', DUP_OUTPUTS, param));
2309
+ else accum[param] = true;
2310
+ });
2311
+ return accum;
2312
+ }, {});
2313
+ return errors;
2314
+ }
2315
+
2316
+
2317
+ /**
2318
+ validate there are no missing or mispelled param names in any task inputs
2319
+ or the final task output
2320
+
2321
+ @return array of errors, or empty array if none
2322
+ */
2323
+ function validateNoMissingNames(ast) {
2324
+ var errors = [];
2325
+ var names = {};
2326
+ if (ast.locals) {
2327
+ names = Object.keys(ast.locals).reduce(function (accum, k) { // start with locals
2328
+ accum[k] = true;
2329
+ return accum;
2330
+ }, names);
2331
+ }
2332
+ ast.inParams.reduce(function (accum, p) { // add input params
2333
+ accum[p] = true;
2334
+ return accum;
2335
+ }, names);
2336
+ ast.tasks.reduce(function (accum, t) { // add task outputs
2337
+ return t.out.reduce(function (innerAccum, p) {
2338
+ innerAccum[p] = true;
2339
+ return innerAccum;
2340
+ }, accum);
2341
+ }, names);
2342
+
2343
+ // now we have all possible provided vars, check task inputs are accounted for
2344
+ ast.tasks.reduce(function (accum, t) { // for all tasks
2345
+ return t.a.reduce(function (innerAccum, p) { // for all in params, except property
2346
+ if (!isLiteralOrProp(p) && !names[p]) innerAccum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
2347
+ return innerAccum;
2348
+ }, accum);
2349
+ }, errors);
2350
+
2351
+ // now check the final task outputs
2352
+ ast.outTask.a.reduce(function (accum, p) { // for final task out params
2353
+ if (!isLiteralOrProp(p) && !names[p]) accum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
2354
+ return accum;
2355
+ }, errors);
2356
+ return errors;
2357
+ }
2358
+
2359
+ return validate;
2360
+
2361
+ });
2362
+
2363
+ /*global define:true */
2364
+
2365
+
2366
+
2367
+ define('react/input-parser',['./event-manager'], function (EventManager) {
2368
+
2369
+ var defaultExecOptions = {
2370
+ reactExecOptions: true,
2371
+ outputStyle: 'cb',
2372
+ };
2373
+
2374
+ var OUTPUT_STYLES = {
2375
+ CALLBACK: 'cb',
2376
+ NONE: 'none'
2377
+ };
2378
+
2379
+ function isExecOptions(x) { return (x && x.reactExecOptions); }
2380
+ function execOptionsFilter(x) { return isExecOptions(x); }
2381
+ function nonExecOptionsFilter(x) { return !isExecOptions(x); }
2382
+ function mergeExecOptions(accum, options) {
2383
+ Object.keys(options).forEach(function (k) { accum[k] = options[k]; });
2384
+ return accum;
2385
+ }
2386
+
2387
+ function splitArgs(args, inParams, style) {
2388
+ var result = { };
2389
+ result.args = inParams.map(function (p) { return args.shift(); }); // take args for input params first
2390
+ if (style === OUTPUT_STYLES.CALLBACK && args.length) result.cb = args.shift(); // next take the cb
2391
+ result.extra = args; // these remaining were after the callback
2392
+ return result;
2393
+ }
2394
+
2395
+ function inputParser(inputArgs, ast) {
2396
+ var parsedInput = { };
2397
+ var execOptionsArr = inputArgs.filter(execOptionsFilter);
2398
+ execOptionsArr.unshift(defaultExecOptions);
2399
+ parsedInput.options = execOptionsArr.reduce(mergeExecOptions, {});
2400
+
2401
+ var args = inputArgs.filter(nonExecOptionsFilter);
2402
+ var splitResult = splitArgs(args, ast.inParams, parsedInput.options.outputStyle);
2403
+ parsedInput.args = splitResult.args;
2404
+ parsedInput.cb = splitResult.cb;
2405
+ if (splitResult.outputStyle) parsedInput.options.outputStyle = splitResult.outputStyle;
2406
+ if (splitResult.extra) parsedInput.extraArgs = splitResult.extra;
2407
+ EventManager.global.emit(EventManager.TYPES.EXEC_INPUT_PREPROCESS, parsedInput); // hook
2408
+ return parsedInput;
2409
+ }
2410
+
2411
+
2412
+ inputParser.defaultExecOptions = defaultExecOptions;
2413
+ return inputParser;
2414
+
2415
+ });
2416
+
2417
+ /*global define:true */
2418
+
2419
+
2420
+
2421
+ define('react/id',[], function () {
2422
+
2423
+ var startingId = 0;
2424
+
2425
+ function createUniqueId() {
2426
+ startingId += 1;
2427
+ if (startingId === Number.MAX_VALUE) startingId = 0; // if hits this start over //TODO need something better?
2428
+ return startingId;
2429
+ }
2430
+
2431
+ return {
2432
+ createUniqueId: createUniqueId
2433
+ };
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/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
+ });
3094
+ define('react', ['react/react'], function (main) { return main; });