react 0.2.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/.npmignore +2 -1
  2. package/README.md +94 -149
  3. package/doc/advanced.md +161 -0
  4. package/doc/color-def.graffle +938 -0
  5. package/doc/color-def.png +0 -0
  6. package/doc/default-simple.dot +19 -0
  7. package/doc/default-simple.dot.png +0 -0
  8. package/examples/default-events1.js +33 -6
  9. package/examples/{fstr1.js → default-log-events.js} +20 -13
  10. package/examples/default-simple.js +45 -0
  11. package/lib/base-task.js +8 -6
  12. package/lib/cb-task.js +14 -1
  13. package/lib/core.js +43 -12
  14. package/lib/dsl.js +14 -6
  15. package/lib/event-collector.js +65 -0
  16. package/lib/event-manager.js +29 -16
  17. package/lib/finalcb-first-task.js +16 -10
  18. package/lib/finalcb-task.js +17 -10
  19. package/lib/input-parser.js +7 -3
  20. package/lib/log-events.js +86 -0
  21. package/lib/parse.js +6 -3
  22. package/lib/promise-resolve.js +35 -0
  23. package/lib/promise-task.js +89 -0
  24. package/lib/ret-task.js +1 -1
  25. package/lib/task.js +32 -23
  26. package/lib/track-tasks.js +60 -0
  27. package/lib/validate.js +3 -3
  28. package/lib/vcon.js +6 -3
  29. package/lib/when-task.js +81 -0
  30. package/package.json +5 -3
  31. package/react.js +33 -5
  32. package/test/core-deferred.test.js +134 -0
  33. package/test/core-promised.test.js +132 -0
  34. package/test/core-when.test.js +84 -0
  35. package/test/core.test.js +108 -60
  36. package/test/dsl.test.js +58 -6
  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 +16 -199
  41. package/test/promise-auto-resolve.test.js +52 -0
  42. package/test/validate.test.js +4 -2
  43. package/test/vcon.test.js +13 -0
  44. package/Jakefile.js +0 -8
  45. package/examples/chain-events1.js +0 -34
  46. package/examples/chain1.js +0 -19
  47. package/examples/fstr-events1.js +0 -51
  48. package/examples/pcode1.js +0 -22
  49. package/jake-tasks/jake-test.js +0 -64
  50. package/lib/chain.js +0 -148
  51. package/lib/fstr.js +0 -119
  52. package/lib/pcode.js +0 -173
  53. package/oldExamples/analyze.js +0 -29
  54. package/oldExamples/analyze2.js +0 -29
  55. package/oldExamples/example10-dsl.js +0 -63
  56. package/oldExamples/example11.js +0 -62
  57. package/oldExamples/example12.js +0 -63
  58. package/oldExamples/example13.js +0 -63
  59. package/oldExamples/example14.js +0 -63
  60. package/oldExamples/example15.js +0 -75
  61. package/oldExamples/example6-ast.js +0 -47
  62. package/oldExamples/example6-dsl.js +0 -49
  63. package/oldExamples/example8-ast.js +0 -55
  64. package/oldExamples/example8-dsl.js +0 -53
  65. package/oldExamples/example9-ast.js +0 -58
  66. package/oldExamples/example9-dsl.js +0 -57
  67. package/oldExamples/function-str-ex1.js +0 -33
  68. package/oldExamples/function-str-ex2.js +0 -67
  69. package/oldExamples/trait1.js +0 -41
  70. package/oldExamples/trait2.js +0 -44
  71. package/test/chain.test.js +0 -253
  72. package/test/fstr.test.js +0 -300
  73. package/test/pcode.test.js +0 -335
@@ -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
@@ -3,6 +3,7 @@
3
3
  var test = require('tap').test;
4
4
 
5
5
  var react = require('../react');
6
+ var EventCollector = require('../lib/event-collector'); // require('react/lib/event-collector'); // turn on tracking and get EventCollector
6
7
 
7
8
  function multiply(x, y, cb) { cb(null, x * y); }
8
9
  function add(x, y, cb) { cb(null, x + y); }
@@ -107,19 +108,17 @@ test('multi-step with after as nonarr fn', function (t) {
107
108
  });
108
109
  t.deepEqual(errors, [], 'no validation errors');
109
110
 
110
- var events = [];
111
- function accumEvents(task) {
112
- events.push(task);
113
- }
114
- fn.events.on('task.complete', accumEvents);
111
+ var collector = new EventCollector();
112
+ collector.capture(fn, 'task.complete');
115
113
 
116
114
  fn(2, 3, function (err, c, d) {
117
115
  t.equal(err, null);
118
116
  t.equal(c, 6);
119
117
  t.equal(d, 5);
118
+ var events = collector.list();
120
119
  t.equal(events.length, 2, 'should have seen one task compl events');
121
- t.equal(events[0].name, 'add', 'name matches');
122
- t.equal(events[1].name, 'multiply', 'name matches');
120
+ t.equal(events[0].task.name, 'add', 'name matches');
121
+ t.equal(events[1].task.name, 'multiply', 'name matches');
123
122
  t.end();
124
123
  });
125
124
  });
@@ -137,31 +136,29 @@ test('mixed multi-step with after as nonarr fn w/events', function (t) {
137
136
  });
138
137
  t.deepEqual(errors, [], 'no validation errors');
139
138
 
140
- var events = [];
141
- function accumEvents(task) {
142
- events.push(task);
143
- }
144
- fn.events.on('task.complete', accumEvents);
139
+ var collector = new EventCollector();
140
+ collector.capture(fn, 'task.complete');
145
141
 
146
142
  fn(2, 3, function (err, c, d) {
147
143
  t.equal(err, null);
148
144
  t.equal(c, 6);
149
145
  t.equal(d, 5);
146
+ var events = collector.list();
150
147
  t.equal(events.length, 2, 'should have seen one task compl events');
151
- t.equal(events[0].name, 'fnRetsSum', 'name matches');
152
- t.ok(events[0].id, 'has unique id');
153
- t.ok(events[0].startTime, 'has startTime');
154
- t.ok(events[0].endTime, 'has endTime');
155
- t.ok(events[0].elapsedTime !== undefined, 'has elapsedTime');
156
- t.ok(events[0].args, 'has args');
157
- t.ok(events[0].results, 'has results');
158
- t.equal(events[1].name, 'multiply', 'name matches');
159
- t.ok(events[1].id, 'has unique id');
160
- t.ok(events[1].startTime, 'has startTime');
161
- t.ok(events[1].endTime, 'has endTime');
162
- t.ok(events[1].elapsedTime !== undefined, 'has elapsedTime');
163
- t.ok(events[1].args, 'has args');
164
- t.ok(events[1].results, 'has results');
148
+ t.equal(events[0].task.name, 'fnRetsSum', 'name matches');
149
+ t.ok(events[0].task.id, 'has unique id');
150
+ t.ok(events[0].task.startTime, 'has startTime');
151
+ t.ok(events[0].task.endTime, 'has endTime');
152
+ t.ok(events[0].task.elapsedTime !== undefined, 'has elapsedTime');
153
+ t.ok(events[0].task.args, 'has args');
154
+ t.ok(events[0].task.results, 'has results');
155
+ t.equal(events[1].task.name, 'multiply', 'name matches');
156
+ t.ok(events[1].task.id, 'has unique id');
157
+ t.ok(events[1].task.startTime, 'has startTime');
158
+ t.ok(events[1].task.endTime, 'has endTime');
159
+ t.ok(events[1].task.elapsedTime !== undefined, 'has elapsedTime');
160
+ t.ok(events[1].task.args, 'has args');
161
+ t.ok(events[1].task.results, 'has results');
165
162
  t.end();
166
163
  });
167
164
  });
@@ -377,7 +374,66 @@ test('multi-step func cb err, cb with error', function (t) {
377
374
  t.end();
378
375
  });
379
376
  });
377
+
378
+ test('using "this" in a cb function', function (t) {
379
+ t.plan(3);
380
+ function getA(cb) {
381
+ /*jshint validthis: true */
382
+ cb(null, this.a);
383
+ }
380
384
 
385
+ var fn = react();
386
+ var errors = fn.setAndValidateAST({
387
+ inParams: [],
388
+ tasks: [
389
+ { f: getA, a: [], out: ['a'] }
390
+ ],
391
+ outTask: { a: ['a'] }
392
+ });
393
+ t.deepEqual(errors, [], 'no validation errors');
394
+
395
+ var obj = {
396
+ a: 100
397
+ };
398
+
399
+ fn.apply(obj, [function (err, a) {
400
+ t.equal(err, null);
401
+ t.equal(a, 100);
402
+ t.end();
403
+ }]);
404
+ });
405
+
406
+ test('using "this" in a sync function', function (t) {
407
+ t.plan(3);
408
+ function getA(cb) {
409
+ /*jshint validthis: true */
410
+ return this.a;
411
+ }
412
+
413
+ var fn = react();
414
+ var errors = fn.setAndValidateAST({
415
+ inParams: [],
416
+ tasks: [
417
+ { f: getA, a: [], out: ['a'], type: 'ret' }
418
+ ],
419
+ outTask: { a: ['a'] }
420
+ });
421
+ t.deepEqual(errors, [], 'no validation errors');
422
+
423
+ var obj = {
424
+ a: 100
425
+ };
426
+
427
+ fn.apply(obj, [function (err, a) {
428
+ t.equal(err, null);
429
+ t.equal(a, 100);
430
+ t.end();
431
+ }]);
432
+ });
433
+
434
+
435
+ // Select first tests
436
+
381
437
 
382
438
  test('selectFirst with first succeeding', function (t) {
383
439
  t.plan(6);
@@ -392,18 +448,16 @@ test('selectFirst with first succeeding', function (t) {
392
448
  });
393
449
  t.deepEqual(errors, [], 'no validation errors');
394
450
 
395
- var events = [];
396
- function accumEvents(task) {
397
- events.push(task);
398
- }
399
- fn.events.on('task.complete', accumEvents);
451
+ var collector = new EventCollector();
452
+ collector.capture(fn, 'task.complete');
400
453
 
401
- fn(2, 3, function (err, c, d) {
454
+ fn(2, 3, function (err, c) {
402
455
  t.equal(err, null);
403
456
  t.equal(c, 6);
457
+ var events = collector.list();
404
458
  t.equal(events.length, 1, 'should have seen one task compl events');
405
- t.equal(events[0].name, 'multiply', 'name matches');
406
- t.deepEqual(events[0].results, [6], 'results match');
459
+ t.equal(events[0].task.name, 'multiply', 'name matches');
460
+ t.deepEqual(events[0].task.results, [6], 'results match');
407
461
  t.end();
408
462
  });
409
463
  });
@@ -425,18 +479,16 @@ test('selectFirst with third succeeding', function (t) {
425
479
  });
426
480
  t.deepEqual(errors, [], 'no validation errors');
427
481
 
428
- var events = [];
429
- function accumEvents(task) {
430
- events.push(task);
431
- }
432
- fn.events.on('task.complete', accumEvents);
482
+ var collector = new EventCollector();
483
+ collector.capture(fn, 'task.complete');
433
484
 
434
- fn(2, 3, function (err, c, d) {
485
+ fn(2, 3, function (err, c) {
435
486
  t.equal(err, null);
436
487
  t.equal(c, 5);
488
+ var events = collector.list();
437
489
  t.equal(events.length, 3, 'should have seen three task compl events');
438
- t.equal(events[2].name, 'add', 'name matches');
439
- t.deepEqual(events[2].results, [5], 'results match');
490
+ t.equal(events[2].task.name, 'add', 'name matches');
491
+ t.deepEqual(events[2].task.results, [5], 'results match');
440
492
  t.end();
441
493
  });
442
494
  });
@@ -462,20 +514,18 @@ test('selectFirst forces order with third succeeding', function (t) {
462
514
  });
463
515
  t.deepEqual(errors, [], 'no validation errors');
464
516
 
465
- var events = [];
466
- function accumEvents(task) {
467
- events.push(task);
468
- }
469
- fn.events.on('task.complete', accumEvents);
517
+ var collector = new EventCollector();
518
+ collector.capture(fn, 'task.complete');
470
519
 
471
- fn(2, 3, function (err, c, d) {
520
+ fn(2, 3, function (err, c) {
472
521
  t.equal(err, null);
473
522
  t.equal(c, 5);
523
+ var events = collector.list();
474
524
  t.equal(events.length, 3, 'should have seen three task compl events');
475
- t.equal(events[0].name, 'noSuccess', 'name matches');
476
- t.equal(events[1].name, 'noSuccessNull', 'name matches');
477
- t.equal(events[2].name, 'add', 'name matches');
478
- t.deepEqual(events[2].results, [5], 'results match');
525
+ t.equal(events[0].task.name, 'noSuccess', 'name matches');
526
+ t.equal(events[1].task.name, 'noSuccessNull', 'name matches');
527
+ t.equal(events[2].task.name, 'add', 'name matches');
528
+ t.deepEqual(events[2].task.results, [5], 'results match');
479
529
  t.end();
480
530
  });
481
531
  });
@@ -501,18 +551,16 @@ test('selectFirst using direct returns', function (t) {
501
551
  });
502
552
  t.deepEqual(errors, [], 'no validation errors');
503
553
 
504
- var events = [];
505
- function accumEvents(task) {
506
- events.push(task);
507
- }
508
- fn.events.on('task.complete', accumEvents);
554
+ var collector = new EventCollector();
555
+ collector.capture(fn, 'task.complete');
509
556
 
510
- fn(2, 3, function (err, c, d) {
557
+ fn(2, 3, function (err, c) {
511
558
  t.equal(err, null);
512
559
  t.equal(c, 5);
560
+ var events = collector.list();
513
561
  t.equal(events.length, 3, 'should have seen three task compl events');
514
- t.equal(events[2].name, 'addRet', 'name matches');
515
- t.deepEqual(events[2].results, [5], 'results match');
562
+ t.equal(events[2].task.name, 'addRet', 'name matches');
563
+ t.deepEqual(events[2].task.results, [5], 'results match');
516
564
  t.end();
517
565
  });
518
566
  });