Skip to content

Instantly share code, notes, and snippets.

@swallentin
Last active October 15, 2015 13:36
Show Gist options
  • Select an option

  • Save swallentin/69f1868b072170fc4bc5 to your computer and use it in GitHub Desktop.

Select an option

Save swallentin/69f1868b072170fc4bc5 to your computer and use it in GitHub Desktop.
var _ = require('lodash'),
events = require('events'),
inEventEmitter = new events.EventEmitter(),
localEventEmitter = new events.EventEmitter(),
outEventEmitter = new events.EventEmitter(),
jobs = [{
"name": "scrape",
"in": ":Editorial:ResearchLink:Added",
"out": ":Editorial:ResearchLink:Processed",
"states": [{
"name": "fetch",
"to": "extract",
"config": {},
"execute": function (config, input, cb) {
input.message += "fetch";
cb(input);
}
}, {
"name": "extract",
"to": "persist",
"config": {},
"execute": function (config, input, cb) {
input.message += "extract";
cb(input);
}
}, {
"name": "persist",
"config": {},
"execute": function (config, input, cb) {
input.message += "persist";
cb(input);
}
}]
}];
jobs.forEach(function (job) {
// bin all incoming global job events
inEventEmitter.on(job.in, function(data) {
console.log(job.in);
var initalState = job.states[0];
localEventEmitter.emit(initalState.name, data);
});
// bind all incoming job internal events
job.states.forEach(function (state) {
localEventEmitter.on(state.name, function (data) {
state.execute(state.config, data, function (output) {
console.log(job.in + ':' + state.name);
if(!state.to) {
outEventEmitter.emit(job.out, output);
return;
}
localEventEmitter.emit(state.to, output);
});
});
});
// bind output job events
// bind input job processing events
outEventEmitter.on(job.out, function(data) {
console.log(job.out);
console.log(data);
});
});
inEventEmitter.emit(jobs[0].in, {"message": "text"});
//localEventEmitter.emit('fetch', {"message": "fetch"});
//localEventEmitter.emit('fetch', {"message": "fetch"});
//localEventEmitter.emit('fetch', {"message": "fetch"});
//localEventEmitter.emit('extract', {"message": "extract"});
//localEventEmitter.emit('persist', {"message": "persist"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment