react 0.2.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +8 -7
  2. package/{lib → dsl}/chain.js +5 -3
  3. package/{lib → dsl}/fstr.js +7 -5
  4. package/{lib → dsl}/pcode.js +8 -6
  5. package/examples/chain-events1.js +3 -3
  6. package/examples/chain1.js +2 -2
  7. package/examples/default-events1.js +2 -2
  8. package/examples/fstr-events1.js +4 -3
  9. package/examples/fstr1.js +3 -2
  10. package/examples/pcode1.js +2 -2
  11. package/lib/base-task.js +1 -0
  12. package/lib/cb-task.js +14 -1
  13. package/lib/core.js +33 -9
  14. package/lib/dsl.js +14 -6
  15. package/lib/event-manager.js +16 -5
  16. package/lib/finalcb-first-task.js +9 -6
  17. package/lib/finalcb-task.js +9 -6
  18. package/lib/input-parser.js +7 -3
  19. package/lib/parse.js +6 -3
  20. package/lib/promise-task.js +89 -0
  21. package/lib/ret-task.js +1 -1
  22. package/lib/task.js +23 -19
  23. package/lib/validate.js +3 -3
  24. package/lib/vcon.js +6 -3
  25. package/lib/when-task.js +81 -0
  26. package/package.json +4 -2
  27. package/promise-resolve.js +35 -0
  28. package/react.js +0 -4
  29. package/test/core-deferred.test.js +134 -0
  30. package/test/core-promised.test.js +132 -0
  31. package/test/core-when.test.js +84 -0
  32. package/test/core.test.js +63 -4
  33. package/test/dsl.test.js +58 -6
  34. package/test/{chain.test.js → dsl/chain.test.js} +71 -1
  35. package/test/{fstr.test.js → dsl/fstr.test.js} +1 -1
  36. package/test/{pcode.test.js → dsl/pcode.test.js} +122 -1
  37. package/test/exec-options.test.js +2 -1
  38. package/test/finalcb-task.test.js +6 -5
  39. package/test/input-parser.test.js +10 -6
  40. package/test/module-use.test.js +2 -190
  41. package/test/promise-auto-resolve.test.js +51 -0
  42. package/test/validate.test.js +4 -2
  43. package/test/vcon.test.js +13 -0
  44. package/oldExamples/analyze.js +0 -29
  45. package/oldExamples/analyze2.js +0 -29
  46. package/oldExamples/example10-dsl.js +0 -63
  47. package/oldExamples/example11.js +0 -62
  48. package/oldExamples/example12.js +0 -63
  49. package/oldExamples/example13.js +0 -63
  50. package/oldExamples/example14.js +0 -63
  51. package/oldExamples/example15.js +0 -75
  52. package/oldExamples/example6-ast.js +0 -47
  53. package/oldExamples/example6-dsl.js +0 -49
  54. package/oldExamples/example8-ast.js +0 -55
  55. package/oldExamples/example8-dsl.js +0 -53
  56. package/oldExamples/example9-ast.js +0 -58
  57. package/oldExamples/example9-dsl.js +0 -57
  58. package/oldExamples/function-str-ex1.js +0 -33
  59. package/oldExamples/function-str-ex2.js +0 -67
  60. package/oldExamples/trait1.js +0 -41
  61. package/oldExamples/trait2.js +0 -44
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ Auto resolve promises passed in as arguments to the flow
5
+
6
+ - Detects promises by checking for .then()
7
+ - Create promise name for param (param__promise)
8
+ - moves existing vCon promise into the param__promise
9
+ - creates WhenTask which resolves param__promise into param
10
+ */
11
+
12
+
13
+ var react = require('./'); // require('react');
14
+
15
+ var PROMISE_SUFFIX = '__promise'; // added to param names that are promises
16
+
17
+ react.events.on(react.events.TYPES.EXEC_TASKS_PRECREATE, function (taskEnv) {
18
+ var vConValues = taskEnv.vCon.values;
19
+ var promiseParams = taskEnv.ast.inParams.filter(function (p) {
20
+ var value = vConValues[p];
21
+ return (value && typeof(value.then) === 'function');
22
+ });
23
+ promiseParams.forEach(function (p) {
24
+ var promiseName = p + PROMISE_SUFFIX;
25
+ vConValues[promiseName] = vConValues[p];
26
+ vConValues[p] = undefined;
27
+ taskEnv.taskDefs.push({
28
+ type: 'when',
29
+ a: [promiseName],
30
+ out: [p]
31
+ });
32
+ });
33
+ });
34
+
35
+ module.exports = react; // return react
package/react.js CHANGED
@@ -6,7 +6,3 @@ module.exports = require('./lib/dsl.js'); // core + default dsl
6
6
  module.exports.options = core.options; // global react options
7
7
  module.exports.events = core.events; // global react event emitter
8
8
 
9
- // additional interfaces
10
- module.exports.fstrDefine = require('./lib/fstr.js');
11
- module.exports.pcodeDefine = require('./lib/pcode.js');
12
- module.exports.chainDefine = require('./lib/chain.js');
@@ -0,0 +1,134 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ Test core PromiseTasks using Deferred - jquery style promises
5
+ */
6
+
7
+
8
+ var test = require('tap').test;
9
+ var Deferred = require('Deferred');
10
+ //var when = Deferred.when;
11
+
12
+
13
+ var react = require('../'); //require('react');
14
+
15
+ function multiply(x, y) {
16
+ var deferred = new Deferred();
17
+ setTimeout(function () {
18
+ deferred.resolve(x * y);
19
+ }, 10);
20
+ return deferred.promise();
21
+ }
22
+ function add(x, y) {
23
+ var deferred = new Deferred();
24
+ setTimeout(function () {
25
+ deferred.resolve(x + y);
26
+ }, 10);
27
+ return deferred.promise();
28
+ }
29
+
30
+ function badFunc(a, b) {
31
+ throw new Error('badFuncThrow');
32
+ }
33
+
34
+ function badF2(a, b) {
35
+ var deferred = new Deferred();
36
+ setTimeout(function () {
37
+ deferred.reject(new Error('my-error'));
38
+ }, 10);
39
+ return deferred.promise();
40
+ }
41
+
42
+
43
+ test('multi-step', function (t) {
44
+ t.plan(4);
45
+ var fn = react();
46
+ var errors = fn.setAndValidateAST({
47
+ inParams: ['a', 'b'],
48
+ tasks: [
49
+ { f: multiply, a: ['a', 'b'], out: ['c'], type: 'promise' },
50
+ { f: add, a: ['c', 'b'], out: ['d'], type: 'promise' }
51
+ ],
52
+ outTask: { a: ['c', 'd'] }
53
+ });
54
+ t.deepEqual(errors, [], 'no validation errors');
55
+
56
+ fn(2, 3, function (err, c, d) {
57
+ t.equal(err, null);
58
+ t.equal(c, 6);
59
+ t.equal(d, 9);
60
+ t.end();
61
+ });
62
+ });
63
+
64
+ test('using "this" in a cb function', function (t) {
65
+ t.plan(3);
66
+ function getA(cb) {
67
+ /*jshint validthis: true */
68
+ var deferred = new Deferred();
69
+ var self = this;
70
+ setTimeout(function () {
71
+ deferred.resolve(self.a);
72
+ }, 10);
73
+ return deferred.promise();
74
+ }
75
+
76
+ var fn = react();
77
+ var errors = fn.setAndValidateAST({
78
+ inParams: [],
79
+ tasks: [
80
+ { f: getA, a: [], out: ['a'], type: 'promise' }
81
+ ],
82
+ outTask: { a: ['a'] }
83
+ });
84
+ t.deepEqual(errors, [], 'no validation errors');
85
+
86
+ var obj = {
87
+ a: 100
88
+ };
89
+
90
+ fn.apply(obj, [function (err, a) {
91
+ t.equal(err, null);
92
+ t.equal(a, 100);
93
+ t.end();
94
+ }]);
95
+ });
96
+
97
+
98
+ test('throws error', function (t) {
99
+ t.plan(2);
100
+ var fn = react();
101
+ var errors = fn.setAndValidateAST({
102
+ inParams: ['a', 'b'],
103
+ tasks: [
104
+ { f: badFunc, a: ['a', 'b'], out: ['c'], type: 'promise' },
105
+ { f: add, a: ['c', 'b'], out: ['d'], type: 'promise' }
106
+ ],
107
+ outTask: { a: ['c', 'd'] }
108
+ });
109
+ t.deepEqual(errors, [], 'no validation errors');
110
+
111
+ fn(2, 3, function (err, c, d) {
112
+ t.equal(err.message, 'badFuncThrow');
113
+ t.end();
114
+ });
115
+ });
116
+
117
+ test('rejects with error', function (t) {
118
+ t.plan(2);
119
+ var fn = react();
120
+ var errors = fn.setAndValidateAST({
121
+ inParams: ['a', 'b'],
122
+ tasks: [
123
+ { f: badF2, a: ['a', 'b'], out: ['c'], type: 'promise' },
124
+ { f: add, a: ['c', 'b'], out: ['d'], type: 'promise' }
125
+ ],
126
+ outTask: { a: ['c', 'd'] }
127
+ });
128
+ t.deepEqual(errors, [], 'no validation errors');
129
+
130
+ fn(2, 3, function (err, c, d) {
131
+ t.equal(err.message, 'my-error');
132
+ t.end();
133
+ });
134
+ });
@@ -0,0 +1,132 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ Test core PromiseTasks using promised-io
5
+ */
6
+
7
+ var test = require('tap').test;
8
+ //var when = require('promised-io/promise');
9
+ var Deferred = require('promised-io/promise').Deferred;
10
+
11
+ var react = require('../'); //require('react');
12
+
13
+
14
+ function multiply(x, y) {
15
+ var deferred = new Deferred();
16
+ setTimeout(function () {
17
+ deferred.resolve(x * y);
18
+ }, 10);
19
+ return deferred.promise;
20
+ }
21
+ function add(x, y) {
22
+ var deferred = new Deferred();
23
+ setTimeout(function () {
24
+ deferred.resolve(x + y);
25
+ }, 10);
26
+ return deferred.promise;
27
+ }
28
+
29
+ function badFunc(a, b) {
30
+ throw new Error('badFuncThrow');
31
+ }
32
+
33
+ function badF2(a, b) {
34
+ var deferred = new Deferred();
35
+ setTimeout(function () {
36
+ deferred.reject(new Error('my-error'));
37
+ }, 10);
38
+ return deferred.promise;
39
+ }
40
+
41
+
42
+ test('multi-step', function (t) {
43
+ t.plan(4);
44
+ var fn = react();
45
+ var errors = fn.setAndValidateAST({
46
+ inParams: ['a', 'b'],
47
+ tasks: [
48
+ { f: multiply, a: ['a', 'b'], out: ['c'], type: 'promise' },
49
+ { f: add, a: ['c', 'b'], out: ['d'], type: 'promise' }
50
+ ],
51
+ outTask: { a: ['c', 'd'] }
52
+ });
53
+ t.deepEqual(errors, [], 'no validation errors');
54
+
55
+ fn(2, 3, function (err, c, d) {
56
+ t.equal(err, null);
57
+ t.equal(c, 6);
58
+ t.equal(d, 9);
59
+ t.end();
60
+ });
61
+ });
62
+
63
+ test('using "this" in a cb function', function (t) {
64
+ t.plan(3);
65
+ function getA(cb) {
66
+ /*jshint validthis: true */
67
+ var deferred = new Deferred();
68
+ var self = this;
69
+ setTimeout(function () {
70
+ deferred.resolve(self.a);
71
+ }, 10);
72
+ return deferred.promise;
73
+ }
74
+
75
+ var fn = react();
76
+ var errors = fn.setAndValidateAST({
77
+ inParams: [],
78
+ tasks: [
79
+ { f: getA, a: [], out: ['a'], type: 'promise' }
80
+ ],
81
+ outTask: { a: ['a'] }
82
+ });
83
+ t.deepEqual(errors, [], 'no validation errors');
84
+
85
+ var obj = {
86
+ a: 100
87
+ };
88
+
89
+ fn.apply(obj, [function (err, a) {
90
+ t.equal(err, null);
91
+ t.equal(a, 100);
92
+ t.end();
93
+ }]);
94
+ });
95
+
96
+ test('throws error', function (t) {
97
+ t.plan(2);
98
+ var fn = react();
99
+ var errors = fn.setAndValidateAST({
100
+ inParams: ['a', 'b'],
101
+ tasks: [
102
+ { f: badFunc, a: ['a', 'b'], out: ['c'], type: 'promise' },
103
+ { f: add, a: ['c', 'b'], out: ['d'], type: 'promise' }
104
+ ],
105
+ outTask: { a: ['c', 'd'] }
106
+ });
107
+ t.deepEqual(errors, [], 'no validation errors');
108
+
109
+ fn(2, 3, function (err, c, d) {
110
+ t.equal(err.message, 'badFuncThrow');
111
+ t.end();
112
+ });
113
+ });
114
+
115
+ test('rejects with error', function (t) {
116
+ t.plan(2);
117
+ var fn = react();
118
+ var errors = fn.setAndValidateAST({
119
+ inParams: ['a', 'b'],
120
+ tasks: [
121
+ { f: badF2, a: ['a', 'b'], out: ['c'], type: 'promise' },
122
+ { f: add, a: ['c', 'b'], out: ['d'], type: 'promise' }
123
+ ],
124
+ outTask: { a: ['c', 'd'] }
125
+ });
126
+ t.deepEqual(errors, [], 'no validation errors');
127
+
128
+ fn(2, 3, function (err, c, d) {
129
+ t.equal(err.message, 'my-error');
130
+ t.end();
131
+ });
132
+ });
@@ -0,0 +1,84 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ Test core WhenTasks using promised-io
5
+ */
6
+
7
+ var test = require('tap').test;
8
+ //var when = require('promised-io/promise');
9
+ var Deferred = require('promised-io/promise').Deferred;
10
+
11
+ var react = require('../'); //require('react');
12
+
13
+
14
+ function multiply(x, y) {
15
+ var deferred = new Deferred();
16
+ setTimeout(function () {
17
+ deferred.resolve(x * y);
18
+ }, 10);
19
+ return deferred.promise;
20
+ }
21
+ function add(x, y) {
22
+ var deferred = new Deferred();
23
+ setTimeout(function () {
24
+ deferred.resolve(x + y);
25
+ }, 10);
26
+ return deferred.promise;
27
+ }
28
+
29
+ function badF2(a, b) {
30
+ var deferred = new Deferred();
31
+ setTimeout(function () {
32
+ deferred.reject(new Error('my-error'));
33
+ }, 10);
34
+ return deferred.promise;
35
+ }
36
+
37
+
38
+ test('multi-step', function (t) {
39
+ t.plan(4);
40
+ var fn = react();
41
+ var errors = fn.setAndValidateAST({
42
+ inParams: ['pm', 'pa'],
43
+ tasks: [
44
+ { a: ['pm'], out: ['m'], type: 'when' },
45
+ { a: ['pa'], out: ['a'], type: 'when' }
46
+ ],
47
+ outTask: { a: ['m', 'a'] }
48
+ });
49
+ t.deepEqual(errors, [], 'no validation errors');
50
+
51
+ var pm = multiply(2, 3);
52
+ var pa = add(4, 5);
53
+
54
+ fn(pm, pa, function (err, m, a) {
55
+ t.equal(err, null);
56
+ t.equal(m, 6);
57
+ t.equal(a, 9);
58
+ t.end();
59
+ });
60
+ });
61
+
62
+
63
+ test('rejects with error', function (t) {
64
+ t.plan(2);
65
+ var fn = react();
66
+ var errors = fn.setAndValidateAST({
67
+ inParams: ['pm', 'pa'],
68
+ tasks: [
69
+ { a: ['pm'], out: ['m'], type: 'when' },
70
+ { a: ['pa'], out: ['a'], type: 'when' }
71
+ ],
72
+ outTask: { a: ['m', 'a'] }
73
+ });
74
+ t.deepEqual(errors, [], 'no validation errors');
75
+
76
+ var pm = badF2(2, 3);
77
+ var pa = add(4, 5);
78
+
79
+
80
+ fn(pm, pa, function (err, m, a) {
81
+ t.equal(err.message, 'my-error');
82
+ t.end();
83
+ });
84
+ });
package/test/core.test.js CHANGED
@@ -377,7 +377,66 @@ test('multi-step func cb err, cb with error', function (t) {
377
377
  t.end();
378
378
  });
379
379
  });
380
+
381
+ test('using "this" in a cb function', function (t) {
382
+ t.plan(3);
383
+ function getA(cb) {
384
+ /*jshint validthis: true */
385
+ cb(null, this.a);
386
+ }
387
+
388
+ var fn = react();
389
+ var errors = fn.setAndValidateAST({
390
+ inParams: [],
391
+ tasks: [
392
+ { f: getA, a: [], out: ['a'] }
393
+ ],
394
+ outTask: { a: ['a'] }
395
+ });
396
+ t.deepEqual(errors, [], 'no validation errors');
397
+
398
+ var obj = {
399
+ a: 100
400
+ };
401
+
402
+ fn.apply(obj, [function (err, a) {
403
+ t.equal(err, null);
404
+ t.equal(a, 100);
405
+ t.end();
406
+ }]);
407
+ });
408
+
409
+ test('using "this" in a sync function', function (t) {
410
+ t.plan(3);
411
+ function getA(cb) {
412
+ /*jshint validthis: true */
413
+ return this.a;
414
+ }
380
415
 
416
+ var fn = react();
417
+ var errors = fn.setAndValidateAST({
418
+ inParams: [],
419
+ tasks: [
420
+ { f: getA, a: [], out: ['a'], type: 'ret' }
421
+ ],
422
+ outTask: { a: ['a'] }
423
+ });
424
+ t.deepEqual(errors, [], 'no validation errors');
425
+
426
+ var obj = {
427
+ a: 100
428
+ };
429
+
430
+ fn.apply(obj, [function (err, a) {
431
+ t.equal(err, null);
432
+ t.equal(a, 100);
433
+ t.end();
434
+ }]);
435
+ });
436
+
437
+
438
+ // Select first tests
439
+
381
440
 
382
441
  test('selectFirst with first succeeding', function (t) {
383
442
  t.plan(6);
@@ -398,7 +457,7 @@ test('selectFirst with first succeeding', function (t) {
398
457
  }
399
458
  fn.events.on('task.complete', accumEvents);
400
459
 
401
- fn(2, 3, function (err, c, d) {
460
+ fn(2, 3, function (err, c) {
402
461
  t.equal(err, null);
403
462
  t.equal(c, 6);
404
463
  t.equal(events.length, 1, 'should have seen one task compl events');
@@ -431,7 +490,7 @@ test('selectFirst with third succeeding', function (t) {
431
490
  }
432
491
  fn.events.on('task.complete', accumEvents);
433
492
 
434
- fn(2, 3, function (err, c, d) {
493
+ fn(2, 3, function (err, c) {
435
494
  t.equal(err, null);
436
495
  t.equal(c, 5);
437
496
  t.equal(events.length, 3, 'should have seen three task compl events');
@@ -468,7 +527,7 @@ test('selectFirst forces order with third succeeding', function (t) {
468
527
  }
469
528
  fn.events.on('task.complete', accumEvents);
470
529
 
471
- fn(2, 3, function (err, c, d) {
530
+ fn(2, 3, function (err, c) {
472
531
  t.equal(err, null);
473
532
  t.equal(c, 5);
474
533
  t.equal(events.length, 3, 'should have seen three task compl events');
@@ -507,7 +566,7 @@ test('selectFirst using direct returns', function (t) {
507
566
  }
508
567
  fn.events.on('task.complete', accumEvents);
509
568
 
510
- fn(2, 3, function (err, c, d) {
569
+ fn(2, 3, function (err, c) {
511
570
  t.equal(err, null);
512
571
  t.equal(c, 5);
513
572
  t.equal(events.length, 3, 'should have seen three task compl events');