var Scientist = function(name) { this.name = name; }; Scientist.prototype = new EventEmitter(); Scientist.prototype.hangCableOnClockTower = function() { setTimeout(function() { this.emit("Cable.Attached.To.ClockTower", this.name); }.bind(this), 500); }; Scientist.prototype.slideDownCable = function() { setTimeout(function() { this.emit("Slid.Down.Cable", this.name); this.connectCableOnStreet(); }.bind(this), 300); }; Scientist.prototype.connectCableOnStreet = function() { setTimeout(function() { this.emit("Street.Cable.Connected", this.name); }.bind(this), 300); }; var scientistBusAdapter = function(scientist, bus) { // publish bridge(s) scientist.on("Street.Cable.Connected", function(name) { bus.scientist.publish({ topic: "Street.Cable.Connected", data: { msg: name + " connected the cable on the street." } }); }); scientist.on("Slid.Down.Cable", function(name) { bus.scientist.publish({ topic: "Slid.Down.Cable", data: { msg: name + " slid down the cable to the street." } }); }); scientist.on("Cable.Attached.To.ClockTower", function(name) { bus.scientist.publish({ topic: "Cable.Attached.To.ClockTower", data: { msg: name + " attached cable to clock tower." } }); }); // subscription bridge(s) bus.timeMachine.subscribe("Reached.88mph", function(d, e) { scientist.slideDownCable(); }).withContext(scientist); }; var TimeTraveler = function(name) { this.name = name; this.id = createUUID(); }; TimeTraveler.prototype = new EventEmitter(); TimeTraveler.prototype.getInTimeMachine = function(timeMachine) { setTimeout(function() { this.timeMachine = timeMachine; this.emit("Driver.in.Time.Machine", this.name, timeMachine.name); }.bind(this), 0); }; var timeTravelerBusAdapter = function(timeTraveler, bus) { // publish bridge(s) timeTraveler.on("Driver.in.Time.Machine", function(name, timeMachineName) { bus.timeTraveler.publish({ topic: "Driver.in.Time.Machine", data: { msg: name + " is now in the " + timeMachineName + "." } }); }); // subscription bridge(s) bus.timeTraveler.subscribe("add.machine", function(d, e) { this.getInTimeMachine(d); // TODO: Change this }).withContext(timeTraveler); bus.timeTraveler.publish({ topic: "traveler.ready", data: { travelerId: timeTraveler.id, name: timeTraveler.name } }); }; var TimeMachine = function(name, targetYear) { this.name = name; this.targetYear = targetYear; this.speed = 0; this.id = createUUID(); }; TimeMachine.prototype = new EventEmitter(); TimeMachine.prototype.touchCable = function() { setTimeout(function() { this.emit("Touched.Cable", this.name); this.timeTravel(); }.bind(this), 0); }; TimeMachine.prototype.addDriver = function(driver) { setTimeout(function() { this.driver = driver; this.emit("Machine.Has.Driver", driver.name, this.name); }.bind(this), 0); }; TimeMachine.prototype.goTo88Mph = function() { setTimeout(function() { this.speed = 88; this.emit("Reached.88mph", this.name); console.log(this); }.bind(this), 1500); }; TimeMachine.prototype.timeTravel = function() { setTimeout(function() { this.emit("Time.Travel.Complete", this.name, this.driver.name, this.targetYear); }.bind(this), 0); }; var timeMachineBusAdapter = function(timeMachine, bus) { // publish bridge(s) timeMachine.on("Time.Travel.Complete", function(name, driverName, year) { bus.timeMachine.publish({ topic: "Time.Travel.Complete", data: { msg: name + " took " + driverName + " to the year " + year + "." } }); }); timeMachine.on("Reached.88mph", function(name) { bus.timeMachine.publish({ topic: "Reached.88mph", data: { msg: name + " has reached 88 mph." } }); }); timeMachine.on("Touched.Cable", function(name, driverName, year) { bus.timeMachine.publish({ topic: "Touched.Cable", data: { msg: name + " touched the cable." } }); }); // subscription bridge(s) bus.lightning.subscribe("Lightning.Strike", function(d, e) { this.touchCable(); }).withContext(timeMachine); bus.timeMachine.subscribe("add.driver", function(d, e) { this.addDriver(d); }).withContext(timeMachine); bus.timeMachine.subscribe("floor.it", function(d, e) { this.goTo88Mph(); }).withContext(timeMachine); bus.timeMachine.publish({ topic: "machine.ready", data: { timeMachineId: timeMachine.id, name: timeMachine.name } }); }; var Lightning = function() {}; Lightning.prototype = new EventEmitter(); Lightning.prototype.strike = function() { setTimeout(function() { this.emit("Lightning.Strike"); }.bind(this), 500); }; var lightningBusAdapter = function(lightning, bus) { // publish bridge(s) lightning.on("Lightning.Strike", function() { bus.lightning.publish({ topic: "Lightning.Strike", data: {} }); }); // subscription bridge(s) bus.scientist.subscribe("Street.Cable.Connected", function(d, e) { this.strike(); }).withContext(lightning); }; var Results = function ( bus ) { res = { write : function ( data ) { if(data.msg) { $( "body" ).append( "
" + data.msg + "
" ); } } }; _.each(bus, function(channel){ channel.subscribe("#", res.write); }); return res; }; function createUUID() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; return s.join(""); } postal.addWireTap(function(d, e) { console.log(JSON.stringify(e, null, 4)); });