-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (43 loc) · 1.77 KB
/
index.js
File metadata and controls
47 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Program is an entry point for BakeryJS
const {Program} = require('bakeryjs');
// In its minimal form it is initialized with at least one root directory of components
const program = new Program(
// This object is a ServiceProvider, it is passed to each component
// and can be used for dependency injection into components
// (think of e.g. logger or database handler)
{},
// These are options to initialize the Program
{
componentPaths: [`${__dirname}/components/`],
}
);
// Program is an event emitter
// 'sent' event is emitted when a message is sent between components.
// It can be used for simple tracing, as in here, or advanced instrumentations.
program.on('sent', (timestamp, source, target, batchSize) => {
console.log(
`${new Date(
timestamp
).toLocaleTimeString()} Sent: ${source} --> ${target} (${batchSize})`
);
});
// Job describes what the Program should do.
// The program can either handle multiple incoming jobs, each with unique data flow,
// or it can be kicked-off with a single job and run indifenitely
// prettier-ignore
const job = {
// at least process property is required; it contains a description of data flow
// data flow description contains a sequence of arrays of components,
// each line is run serially and components inside the array run in parallel
process: [
['helloworld'], // helloworld is a generator
['wordcount', 'punctcount'], // these are processors which will run in parallel for each emitted message
['checksum'], // this processor will run after the previous processors
]
};
// This will start the program with your job
// The second argument is an optional 'drain' function which receives
// all resulting messages which passed through the data flow
program.run(job, (msg) => {
console.log('Drain received a message', msg);
});