Skip to content

Instantly share code, notes, and snippets.

@mpj
Created February 9, 2014 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpj/8906631 to your computer and use it in GitHub Desktop.
Save mpj/8906631 to your computer and use it in GitHub Desktop.
start of a bus
var createBus = function() {
var me = {}
var _handlers = {}
var _events = []
me.space = function(name) {
return {
on: function(address) {
return {
then: function(transform) {
_handlers[address] = _handlers[address] || []
_handlers[address].push(transform)
}
}
}
}
}
me.send = function(receivedAddress, receivedMessage) {
var deliveries = {}
deliveries[receivedAddress] = receivedMessage
if(!_handlers[receivedAddress]) {
_events.push({
unhandled: [receivedAddress, receivedMessage]
})
return;
}
_handlers[receivedAddress].forEach(function(handler) {
var event = {}
event.received = [receivedAddress, receivedMessage]
_events.push(event)
var sender = function(sendAddress, sendMessage) {
event.sent = [sendAddress, sendMessage]
me.send (sendAddress, sendMessage)
}
handler(sender, deliveries)
})
}
me.all = function() {
return _events;
}
return me
}
var run = function(spec) {
var bus = spec.go()
var wasOk = false
bus.all().forEach(function(event) {
if (event.unhandled && event.unhandled[0] === 'expectation-ok') {
wasOk = true
}
})
if (wasOk)
console.log("OK:", spec.name)
else
console.warn("FAIL:", spec.name)
}
var spec = function(bus, name) {
return {
when: function(whenAddress, whenMessage) {
return {
willSend: function(willSendAddress, willSendMessage) {
bus.space().on(willSendAddress).then(function(s,d) {
if (d[willSendAddress] === willSendMessage)
s('expectation-ok', name)
})
return {
name: name,
go: function() {
bus.send(whenAddress, whenMessage)
return bus
}
}
}
}
}
}
}
var bus = createBus()
bus
.space('app')
.on('start')
.then(function(s, d) {
s('greeting', 'hello')
})
run(spec(bus, 'it sends start')
.when('start')
.willSend('greeting', 'hello'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment