var __slice=Array.prototype.slice;define(function(){var a,b,c,d,e,f,g;return e=1,g=1,f=1,d=function(){function a(a,b,c){this.publisher=a,this.handler=b,this.context=c,this.id=g++,this.online=!0,this.tip=null}return a}(),a=function(){function a(a,b){this.publisher=a,this.content=b,this.id=f++,this.previous=this.publisher.tip(),this.publisher.messages.push(this)}return a.prototype.copy=function(){return this.content.slice()},a.prototype.available=function(){return this.publisher.active},a}(),c=function(){function a(a){this.topic=a,this.subscribers=[],this.messages=[],this.active=!0}return a.prototype.tip=function(){return this.messages[this.messages.length-1]},a.prototype._add=function(a){return this.subscribers.push(a)},a.prototype._remove=function(a){var b,c;b=this.subscribers.length,c=[];while(b--)if(a===this.subscribers[b]){this.subscriber.pop(b);break}return c},a}(),b=function(){function b(a){this.debug=a!=null?a:!1,this.id=e++,this.publishers={},this.subscribers={},this.messages={}}return b.prototype.version="0.3",b.prototype._addPublisher=function(a){var b;return(b=this.publishers[a])||(b=new c(a),this.publishers[a]=b,this.log("Publisher '"+b.topic+"' added")),b},b.prototype._addSubscriber=function(a,b,c){var e;return e=new d(a,b,c),this.subscribers[e.id]=e,a._add(e),this.log("Subscriber #"+e.id+" added"),e},b.prototype.subscribe=function(a,b,c,e){var f,g,h;e==null&&(e=!0);if(a instanceof d)g=a,g.online=!0,f=g.publisher,e=b||e,this.log("Subscriber #"+g.id+" online");else{if(c===!0||c==="tip"||c===!1)h=[c,e],e=h[0],c=h[1];f=this._addPublisher(a),g=this._addSubscriber(f,b,c)}return e&&this._migrate(f,g,e),g},b.prototype.unsubscribe=function(a,b){b==null&&(b=!1);if(!a instanceof d&&!(a=this.subscribers[a]))return;return b?(delete this.subscribers[a.id],a.publisher._remove(a),this.log("Subscriber #"+a.id+" removed")):(a.online=!1,this.log("Subscriber #"+a.id+" offline"))},b.prototype.publish=function(){var a,b,c,d;d=arguments[0],a=2>arguments.length?[]:__slice.call(arguments,1),c=this._addPublisher(d);if(!c.active)return;return this.log("'"+c.topic+"' published ->",a),b=this._record(c,a),this._applyToAll(b),c},b.prototype._migrate=function(a,b,c){var d,e;if(a.messages.length)return c==="tip"?d=[a.tip()]:d=a.messages,e=b.tip?b.tip.id:-1,this._migrateAll(d,b,e)},b.prototype._migrateAll=function(a,b,c){var d,e,f,g;g=[];for(e=0,f=a.length;e<f;e++){d=a[e];if(d.id>c)g.push(this._applyToOne(d,b));else continue}return g},b.prototype._applyToAll=function(a,b){var c,d,e,f;f=a.publisher.subscribers;for(d=0,e=f.length;d<e;d++)c=f[d],this._applyToOne(a,c,b)},b.prototype._applyToOne=function(a,b){if(!b.online)return;return this.log("Subscriber #"+b.id+" <-",a.content),b.handler.apply(b.context,a.copy()),b.tip=a},b.prototype._record=function(b,c){var d;return d=new a(b,c),this.messages[d.id]=this,d},b}(),(typeof console!="undefined"&&console!==null?console.log:void 0)?b.prototype.log=function(){var a,b;b=arguments[0],a=2>arguments.length?[]:__slice.call(arguments,1);if(this.debug)return b="Hub #"+this.id+": "+b,a.length?console.log.apply(console,[b].concat(__slice.call(a))):console.log(b)}:b.prototype.log=function(){},b})