Last active
December 25, 2015 02:19
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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