Skip to content

Instantly share code, notes, and snippets.

@SteveBate
Last active December 25, 2015 02:19
Show Gist options
  • Save SteveBate/6901605 to your computer and use it in GitHub Desktop.
Save SteveBate/6901605 to your computer and use it in GitHub Desktop.
A basic Javascript implementation of the messaging pipeline as described at http://eventuallyconsistent.net/2013/08/12/messaging-as-a-programming-model-part-1/
var utils = utils || {};
// pipeline
utils.pipeline = function(){
var handlers = [];
var register = function(handler){
if(typeof handler !== 'function')
throw { name: 'InvalidTypeException', description: 'handler should be a function'}
handlers.push(handler);
}
var execute = function(msg, cb){
var stop = false;
for(var i = 0; i < handlers.length && stop === false; i++){
handlers[i](msg, function(err){
stop = true;
cb(err);
});
}
};
return {
register : register,
execute : execute
}
}
// message
var msg = { name: 'joe bloggs', age: 30, status: 'married' };
// filters
var step1 = function(msg, cb){
console.log(msg.name);
cb({ name: 'SomeException', description:'step 1 exception'});
}
var step2 = function(msg, cb){
console.log(msg.age);
cb({ name: 'SomeException', description:'step 2 exception'});
}
var step3 = function(msg, cb){
console.log(msg.status);
}
// setup
var p = new pipeline.pipeline();
p.register(step1)
p.register(step2)
p.register(step3)
// invoke
p.execute(msg, function(err){
console.log(err.description);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment