react 0.6.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/README.md +15 -217
  2. package/ReactJSErrors.js +40 -0
  3. package/addons.js +4 -0
  4. package/lib/$.js +46 -0
  5. package/lib/CSSCore.js +114 -0
  6. package/lib/CSSProperty.js +90 -0
  7. package/lib/CSSPropertyOperations.js +97 -0
  8. package/lib/CallbackRegistry.js +91 -0
  9. package/lib/ChangeEventPlugin.js +365 -0
  10. package/lib/CompositionEventPlugin.js +212 -0
  11. package/lib/DOMChildrenOperations.js +135 -0
  12. package/lib/DOMProperty.js +266 -0
  13. package/lib/DOMPropertyOperations.js +168 -0
  14. package/lib/Danger.js +186 -0
  15. package/lib/DefaultDOMPropertyConfig.js +187 -0
  16. package/lib/DefaultEventPluginOrder.js +44 -0
  17. package/lib/EnterLeaveEventPlugin.js +112 -0
  18. package/lib/EventConstants.js +73 -0
  19. package/lib/EventListener.js +61 -0
  20. package/lib/EventPluginHub.js +190 -0
  21. package/lib/EventPluginRegistry.js +237 -0
  22. package/lib/EventPluginUtils.js +185 -0
  23. package/lib/EventPropagators.js +179 -0
  24. package/lib/ExecutionEnvironment.js +41 -0
  25. package/lib/LinkedStateMixin.js +46 -0
  26. package/lib/LinkedValueMixin.js +68 -0
  27. package/lib/MobileSafariClickEventPlugin.js +63 -0
  28. package/lib/PooledClass.js +113 -0
  29. package/lib/React.js +71 -0
  30. package/lib/ReactChildren.js +132 -0
  31. package/lib/ReactComponent.js +515 -0
  32. package/lib/ReactComponentBrowserEnvironment.js +140 -0
  33. package/lib/ReactComponentEnvironment.js +24 -0
  34. package/lib/ReactCompositeComponent.js +1020 -0
  35. package/lib/ReactCurrentOwner.js +39 -0
  36. package/lib/ReactDOM.js +194 -0
  37. package/lib/ReactDOMButton.js +64 -0
  38. package/lib/ReactDOMComponent.js +374 -0
  39. package/lib/ReactDOMForm.js +52 -0
  40. package/lib/ReactDOMIDOperations.js +173 -0
  41. package/lib/ReactDOMInput.js +169 -0
  42. package/lib/ReactDOMOption.js +50 -0
  43. package/lib/ReactDOMSelect.js +160 -0
  44. package/lib/ReactDOMSelection.js +189 -0
  45. package/lib/ReactDOMTextarea.js +136 -0
  46. package/lib/ReactDefaultBatchingStrategy.js +75 -0
  47. package/lib/ReactDefaultInjection.js +91 -0
  48. package/lib/ReactDefaultPerf.js +407 -0
  49. package/lib/ReactErrorUtils.js +46 -0
  50. package/lib/ReactEventEmitter.js +341 -0
  51. package/lib/ReactEventEmitterMixin.js +89 -0
  52. package/lib/ReactEventTopLevelCallback.js +89 -0
  53. package/lib/ReactInputSelection.js +140 -0
  54. package/lib/ReactInstanceHandles.js +322 -0
  55. package/lib/ReactLink.js +54 -0
  56. package/lib/ReactMarkupChecksum.js +53 -0
  57. package/lib/ReactMount.js +617 -0
  58. package/lib/ReactMountReady.js +95 -0
  59. package/lib/ReactMultiChild.js +441 -0
  60. package/lib/ReactMultiChildUpdateTypes.js +36 -0
  61. package/lib/ReactOwner.js +146 -0
  62. package/lib/ReactPerf.js +88 -0
  63. package/lib/ReactPropTransferer.js +128 -0
  64. package/lib/ReactPropTypes.js +158 -0
  65. package/lib/ReactReconcileTransaction.js +161 -0
  66. package/lib/ReactServerRendering.js +62 -0
  67. package/lib/ReactStateSetters.js +111 -0
  68. package/lib/ReactTextComponent.js +94 -0
  69. package/lib/ReactTransitionEvents.js +97 -0
  70. package/lib/ReactTransitionGroup.js +112 -0
  71. package/lib/ReactTransitionKeySet.js +111 -0
  72. package/lib/ReactTransitionableChild.js +152 -0
  73. package/lib/ReactUpdates.js +145 -0
  74. package/lib/ReactWithAddons.js +41 -0
  75. package/lib/SelectEventPlugin.js +217 -0
  76. package/lib/SimpleEventPlugin.js +365 -0
  77. package/lib/SyntheticClipboardEvent.js +45 -0
  78. package/lib/SyntheticCompositionEvent.js +51 -0
  79. package/lib/SyntheticEvent.js +163 -0
  80. package/lib/SyntheticFocusEvent.js +44 -0
  81. package/lib/SyntheticKeyboardEvent.js +56 -0
  82. package/lib/SyntheticMouseEvent.js +85 -0
  83. package/lib/SyntheticTouchEvent.js +50 -0
  84. package/lib/SyntheticUIEvent.js +45 -0
  85. package/lib/SyntheticWheelEvent.js +63 -0
  86. package/lib/Transaction.js +251 -0
  87. package/lib/ViewportMetrics.js +37 -0
  88. package/lib/accumulate.js +54 -0
  89. package/lib/adler32.js +39 -0
  90. package/lib/containsNode.js +49 -0
  91. package/lib/copyProperties.js +54 -0
  92. package/lib/createArrayFrom.js +94 -0
  93. package/lib/createNodesFromMarkup.js +93 -0
  94. package/lib/createObjectFrom.js +61 -0
  95. package/lib/cx.js +44 -0
  96. package/lib/dangerousStyleValue.js +57 -0
  97. package/lib/emptyFunction.js +43 -0
  98. package/lib/escapeTextForBrowser.js +47 -0
  99. package/lib/ex.js +49 -0
  100. package/lib/filterAttributes.js +45 -0
  101. package/lib/flattenChildren.js +54 -0
  102. package/lib/forEachAccumulated.js +36 -0
  103. package/lib/ge.js +76 -0
  104. package/lib/getActiveElement.js +33 -0
  105. package/lib/getEventTarget.js +36 -0
  106. package/lib/getMarkupWrap.js +108 -0
  107. package/lib/getNodeForCharacterOffset.js +80 -0
  108. package/lib/getReactRootElementInContainer.js +40 -0
  109. package/lib/getTextContentAccessor.js +40 -0
  110. package/lib/getUnboundedScrollPosition.js +45 -0
  111. package/lib/hyphenate.js +35 -0
  112. package/lib/invariant.js +54 -0
  113. package/lib/isEventSupported.js +74 -0
  114. package/lib/isNode.js +33 -0
  115. package/lib/isTextInputElement.js +49 -0
  116. package/lib/isTextNode.js +30 -0
  117. package/lib/joinClasses.js +44 -0
  118. package/lib/keyMirror.js +58 -0
  119. package/lib/keyOf.js +41 -0
  120. package/lib/memoizeStringOnly.js +39 -0
  121. package/lib/merge.js +37 -0
  122. package/lib/mergeHelpers.js +137 -0
  123. package/lib/mergeInto.js +45 -0
  124. package/lib/mixInto.js +34 -0
  125. package/lib/mutateHTMLNodeWithMarkup.js +100 -0
  126. package/lib/objMap.js +47 -0
  127. package/lib/objMapKeyVal.js +47 -0
  128. package/lib/performanceNow.js +42 -0
  129. package/lib/shallowEqual.js +49 -0
  130. package/lib/traverseAllChildren.js +127 -0
  131. package/package.json +33 -21
  132. package/react.js +4 -0
  133. package/.npmignore +0 -7
  134. package/.travis.yml +0 -5
  135. package/Jakefile.js +0 -39
  136. package/LICENSE +0 -19
  137. package/browser-test/dist.html +0 -89
  138. package/browser-test/index.html +0 -85
  139. package/browser-test/min.html +0 -89
  140. package/dist/react.js +0 -3094
  141. package/dist/react.min.js +0 -22
  142. package/doc/advanced.md +0 -166
  143. package/doc/color-def.graffle +0 -938
  144. package/doc/color-def.png +0 -0
  145. package/doc/simple.dot +0 -25
  146. package/doc/simple.png +0 -0
  147. package/examples/longer-example.js +0 -41
  148. package/examples/simple.js +0 -45
  149. package/examples/using-ast-directly.js +0 -30
  150. package/examples/using-events1.js +0 -79
  151. package/examples/using-log-events.js +0 -43
  152. package/lib/base-task.js +0 -123
  153. package/lib/cb-task.js +0 -84
  154. package/lib/core.js +0 -138
  155. package/lib/dsl.js +0 -138
  156. package/lib/error.js +0 -55
  157. package/lib/event-collector.js +0 -81
  158. package/lib/event-manager.js +0 -85
  159. package/lib/eventemitter.js +0 -20
  160. package/lib/finalcb-first-task.js +0 -68
  161. package/lib/finalcb-task.js +0 -65
  162. package/lib/id.js +0 -22
  163. package/lib/input-parser.js +0 -56
  164. package/lib/log-events.js +0 -92
  165. package/lib/parse.js +0 -41
  166. package/lib/promise-resolve.js +0 -50
  167. package/lib/promise-task.js +0 -93
  168. package/lib/react.js +0 -59
  169. package/lib/ret-task.js +0 -71
  170. package/lib/sprintf.js +0 -18
  171. package/lib/status.js +0 -14
  172. package/lib/task.js +0 -251
  173. package/lib/track-tasks.js +0 -74
  174. package/lib/validate.js +0 -159
  175. package/lib/vcon.js +0 -90
  176. package/lib/when-task.js +0 -85
  177. package/src/dist.build.requirejs +0 -20
  178. package/test/ast.mocha.js +0 -136
  179. package/test/cb-task.mocha.js +0 -220
  180. package/test/core-deferred.mocha.js +0 -143
  181. package/test/core-when.mocha.js +0 -96
  182. package/test/core.mocha.js +0 -589
  183. package/test/dsl.mocha.js +0 -350
  184. package/test/event-manager.mocha.js +0 -119
  185. package/test/exec-options.mocha.js +0 -48
  186. package/test/finalcb-task.mocha.js +0 -58
  187. package/test/input-parser.mocha.js +0 -86
  188. package/test/mocha.opts +0 -2
  189. package/test/module-use.mocha.js +0 -147
  190. package/test/promise-auto-resolve.mocha.js +0 -68
  191. package/test/ret-task.mocha.js +0 -220
  192. package/test/task.mocha.js +0 -42
  193. package/test/validate-cb-task.mocha.js +0 -100
  194. package/test/validate-ret-task.mocha.js +0 -110
  195. package/test/validate.mocha.js +0 -324
  196. package/test/vcon.mocha.js +0 -193
  197. package/vendor/chai/chai.js +0 -2038
  198. package/vendor/jquery/jquery-1.7.1.js +0 -9266
  199. package/vendor/jquery/jquery-1.7.1.min.js +0 -4
  200. package/vendor/mocha/mocha.css +0 -135
  201. package/vendor/mocha/mocha.js +0 -3589
  202. package/vendor/node/util.js +0 -531
  203. package/vendor/requirejs/require.js +0 -2053
  204. package/vendor/requirejs/require.min.js +0 -33
package/doc/color-def.png DELETED
Binary file
package/doc/simple.dot DELETED
@@ -1,25 +0,0 @@
1
- digraph loadRender {
2
- loadRender_input [ shape = box, style = filled, fillcolor = aquamarine ];
3
- fooPath [ style = filled, fillcolor = gainsboro ];
4
- barPath [ style = filled, fillcolor = gainsboro ];
5
- barP2 [ style = filled, fillcolor = gainsboro ];
6
- loadFoo [ shape = box, color = blue, fillcolor = beige, style = filled ];
7
- foo [ style = filled, fillcolor = gainsboro ];
8
- loadBar [ shape = box, color = blue, fillcolor = beige, style = filled ];
9
- bar [ style = filled, fillcolor = gainsboro ];
10
- render [ shape = box, color = blue, fillcolor = beige, style = filled ];
11
- renderedOut [ style = filled, fillcolor = gainsboro ];
12
- loadRender_output [ shape = box, style = filled, fillcolor = aquamarine ];
13
- loadRender_input -> fooPath;
14
- loadRender_input -> barPath;
15
- loadRender_input -> barP2;
16
- fooPath -> loadFoo;
17
- loadFoo -> foo;
18
- barPath -> loadBar;
19
- barP2 -> loadBar;
20
- loadBar -> bar;
21
- foo -> render;
22
- bar -> render;
23
- render -> renderedOut;
24
- renderedOut -> loadRender_output;
25
- }
package/doc/simple.png DELETED
Binary file
@@ -1,41 +0,0 @@
1
- 'use strict';
2
- /*jshint white: false */
3
-
4
- var react = require('../'); // require('react');
5
-
6
- function loadUser(uid, cb){ setTimeout(cb, 100, null, "User"+uid); }
7
- function loadFile(filename, cb){ setTimeout(cb, 100, null, 'Filedata'+filename); }
8
- function markdown(filedata) { return 'html'+filedata; }
9
- function prepareDirectory(outDirname, cb){ setTimeout(cb, 200, null, 'dircreated-'+outDirname); }
10
- function writeOutput(html, user, cb){ setTimeout(cb, 300, null, html+'_bytesWritten'); }
11
- function loadEmailTemplate(cb) { setTimeout(cb, 50, null, 'emailmd'); }
12
- function customizeEmail(user, emailHtml) { return 'cust-'+user+emailHtml; }
13
- function deliverEmail(custEmailHtml, cb) { setTimeout(cb, 100, null, 'delivered-'+custEmailHtml); }
14
-
15
- function useHtml(err, html, user, bytesWritten) {
16
- if (err) {
17
- console.log('***Error: %s', err);
18
- console.error(err.stack);
19
- return;
20
- }
21
- console.log('final result: %s, user: %s, written:%s', html, user, bytesWritten);
22
- }
23
-
24
- // define fn, glue together with react, it will parallelize
25
- // starts with name and in/out params, then the tasks
26
- var loadAndSave = react('loadAndSave', 'filename, uid, outDir, cb -> err, html, user, bytes',
27
- loadUser, 'uid, cb -> err, user', // calling async loadUser with uid, cb called with err and user
28
- loadFile, 'filename, cb -> err, filedata',
29
- markdown, 'filedata -> html', // using a sync function
30
- prepareDirectory, 'outDir, cb -> err, dircreated',
31
- writeOutput, 'html, user, cb -> err, bytes', { after: prepareDirectory }, // only after prepareDirectory done
32
- loadEmailTemplate, 'cb -> err, emailmd',
33
- markdown, 'emailmd -> emailHtml', // using a sync function
34
- customizeEmail, 'user, emailHtml -> custEmailHtml', // sync function
35
- deliverEmail, 'custEmailHtml, cb -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
36
- );
37
-
38
- //in another module you can use this as any other exported fn
39
- loadAndSave('file.md', 100, '/tmp/foo', useHtml); // executing the flow
40
-
41
-
@@ -1,45 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- Simple example showing flow definition of two async functions feeding a
5
- synchronous function.
6
-
7
- First two async functions inputs are satisfied by the flow inputs, so
8
- they will both run immediately in parallel.
9
-
10
- The last function waits for the outputs of the previous ones, then
11
- executes synchronously.
12
-
13
- Finally the flow calls the callback with the output values once all
14
- the tasks have completed.
15
- */
16
-
17
- var react = require('../'); // require('react');
18
-
19
- function loadFoo(fooPath, cb) {
20
- setTimeout(function () {
21
- cb(null, [fooPath, 'data'].join(':'));
22
- }, 10);
23
- }
24
-
25
- function loadBar(barPath, barP2, cb) {
26
- setTimeout(function () {
27
- cb(null, [barPath, barP2, 'data'].join(':'));
28
- }, 10);
29
- }
30
-
31
- function render(foo, bar) {
32
- return ['<html>', foo, '/', bar, '</html>'].join('');
33
- }
34
-
35
-
36
- var fn = react('loadRender', 'fooPath, barPath, barP2, cb -> err, renderedOut',
37
- loadFoo, 'fooPath, cb -> err, foo',
38
- loadBar, 'barPath, barP2, cb -> err, bar',
39
- render, 'foo, bar -> renderedOut'
40
- );
41
-
42
-
43
- fn('foo.txt', 'bar.txt', 'BBB', function (err, renderedOut) {
44
- console.error('results:', renderedOut);
45
- });
@@ -1,30 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- Advanced example using the AST directly which most users will not do.
5
- For general use, see other examples like simple.js
6
- */
7
- var react = require('../'); // require('react');
8
-
9
- function load(res, cb) { setTimeout(cb, 100, null, res + '-loaded'); }
10
- function prefix(prefstr, str, cb) { setTimeout(cb, 100, null, prefstr + str); }
11
- function postfix(str, poststr, cb) { setTimeout(cb, 100, null, str + poststr); }
12
- function upper(str) { return str.toUpperCase(); }
13
-
14
- var fn = react();
15
- var errors = fn.setAndValidateAST({
16
- inParams: ['res', 'prefstr', 'poststr'],
17
- tasks: [
18
- { f: load, a: ['res'], out: ['lres'] },
19
- { f: upper, a: ['lres'], out: ['ulres'], type: 'ret' },
20
- { f: prefix, a: ['prefstr', 'ulres'], out: ['plres'] },
21
- { f: postfix, a: ['plres', 'poststr'], out: ['plresp'] }
22
- ],
23
- outTask: { a: ['plresp'] }
24
- });
25
- console.error('errors:', errors); // []
26
-
27
- fn('foo', 'pre-', '-post', function cb(err, lres) {
28
- console.error('err:', err); // null
29
- console.error('lres:', lres); // pre-FOO-LOADED-post
30
- });
@@ -1,79 +0,0 @@
1
- 'use strict';
2
- /*jshint white: false */
3
-
4
- /**
5
- Default DSL, showing use of events
6
- */
7
-
8
- var react = require('../'); // require('react');
9
- react.trackTasks(); // turn on flow and task tracking events
10
-
11
- //output events as tasks start and complete
12
- react.events.on('flow.*', function (obj) {
13
- /*jshint validthis: true */
14
- var time = new Date();
15
- time.setTime(obj.time);
16
- var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
17
- var eventTimeStr = time.toISOString();
18
- if (this.event === 'flow.complete') {
19
- var env = obj;
20
- console.error('%s: %s \tmsecs:(%s) \n\targs:(%s) \n\tresults:(%s)\n',
21
- this.event, env.name, env.elapsedTime, argsNoCb, env.results);
22
- } else {
23
- var name = obj.name;
24
- var args = obj.args;
25
- console.error('%s: %s \n\targs:(%s)\n', this.event, name, argsNoCb);
26
- }
27
- });
28
-
29
- react.events.on('task.*', function (obj) {
30
- /*jshint validthis: true */
31
- var time = new Date();
32
- time.setTime(obj.time);
33
- var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
34
- var eventTimeStr = time.toISOString();
35
- if (this.event === 'task.complete') {
36
- var task = obj;
37
- console.error('%s: %s \tmsecs:(%s) \n\targs:(%s) \n\tresults:(%s)\n',
38
- this.event, task.name, task.elapsedTime, argsNoCb, task.results);
39
- } else {
40
- var name = obj.name;
41
- var args = obj.args;
42
- console.error('%s: %s \n\targs:(%s)\n', this.event, name, argsNoCb);
43
- }
44
- });
45
-
46
-
47
-
48
- function loadUser(uid, cb){ setTimeout(cb, 100, null, "User"+uid); }
49
- function loadFile(filename, cb){ setTimeout(cb, 100, null, 'Filedata'+filename); }
50
- function markdown(filedata) { return 'html'+filedata; }
51
- function prepareDirectory(outDirname, cb){ setTimeout(cb, 200, null, 'dircreated-'+outDirname); }
52
- function writeOutput(html, user, cb){ setTimeout(cb, 300, null, html+'_bytesWritten'); }
53
- function loadEmailTemplate(cb) { setTimeout(cb, 50, null, 'emailmd'); }
54
- function customizeEmail(user, emailHtml) { return 'cust-'+user+emailHtml; }
55
- function deliverEmail(custEmailHtml, cb) { setTimeout(cb, 100, null, 'delivered-'+custEmailHtml); }
56
-
57
- function useHtml(err, html, user, bytesWritten) {
58
- if (err) {
59
- console.log('***Error: %s', err);
60
- return;
61
- }
62
- console.log('final result: %s, user: %s, written:%s', html, user, bytesWritten);
63
- }
64
-
65
- var loadAndSave = react('loadAndSave', 'filename, uid, outDirname, cb -> err, html, user, bytesWritten', // name, in/out params
66
- loadUser, 'uid, cb -> err, user', // calling async fn loadUser with uid, callback is called with err and user
67
- loadFile, 'filename, cb -> err, filedata',
68
- markdown, 'filedata -> html', // using a sync function
69
- prepareDirectory, 'outDirname, cb -> err, dircreated',
70
- writeOutput, 'html, user, cb -> err, bytesWritten', { after: prepareDirectory }, // only after prepareDirectory done
71
- loadEmailTemplate, 'cb -> err, emailmd',
72
- markdown, 'emailmd -> emailHtml', // using a sync function
73
- customizeEmail, 'user, emailHtml -> custEmailHtml', // sync fn
74
- deliverEmail, 'custEmailHtml, cb -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
75
- );
76
-
77
- loadAndSave('file.md', 100, '/tmp/foo', useHtml); // executing the flow
78
-
79
-
@@ -1,43 +0,0 @@
1
- 'use strict';
2
- /*jshint white: false */
3
-
4
- /**
5
- Default DSL, showing use of events
6
- */
7
-
8
- var react = require('../'); // require('react');
9
- react.logEvents(); // turn on logging of all flow and task events for all react functions
10
-
11
-
12
- function loadUser(uid, cb){ setTimeout(cb, 100, null, "User"+uid); }
13
- function loadFile(filename, cb){ setTimeout(cb, 100, null, 'Filedata'+filename); }
14
- function markdown(filedata) { return 'html'+filedata; }
15
- function prepareDirectory(outDirname, cb){ setTimeout(cb, 200, null, 'dircreated-'+outDirname); }
16
- function writeOutput(html, user, cb){ setTimeout(cb, 300, null, html+'_bytesWritten'); }
17
- function loadEmailTemplate(cb) { setTimeout(cb, 50, null, 'emailmd'); }
18
- function customizeEmail(user, emailHtml) { return 'cust-'+user+emailHtml; }
19
- function deliverEmail(custEmailHtml, cb) { setTimeout(cb, 100, null, 'delivered-'+custEmailHtml); }
20
-
21
- function useHtml(err, html, user, bytesWritten) {
22
- if (err) {
23
- console.log('***Error: %s', err);
24
- return;
25
- }
26
- console.log('final result: %s, user: %s, written:%s', html, user, bytesWritten);
27
- }
28
-
29
- var loadAndSave = react('loadAndSave', 'filename, uid, outDirname, cb -> err, html, user, bytesWritten', // name, in/out params
30
- loadUser, 'uid, cb -> err, user', // calling async fn loadUser with uid, callback is called with err and user
31
- loadFile, 'filename, cb -> err, filedata',
32
- markdown, 'filedata -> html', // using a sync function
33
- prepareDirectory, 'outDirname, cb -> err, dircreated',
34
- writeOutput, 'html, user, cb -> err, bytesWritten', { after: prepareDirectory }, // only after prepareDirectory done
35
- loadEmailTemplate, 'cb -> err, emailmd',
36
- markdown, 'emailmd -> emailHtml', // using a sync function
37
- customizeEmail, 'user, emailHtml -> custEmailHtml', // sync fn
38
- deliverEmail, 'custEmailHtml, cb -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
39
- );
40
-
41
- loadAndSave('file.md', 100, '/tmp/foo', useHtml); // executing the flow
42
-
43
-
package/lib/base-task.js DELETED
@@ -1,123 +0,0 @@
1
- 'use strict';
2
- /*global define:true */
3
-
4
- if (typeof define !== 'function') {
5
- var define = require('amdefine')(module);
6
- }
7
-
8
- define(['ensure-array', './status', './event-manager'],
9
- function (array, STATUS, EventManager) {
10
-
11
- function BaseTask() {
12
- }
13
-
14
- /**
15
- Getter Fn to retrieveAn array of the output param names for this task.
16
- */
17
- BaseTask.prototype.getOutParams = function () {
18
- return array(this.out); // ensure array
19
- };
20
-
21
- BaseTask.prototype.isComplete = function () {
22
- return (this.status === STATUS.COMPLETE);
23
- };
24
-
25
- BaseTask.prototype.start = function (args) { // mark task as started with args and note time
26
- /*jshint validthis: true */
27
- this.args = args;
28
- this.env.currentTask = this;
29
- this.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_START, this);
30
- };
31
-
32
- BaseTask.prototype.complete = function (args) { //args that were used are available
33
- /*jshint validthis: true */
34
- this.status = STATUS.COMPLETE;
35
- this.results = args;
36
- this.env.currentTask = this;
37
- this.env.flowEmitter.emit(EventManager.TYPES.EXEC_TASK_COMPLETE, this);
38
- };
39
-
40
- BaseTask.prototype.functionExists = function (vCon) {
41
- var fn = this.f;
42
- if (!fn) return false;
43
- if (fn instanceof Function) return true;
44
- if (typeof(fn) === 'string') {
45
- var f = vCon.getVar(fn); // fn/method by string
46
- if (f && f instanceof Function) return true;
47
- }
48
- return false;
49
- };
50
-
51
- BaseTask.prototype.areAllDepArgsDefined = function (vCon) {
52
- return this.a.every(function (k) { return (vCon.getVar(k) !== undefined); });
53
- };
54
-
55
- BaseTask.prototype.depTasksAreDone = function (tasksByName) {
56
- return (!this.after || !this.after.length || // no dep tasks OR
57
- this.after.every(function (n) { return tasksByName[n].isComplete(); })); //all done
58
- };
59
-
60
- function isObjProperty(str) { return (str.indexOf('.') !== -1); }
61
-
62
- /**
63
- check that obj parent is def and not null so writing to obj.prop
64
- will not fail. ex: 'b.c' checks that b is def and not null.
65
- Also returns true if not obj.prop but simple var ex: 'b'.
66
- Tasks will implement outParentsExist() passing each out str
67
- to this if they want to do this check.
68
- */
69
- BaseTask.prototype.parentExists = function (objPropStr, vCon) {
70
- if (!isObjProperty(objPropStr)) return true; // NOT obj prop, just simple arg, ret true
71
- var nameAndProps = objPropStr.split('.');
72
- nameAndProps.pop(); // pop off final prop
73
- var parent = nameAndProps.reduce(function (accObj, prop) {
74
- if (accObj === undefined || accObj === null) return undefined; // prevent exception
75
- return accObj[prop];
76
- }, vCon.values); // vCon['foo']['bar']
77
- return (parent !== undefined && parent !== null);
78
- };
79
-
80
- /**
81
- If params are obj property writes make sure the dst objects
82
- are defined and not null. cb: ['b.c'] -> b is def and not null.
83
- If null is specified then param is valid and will be ignored.
84
- @returns true if all obj prop parents are def and non null
85
- */
86
- BaseTask.prototype.outParentsExist = function (vCon) {
87
- var self = this;
88
- return this.getOutParams().every(function (x) {
89
- if (x === null) return true;
90
- return self.parentExists(x, vCon);
91
- });
92
- };
93
-
94
- BaseTask.prototype.isReady = function (vCon, tasksByName) {
95
- return !this.status && // not started AND
96
- this.functionExists(vCon) && // function/method exists AND
97
- this.areAllDepArgsDefined(vCon) && // all dep vars defined AND
98
- this.depTasksAreDone(tasksByName) && // all dep tasks are done AND
99
- (!this.outParentsExist || // (task does not implement outParentsExist method OR
100
- this.outParentsExist(vCon)); // output parents exists (for obj property writes)
101
- };
102
-
103
- BaseTask.prototype.isMethodCall = function () {
104
- /*jshint regexp: false */
105
- return (typeof(this.f) === 'string' && /^.*\..*$/.test(this.f)); //str contains .
106
- };
107
-
108
- BaseTask.prototype.getMethodObj = function (vCon) { //obj.prop.prop2, returns obj.prop or undefined
109
- var name = this.f;
110
- if (!name) return undefined;
111
- var nameAndProps = name.split('.');
112
- nameAndProps.pop(); // pop off last one
113
- if (!nameAndProps.length) return undefined;
114
- var result = nameAndProps.reduce(function (accObj, prop) {
115
- if (accObj === undefined || accObj === null) return undefined; // prevent exception
116
- return accObj[prop];
117
- }, vCon.values); // vCon['foo']['bar']
118
- return result;
119
- };
120
-
121
- return BaseTask;
122
-
123
- });
package/lib/cb-task.js DELETED
@@ -1,84 +0,0 @@
1
- 'use strict';
2
- /*global define:true */
3
-
4
- if (typeof define !== 'function') {
5
- var define = require('amdefine')(module);
6
- }
7
-
8
- define(['util', './sprintf', './base-task'], function (util, sprintf, BaseTask) {
9
-
10
- function format_error(errmsg, obj) {
11
- return sprintf('%s - %s', errmsg, util.inspect(obj));
12
- }
13
-
14
- var REQ = 'cbTask requires f, a, out';
15
- var FN_REQ = 'cbTask requires f to be a function or string';
16
- var A_REQ = 'cbTask requires a to be an array of string param names';
17
- var CB_REQ = 'cbTask requires out to be an array of string param names';
18
-
19
- function CbTask(taskDef) {
20
- var self = this;
21
- Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
22
- }
23
-
24
- CbTask.prototype = new BaseTask();
25
- CbTask.prototype.constructor = CbTask;
26
-
27
- CbTask.validate = function (taskDef) {
28
- var errors = [];
29
- if (!taskDef.f || !taskDef.a || !taskDef.out) {
30
- errors.push(format_error(REQ, taskDef));
31
- } else {
32
- var ftype = typeof(taskDef.f);
33
- if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
34
- errors.push(format_error(FN_REQ, taskDef));
35
- }
36
- if (! (Array.isArray(taskDef.a) &&
37
- taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
38
- errors.push(format_error(A_REQ, taskDef));
39
- }
40
- if (! (Array.isArray(taskDef.out) &&
41
- taskDef.out.every(function (x) { return (typeof(x) === 'string'); }))) {
42
- errors.push(format_error(CB_REQ, taskDef));
43
- }
44
- }
45
- return errors;
46
- };
47
-
48
- CbTask.prototype.prepare = function prepare(handleTaskError, vCon, contExec) {
49
- var self = this;
50
- this.cbFun = function (err, arg0, arg1, argn) {
51
- var args = Array.prototype.slice.call(arguments, 1);
52
- if (err) { handleTaskError(self, err); return; } //handle error and return, we are done
53
-
54
- //no error, save callback args to vCon context, then continue execution
55
- vCon.saveResults(self.out, args);
56
- self.complete(args);
57
- contExec();
58
- };
59
- };
60
-
61
- CbTask.prototype.exec = function exec(vCon, handleError, contExec) {
62
- try {
63
- var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
64
- //console.error('CbTask.exec.args=', args);
65
- //console.error('CbTask.exec.vCon=', vCon);
66
- this.start(args); //note the start time, args
67
- args.push(this.cbFun); // push callback fn on end
68
- var func = this.f;
69
- var bindObj = vCon.getVar('this'); //global space or the original this
70
- if (this.isMethodCall()) { //if method call then reset func and bindObj
71
- func = vCon.getVar(this.f);
72
- bindObj = this.getMethodObj(vCon);
73
- } else if (typeof(func) === 'string') {
74
- func = vCon.getVar(func); // we want the actual fn from this string
75
- }
76
- func.apply(bindObj, args);
77
- } catch (err) { //catch and handle the task error, calling final cb
78
- handleError(this, err);
79
- }
80
- };
81
-
82
- return CbTask;
83
-
84
- });