react 0.2.0 → 0.2.1
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.
- package/examples/default-events1.js +52 -0
- package/examples/default1.js +10 -10
- package/lib/dsl.js +3 -3
- package/package.json +1 -1
- package/test/dsl.test.js +5 -5
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*jshint white: false */
|
|
3
|
+
|
|
4
|
+
var react = require('../'); // require('react');
|
|
5
|
+
|
|
6
|
+
//output events as tasks start and complete
|
|
7
|
+
react.events.on('*', function (obj) {
|
|
8
|
+
var time = new Date();
|
|
9
|
+
time.setTime(obj.time);
|
|
10
|
+
var eventTimeStr = time.toISOString();
|
|
11
|
+
var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
|
|
12
|
+
if (obj.event === 'task.complete') {
|
|
13
|
+
console.error('%s: %s \tmsecs:(%s) \n\targs:(%s) \n\tresults:(%s)\n',
|
|
14
|
+
obj.event, obj.name, obj.elapsedTime, argsNoCb, obj.results);
|
|
15
|
+
} else {
|
|
16
|
+
console.error('%s: %s \n\targs:(%s)\n', obj.event, obj.name, argsNoCb);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
function loadUser(uid, cb){ setTimeout(cb, 100, null, "User"+uid); }
|
|
22
|
+
function loadFile(filename, cb){ setTimeout(cb, 100, null, 'Filedata'+filename); }
|
|
23
|
+
function markdown(filedata) { return 'html'+filedata; }
|
|
24
|
+
function prepareDirectory(outDirname, cb){ setTimeout(cb, 200, null, 'dircreated-'+outDirname); }
|
|
25
|
+
function writeOutput(html, user, cb){ setTimeout(cb, 300, null, html+'_bytesWritten'); }
|
|
26
|
+
function loadEmailTemplate(cb) { setTimeout(cb, 50, null, 'emailmd'); }
|
|
27
|
+
function customizeEmail(user, emailHtml, cb) { return 'cust-'+user+emailHtml; }
|
|
28
|
+
function deliverEmail(custEmailHtml, cb) { setTimeout(cb, 100, null, 'delivered-'+custEmailHtml); }
|
|
29
|
+
|
|
30
|
+
function useHtml(err, html, user, bytesWritten) {
|
|
31
|
+
if (err) {
|
|
32
|
+
console.log('***Error: %s', err);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
console.log('final result: %s, user: %s, written:%s', html, user, bytesWritten);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var loadAndSave = react('loadAndSave', 'filename, uid, outDirname, cb -> err, html, user, bytesWritten', // name, in/out params
|
|
39
|
+
loadUser, 'uid, cb -> err, user', // calling async fn loadUser with uid, callback is called with err and user
|
|
40
|
+
loadFile, 'filename, cb -> err, filedata',
|
|
41
|
+
markdown, 'filedata -> html', // using a sync function
|
|
42
|
+
prepareDirectory, 'outDirname, cb -> err, dircreated',
|
|
43
|
+
writeOutput, 'html, user, cb -> err, bytesWritten', { after: prepareDirectory }, // only after prepareDirectory done
|
|
44
|
+
loadEmailTemplate, 'cb -> err, emailmd',
|
|
45
|
+
markdown, 'emailmd -> emailHtml', // using a sync function
|
|
46
|
+
customizeEmail, 'user, emailHtml, cb -> err, custEmailHtml',
|
|
47
|
+
deliverEmail, 'custEmailHtml, cb -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
loadAndSave('file.md', 100, '/tmp/foo', useHtml); // executing the flow
|
|
51
|
+
|
|
52
|
+
|
package/examples/default1.js
CHANGED
|
@@ -23,16 +23,16 @@ function useHtml(err, html, user, bytesWritten) {
|
|
|
23
23
|
// define fn, glue together with react, it will parallelize
|
|
24
24
|
// starts with name and in/out params, then the tasks
|
|
25
25
|
var loadAndSave = react('loadAndSave', 'fName, uid, outDir, cb -> err, html, user, bytes',
|
|
26
|
-
loadUser, 'uid
|
|
27
|
-
loadFile, 'filename
|
|
28
|
-
markdown, 'filedata ->
|
|
29
|
-
prepareDirectory, 'outDirname
|
|
30
|
-
writeOutput, 'html, user
|
|
31
|
-
loadEmailTemplate, '
|
|
32
|
-
markdown, 'emailmd ->
|
|
33
|
-
customizeEmail, 'user, emailHtml ->
|
|
34
|
-
deliverEmail, 'custEmailHtml -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
|
|
35
|
-
);
|
|
26
|
+
loadUser, 'uid, cb -> err, user', // calling async loadUser with uid, cb called with err and user
|
|
27
|
+
loadFile, 'filename, cb -> err, filedata',
|
|
28
|
+
markdown, 'filedata -> html', // using a sync function
|
|
29
|
+
prepareDirectory, 'outDirname, cb -> err, dircreated',
|
|
30
|
+
writeOutput, 'html, user, cb -> err, bytesWritten', { after: prepareDirectory }, // only after prepareDirectory done
|
|
31
|
+
loadEmailTemplate, 'cb -> err, emailmd',
|
|
32
|
+
markdown, 'emailmd -> emailHtml', // using a sync function
|
|
33
|
+
customizeEmail, 'user, emailHtml, cb -> custEmailHtml',
|
|
34
|
+
deliverEmail, 'custEmailHtml, cb -> err, deliveredEmail', { after: writeOutput } // only after writeOutput is done
|
|
35
|
+
);
|
|
36
36
|
|
|
37
37
|
//in another module you can use this as any other exported fn
|
|
38
38
|
loadAndSave('file.md', 100, '/tmp/foo', useHtml); // executing the flow
|
package/lib/dsl.js
CHANGED
|
@@ -63,12 +63,12 @@ function parseTasks(arr) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
Parse the variable arguments into in/out params,
|
|
66
|
+
Parse the variable arguments into in/out params, options, tasks
|
|
67
67
|
*/
|
|
68
68
|
function parseVargs(vargs) {
|
|
69
69
|
var inOutParamStr = vargs.shift() || '';
|
|
70
|
-
// if
|
|
71
|
-
var options = (vargs.length && typeof(vargs[
|
|
70
|
+
// if next arg is object, shift it off as options
|
|
71
|
+
var options = (vargs.length && typeof(vargs[0]) === 'object') ? vargs.shift() : { };
|
|
72
72
|
var taskDefArr = vargs; // rest are for the tasks
|
|
73
73
|
var defObj = {
|
|
74
74
|
inOutParamStr: inOutParamStr,
|
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.2.
|
|
4
|
+
"version": "0.2.1",
|
|
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" },
|
package/test/dsl.test.js
CHANGED
|
@@ -129,9 +129,9 @@ test('two inputs, two tasks, two out params', function (t) {
|
|
|
129
129
|
|
|
130
130
|
test('two inputs, two tasks, two out params, options', function (t) {
|
|
131
131
|
var r = react('myName', 'a, b, cb -> err, c, d',
|
|
132
|
+
{ otherOptFoo: 'foo'}, // main flow options
|
|
132
133
|
falpha, 'a, b, cb -> err, c',
|
|
133
|
-
fbeta, 'a, b, cb -> err, d, e'
|
|
134
|
-
{ name: 'myflow', otherOptFoo: 'foo'}
|
|
134
|
+
fbeta, 'a, b, cb -> err, d, e'
|
|
135
135
|
);
|
|
136
136
|
t.deepEqual(r.ast.inParams, ['a', 'b']);
|
|
137
137
|
t.deepEqual(r.ast.tasks, [
|
|
@@ -139,7 +139,7 @@ test('two inputs, two tasks, two out params, options', function (t) {
|
|
|
139
139
|
{ f: fbeta, a: ['a', 'b'], out: ['d', 'e'], type: 'cb', name: 'fbeta'}
|
|
140
140
|
]);
|
|
141
141
|
t.deepEqual(r.ast.outTask, { a: ['c', 'd'], type: 'finalcb' });
|
|
142
|
-
t.equal(r.ast.name, '
|
|
142
|
+
t.equal(r.ast.name, 'myName', 'name should match');
|
|
143
143
|
t.equal(r.ast.otherOptFoo, 'foo', 'other options should pass through');
|
|
144
144
|
t.end();
|
|
145
145
|
});
|
|
@@ -220,9 +220,9 @@ test('not enough args throws error', function (t) {
|
|
|
220
220
|
|
|
221
221
|
test('selectFirst', function (t) {
|
|
222
222
|
var r = react.selectFirst('myName', 'a, b, cb -> c',
|
|
223
|
+
{ otherOptFoo: 'foo'}, // main options
|
|
223
224
|
falpha, 'a, b, cb -> err, c',
|
|
224
|
-
fbeta, 'a, b -> c'
|
|
225
|
-
{ otherOptFoo: 'foo'}
|
|
225
|
+
fbeta, 'a, b -> c'
|
|
226
226
|
);
|
|
227
227
|
t.equal(r.ast.name, 'myName');
|
|
228
228
|
t.deepEqual(r.ast.inParams, ['a', 'b']);
|