react 0.3.0 → 0.3.4

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.
@@ -0,0 +1,117 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ Track the tasks, start, complete, args, results, elapsed time
5
+ Emits events that can be monitored
6
+
7
+ - track start and complete
8
+ - record args each task was called with
9
+ - record results at completion
10
+ - record start, end, and calc elapsed time
11
+ - emits flow.begin with flowEnv
12
+ - emits task.begin with task
13
+ - emits task.complete with task
14
+ - emits flow complete with flowEnv
15
+ - emits flow errored with flowEnv
16
+ */
17
+
18
+
19
+ var react = require('../'); // require('react');
20
+
21
+ var TASK_EVENTS_RE = /^task\./;
22
+ var FLOW_EVENTS_RE = /^flow\./;
23
+
24
+ /**
25
+ Accumulator to make it easy to capture events
26
+
27
+ @example
28
+ var EventCollector = require('react/lib/track-tasks').EventCollector;
29
+ var collector = new EventCollector();
30
+
31
+ collector.captureGlobal('*'); // capture all react events for all flows
32
+
33
+ // OR
34
+
35
+ collector.capture(flowFn, 'task.'); // capture task events on a flow
36
+ collector.capture(flowFn, 'flow.'); // add capture flow events on a flow
37
+
38
+ var events = collector.list(); // retrieve the list of events
39
+ */
40
+ function EventCollector() {
41
+ this.events = [];
42
+ }
43
+
44
+ /**
45
+ register listener to capture all events
46
+ @param eventId event id or wildcarded id
47
+ */
48
+ EventCollector.prototype.captureGlobal = function (eventId) {
49
+ this.capture(react, eventId);
50
+ };
51
+
52
+ /**
53
+ register listener to capture events for a specific flow
54
+ @param flowFn the react flow function or can pass global react
55
+ @param eventId event id or wildcarded id
56
+ */
57
+ EventCollector.prototype.capture = function (flowFn, eventId) {
58
+ var emitter = flowFn.events;
59
+ var self = this;
60
+ function accumEvents(obj) {
61
+ var eventObject = {
62
+ event: this.event,
63
+ time: Date.now()
64
+ };
65
+ if (FLOW_EVENTS_RE.test(this.event)) {
66
+ eventObject.env = obj;
67
+ } else if (TASK_EVENTS_RE.test(this.event)) {
68
+ eventObject.task = obj;
69
+ }
70
+ self.events.push(eventObject);
71
+ }
72
+ emitter.on(eventId, accumEvents);
73
+ };
74
+
75
+ EventCollector.prototype.list = function () {
76
+ return this.events;
77
+ };
78
+
79
+ react.events.on(react.events.TYPES.EXEC_FLOW_START, function (env){
80
+ env.startTime = Date.now();
81
+ env.flowEmitter.emit(react.events.TYPES.FLOW_BEGIN, env); //fire public ev
82
+ });
83
+
84
+ react.events.on(react.events.TYPES.EXEC_TASK_START, function (task) {
85
+ task.startTime = Date.now();
86
+ task.env.flowEmitter.emit(react.events.TYPES.TASK_BEGIN, task); //fire public ev
87
+ });
88
+
89
+ react.events.on(react.events.TYPES.EXEC_TASK_COMPLETE, function (task) {
90
+ task.endTime = Date.now();
91
+ task.elapsedTime = task.endTime - task.startTime;
92
+ task.env.flowEmitter.emit(react.events.TYPES.TASK_COMPLETE, task); // fire public ev
93
+ });
94
+
95
+ react.events.on(react.events.TYPES.EXEC_TASK_ERRORED, function (task) {
96
+ task.endTime = Date.now();
97
+ task.elapsedTime = task.endTime - task.startTime;
98
+ task.env.flowEmitter.emit(react.events.TYPES.TASK_ERRORED, task); // fire public ev
99
+ });
100
+
101
+ react.events.on(react.events.TYPES.EXEC_FLOW_COMPLETE, function (env) {
102
+ env.endTime = Date.now();
103
+ env.elapsedTime = env.endTime - env.startTime;
104
+ env.flowEmitter.emit(react.events.TYPES.FLOW_COMPLETE, env); //fire public ev
105
+ });
106
+
107
+ react.events.on(react.events.TYPES.EXEC_FLOW_ERRORED, function (env) {
108
+ env.endTime = Date.now();
109
+ env.elapsedTime = env.endTime - env.startTime;
110
+ env.flowEmitter.emit(react.events.TYPES.FLOW_ERRORED, env); //fire public ev
111
+ });
112
+
113
+
114
+
115
+
116
+ module.exports = react; // return react
117
+ module.exports.EventCollector = EventCollector;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react",
3
3
  "description": "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.",
4
- "version": "0.3.0",
4
+ "version": "0.3.4",
5
5
  "author": "Jeff Barczewski <jeff.barczewski@gmail.com>",
6
6
  "repository": { "type": "git", "url": "http://github.com/jeffbski/react.git" },
7
7
  "bugs" : { "url": "http://github.com/jeffbski/react/issues" },
@@ -14,9 +14,9 @@ var react = require('./'); // require('react');
14
14
 
15
15
  var PROMISE_SUFFIX = '__promise'; // added to param names that are promises
16
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) {
17
+ react.events.on(react.events.TYPES.EXEC_TASKS_PRECREATE, function (env) {
18
+ var vConValues = env.vCon.values;
19
+ var promiseParams = env.ast.inParams.filter(function (p) {
20
20
  var value = vConValues[p];
21
21
  return (value && typeof(value.then) === 'function');
22
22
  });
@@ -24,7 +24,7 @@ react.events.on(react.events.TYPES.EXEC_TASKS_PRECREATE, function (taskEnv) {
24
24
  var promiseName = p + PROMISE_SUFFIX;
25
25
  vConValues[promiseName] = vConValues[p];
26
26
  vConValues[p] = undefined;
27
- taskEnv.taskDefs.push({
27
+ env.taskDefs.push({
28
28
  type: 'when',
29
29
  a: [promiseName],
30
30
  out: [p]
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/track-tasks').EventCollector; // require('react/lib/track-tasks'); // turn on tracking
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
  });
@@ -451,18 +448,16 @@ test('selectFirst with first succeeding', function (t) {
451
448
  });
452
449
  t.deepEqual(errors, [], 'no validation errors');
453
450
 
454
- var events = [];
455
- function accumEvents(task) {
456
- events.push(task);
457
- }
458
- fn.events.on('task.complete', accumEvents);
451
+ var collector = new EventCollector();
452
+ collector.capture(fn, 'task.complete');
459
453
 
460
454
  fn(2, 3, function (err, c) {
461
455
  t.equal(err, null);
462
456
  t.equal(c, 6);
457
+ var events = collector.list();
463
458
  t.equal(events.length, 1, 'should have seen one task compl events');
464
- t.equal(events[0].name, 'multiply', 'name matches');
465
- 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');
466
461
  t.end();
467
462
  });
468
463
  });
@@ -484,18 +479,16 @@ test('selectFirst with third succeeding', function (t) {
484
479
  });
485
480
  t.deepEqual(errors, [], 'no validation errors');
486
481
 
487
- var events = [];
488
- function accumEvents(task) {
489
- events.push(task);
490
- }
491
- fn.events.on('task.complete', accumEvents);
482
+ var collector = new EventCollector();
483
+ collector.capture(fn, 'task.complete');
492
484
 
493
485
  fn(2, 3, function (err, c) {
494
486
  t.equal(err, null);
495
487
  t.equal(c, 5);
488
+ var events = collector.list();
496
489
  t.equal(events.length, 3, 'should have seen three task compl events');
497
- t.equal(events[2].name, 'add', 'name matches');
498
- 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');
499
492
  t.end();
500
493
  });
501
494
  });
@@ -521,20 +514,18 @@ test('selectFirst forces order with third succeeding', function (t) {
521
514
  });
522
515
  t.deepEqual(errors, [], 'no validation errors');
523
516
 
524
- var events = [];
525
- function accumEvents(task) {
526
- events.push(task);
527
- }
528
- fn.events.on('task.complete', accumEvents);
517
+ var collector = new EventCollector();
518
+ collector.capture(fn, 'task.complete');
529
519
 
530
520
  fn(2, 3, function (err, c) {
531
521
  t.equal(err, null);
532
522
  t.equal(c, 5);
523
+ var events = collector.list();
533
524
  t.equal(events.length, 3, 'should have seen three task compl events');
534
- t.equal(events[0].name, 'noSuccess', 'name matches');
535
- t.equal(events[1].name, 'noSuccessNull', 'name matches');
536
- t.equal(events[2].name, 'add', 'name matches');
537
- 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');
538
529
  t.end();
539
530
  });
540
531
  });
@@ -560,18 +551,16 @@ test('selectFirst using direct returns', function (t) {
560
551
  });
561
552
  t.deepEqual(errors, [], 'no validation errors');
562
553
 
563
- var events = [];
564
- function accumEvents(task) {
565
- events.push(task);
566
- }
567
- fn.events.on('task.complete', accumEvents);
554
+ var collector = new EventCollector();
555
+ collector.capture(fn, 'task.complete');
568
556
 
569
557
  fn(2, 3, function (err, c) {
570
558
  t.equal(err, null);
571
559
  t.equal(c, 5);
560
+ var events = collector.list();
572
561
  t.equal(events.length, 3, 'should have seen three task compl events');
573
- t.equal(events[2].name, 'addRet', 'name matches');
574
- 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');
575
564
  t.end();
576
565
  });
577
566
  });
@@ -4,6 +4,7 @@ var test = require('tap').test;
4
4
  var sprintf = require('sprintf').sprintf;
5
5
 
6
6
  var chainDefine = require('../../dsl/chain'); // require('react/dsl/chain');
7
+ require('../../lib/track-tasks'); // require('react/lib/track-tasks'); // turn on tracking
7
8
 
8
9
  function falpha() { }
9
10
  function fbeta() { }
@@ -4,6 +4,7 @@ var test = require('tap').test;
4
4
  var sprintf = require('sprintf').sprintf;
5
5
 
6
6
  var pcode = require('../../dsl/pcode'); // require('react/dsl/pcode');
7
+ var EventCollector = require('../../lib/track-tasks').EventCollector; // require('react/lib/track-tasks'); // turn on tracking
7
8
 
8
9
  function falpha() { }
9
10
  function fbeta() { }
@@ -359,11 +360,6 @@ test('use pcodeDefine with events', function (t) {
359
360
  function multiply(a, b, cb) { cb(null, a * b); }
360
361
  function add(a, b, cb) { cb(null, a + b); }
361
362
 
362
- var events = [];
363
- function accumEvents(task) {
364
- events.push(task);
365
- }
366
-
367
363
  var locals = { multiply: multiply, add: add };
368
364
  var fn = pcode('a, b, cb', [
369
365
  'm := multiply(a, b)',
@@ -371,17 +367,19 @@ test('use pcodeDefine with events', function (t) {
371
367
  'cb(err, m, s)'
372
368
  ], locals);
373
369
 
374
- fn.events.on('task.complete', accumEvents);
370
+ var collector = new EventCollector();
371
+ collector.capture(fn, 'task.complete');
375
372
 
376
373
  fn(2, 3, function (err, m, s) {
377
374
  t.deepEqual(err, null, 'should not be any error');
378
375
  t.equal(m, 6);
379
376
  t.equal(s, 8);
377
+ var events = collector.list();
380
378
  t.equal(events.length, 2, 'should have seen two task compl events');
381
- t.equal(events[0].name, 'multiply', 'name matches');
382
- t.deepEqual(events[0].results, [6], 'results match');
383
- t.equal(events[1].name, 'add', 'name matches');
384
- t.deepEqual(events[1].results, [8], 'results match');
379
+ t.equal(events[0].task.name, 'multiply', 'name matches');
380
+ t.deepEqual(events[0].task.results, [6], 'results match');
381
+ t.equal(events[1].task.name, 'add', 'name matches');
382
+ t.deepEqual(events[1].task.results, [8], 'results match');
385
383
  t.end();
386
384
  });
387
385
  });
@@ -394,11 +392,6 @@ test('use pcodeDefine.selectFirst with events', function (t) {
394
392
  function noSuccessNull(a, b, cb) { cb(null, null); } // returns null result
395
393
  function add(a, b, cb) { cb(null, a + b); }
396
394
 
397
- var events = [];
398
- function accumEvents(task) {
399
- events.push(task);
400
- }
401
-
402
395
  var locals = { noSuccess: noSuccess, noSuccessNull: noSuccessNull, add: add };
403
396
  var fn = pcode.selectFirst('a, b, cb', [
404
397
  'c := noSuccess(a, b)',
@@ -408,16 +401,18 @@ test('use pcodeDefine.selectFirst with events', function (t) {
408
401
  'cb(err, c)'
409
402
  ], locals);
410
403
 
411
- fn.events.on('task.complete', accumEvents);
404
+ var collector = new EventCollector();
405
+ collector.capture(fn, 'task.complete');
412
406
 
413
407
  fn(2, 3, function (err, c) {
414
408
  t.deepEqual(err, null, 'should not be any error');
415
409
  t.equal(c, 5);
410
+ var events = collector.list();
416
411
  t.equal(events.length, 3, 'should have seen two task compl events');
417
- t.equal(events[0].name, 'noSuccess', 'name matches');
418
- t.equal(events[1].name, 'noSuccessNull', 'name matches');
419
- t.equal(events[2].name, 'add', 'name matches');
420
- t.deepEqual(events[2].results, [5], 'results match');
412
+ t.equal(events[0].task.name, 'noSuccess', 'name matches');
413
+ t.equal(events[1].task.name, 'noSuccessNull', 'name matches');
414
+ t.equal(events[2].task.name, 'add', 'name matches');
415
+ t.deepEqual(events[2].task.results, [5], 'results match');
421
416
  t.end();
422
417
  });
423
418
  });
@@ -427,11 +422,6 @@ test('use pcodeDefine events emit to global emitter', function (t) {
427
422
  function multiply(a, b, cb) { cb(null, a * b); }
428
423
  function add(a, b, cb) { cb(null, a + b); }
429
424
 
430
- var events = [];
431
- function accumEvents(task) {
432
- events.push(task);
433
- }
434
-
435
425
  var locals = { multiply: multiply, add: add };
436
426
  var fn = pcode('a, b, cb', [
437
427
  'm := multiply(a, b)',
@@ -439,17 +429,19 @@ test('use pcodeDefine events emit to global emitter', function (t) {
439
429
  'cb(err, m, s)'
440
430
  ], locals);
441
431
 
442
- pcode.events.on('task.complete', accumEvents); // the global react emitter
432
+ var collector = new EventCollector();
433
+ collector.capture(pcode, 'task.complete'); // the global react emitter
443
434
 
444
435
  fn(2, 3, function (err, m, s) {
445
436
  t.deepEqual(err, null, 'should not be any error');
446
437
  t.equal(m, 6);
447
438
  t.equal(s, 8);
439
+ var events = collector.list();
448
440
  t.equal(events.length, 2, 'should have seen two task compl events');
449
- t.equal(events[0].name, 'multiply', 'name matches');
450
- t.deepEqual(events[0].results, [6], 'results match');
451
- t.equal(events[1].name, 'add', 'name matches');
452
- t.deepEqual(events[1].results, [8], 'results match');
441
+ t.equal(events[0].task.name, 'multiply', 'name matches');
442
+ t.deepEqual(events[0].task.results, [6], 'results match');
443
+ t.equal(events[1].task.name, 'add', 'name matches');
444
+ t.deepEqual(events[1].task.results, [8], 'results match');
453
445
  t.end();
454
446
  });
455
447
  });