!function(){function a(a,b,c,d){function e(a,e){function f(a,b){return function(){var c=Array.prototype.slice.call(arguments);return e.then(function(){var e=Array.prototype.slice.call(arguments),f=[],g=e.shift();e.forEach(function(a){f.push(a)}),c.forEach(function(a){f.push(a)}),c=void 0,b?(a.apply(d,f),g.apply(null,e)):(f.unshift(g),a.apply(d,f))}),g}}var g={};return Object.keys(b).forEach(function(a){g[a]=f(b[a])}),Object.keys(c).forEach(function(a){g[a]=f(c[a],!0)}),g}function f(b,c){return function(){var f=Array.prototype.slice.call(arguments),g=Future(),h=Sequence();return f.unshift(g.deliver),b.apply(d,f),h.then(g.when),e(a[c],h)}}var g={};return"undefined"==typeof d&&(d=null),Object.keys(a).forEach(function(b){g[b]=f(a[b],b)}),g}this.module&&module.exports?module.exports=a:this.Chainify=a}(),function(){function a(a){return a instanceof d}function b(a){this.name="FutureTimeout",this.message="timeout "+a+"ms"}function c(a,b){var c={};return b.forEach(function(b){c[b]=function(){return a[b].apply(a,arguments),c}}),c}function d(b,c){if(!a(this))return new d(b,c);var e=this;e._everytimers={},e._onetimers={},e._index=0,e._deliveries=0,e._time=0,e._asap=!0,e._passenger=null,e.fulfilled=!1,e._global_context=b,e._global_context="undefined"==typeof e._global_context?null:e._global_context,e._options=c||{},e._options.error=e._options.error||function(a){throw a},e.errback=function(){arguments.length<2?e.deliver.call(e,arguments[0]||new Error("`errback` called without Error")):e.deliver.apply(e,arguments)},e.callback=function(){var a=Array.prototype.slice.call(arguments);a.unshift(void 0),e.deliver.apply(e,a)},e.fulfill=function(){arguments.length?e.deliver.apply(e,arguments):e.deliver(),e.fulfilled=!0},e.when=function(a,b){return e._onetimers[e._index]=!0,e.whenever(a,b),e},e.whenever=function(a,b){var c,d=e._index;return"function"!=typeof a?(e._options.error(new Error("Future().whenever(callback, [context]): callback must be a function.")),void 0):e._findCallback(a,b)?(e._options.error(new Error("Future().everytimers is a strict set. Cannot add already subscribed `callback, [context]`.")),void 0):(c=e._everytimers[d]={id:d,callback:a,context:null===b?null:b||e._global_context},e._asap&&e._deliveries>0&&(c.callback.apply(c.context,e._data),e._onetimers[d]&&(delete e._onetimers[d],delete e._everytimers[d])),e._index+=1,e._index>=f&&e._cleanup(),e)},e.deliver=function(){if(e.fulfilled)return e._options.error(new Error("`Future().fulfill(err, data, ...)` renders future deliveries useless")),void 0;var a=Array.prototype.slice.call(arguments);return e._data=a,e._deliveries+=1,Object.keys(e._everytimers).forEach(function(b){var c=e._everytimers[b],d=c.callback,f=c.context;e._onetimers[b]&&(delete e._everytimers[b],delete e._onetimers[b]),d.apply(f,a)}),a[0]&&"FutureTimeout"!==a[0].name&&e._resetTimeout(),e}}function e(a,b){return new d(a,b)}var f=Math.pow(2,52);d.prototype.setContext=function(a){var b=this;b._global_context=a},d.prototype.setTimeout=function(a){var b=this;b._time=a,b._resetTimeout()},d.prototype._resetTimeout=function(){var a=this;a._timeout_id&&(clearTimeout(a._timeout_id),a._timeout_id=void 0),a._time>0&&(a._timeout_id=setTimeout(function(){a.deliver(new b(a._time)),a._timeout_id=void 0},a._time))},d.prototype.callbackCount=function(){var a=this;return Object.keys(a._everytimers).length},d.prototype.deliveryCount=function(){var a=this;return a._deliveries},d.prototype.setAsap=function(a){var b=this;return void 0===a&&(a=!0),!0!==a&&!1!==a?(b._options.error(new Error("Future.setAsap(asap) accepts literal true or false, not "+a)),void 0):(b._asap=a,void 0)},d.prototype._findCallback=function(a,b){var c,d=this;return Object.keys(d._everytimers).forEach(function(e){var f=d._everytimers[e];a===f.callback&&(b===f.context||f.context===d._global_context)&&(c=f)}),c},d.prototype.hasCallback=function(){var a=this;return!!a._findCallback.apply(a,arguments)},d.prototype.removeCallback=function(a,b){var c=this,d=c._findCallback(a,b);return d&&(delete c._everytimers[d.id],c._onetimers[d.id]=void 0,delete c._onetimers[d.id]),c},d.prototype.passable=function(){var a=this;return a._passenger=c(a,["when","whenever"]),a._passenger},d.prototype._cleanup=function(){var a=this,b={},c={};a._index=0,Object.keys(a._everytimers).forEach(function(d){var e=b[a._index]=a._everytimers[d];a._onetimers[d]&&(c[a._index]=!0),e.id=a._index,a._index+=1}),a._onetimers=c,a._everytimers=b},d.prototype.isFuture=a,d.isFuture=a,d.create=e,this.module&&module.exports?module.exports=d:this.Future=d}(),function(){function a(a){return a instanceof b}function b(c){function e(){l&&m===h.length&&(m=0,n.deliver.apply(n,h),h=new Array(h.length),i=new Array(i.length))}function f(){var a=k?"when":"whenever";l=!0,h=new Array(j.length),i=new Array(i.length),j.forEach(function(b,c){b[a](function(){var a=Array.prototype.slice.call(arguments);h[c]=a,i[c]||(i[c]=!0,m+=1),e()})})}var g=this,h=[],i=[],j=[],k=!1,l=!1,m=0,n=d.create(c);return c=c||null,a(this)?(g.deliverer=function(){var a=d.create();return g.add(a),a.deliver},g.newCallback=g.deliverer,g.when=function(){l||f(),n.when.apply(n,arguments)},g.whenever=function(){l||f(),n.whenever.apply(n,arguments)},g.add=function(){if(l)throw new Error("`Join().add(Array | subs1, [subs2, ...])` requires that all additions be completed before the first `when()` or `whenever()`");var a=Array.prototype.slice.call(arguments);return 0===a.length?g.newCallback():(a=Array.isArray(a[0])?a[0]:a,a.forEach(function(a){if(a.whenever||(k=!0),!a.when)throw new Error("`Join().add(future)` requires either a promise or future");j.push(a)}),void 0)},void 0):new b(c)}function c(a){return new b(a)}var d=this.Future;b.create=c,b.isJoin=a,this.module&&module.exports?module.exports=b:this.Join=b}(),function(){function a(a){this.name="MaxCountReached",this.message="Loop looped "+a+" times"}function b(){return(new Date).valueOf()}function c(c){function d(){var d=Math.max(m-(b()-f),0);if(isNaN(d)&&(d=m),!h){if(o&&n>=o)return l.deliver(new a(o)),void 0;i.unshift(e),setTimeout(function(){f=b();try{j.apply(c,i),n+=1}catch(a){throw a}},d)}}function e(){return i=Array.prototype.slice.call(arguments),"break"===i[0]?(i.shift(),l.deliver.apply(l,i),void 0):(d(),void 0)}var f,g,h,i,j,k=this,l=Future(),m=0,n=0,o=0;k.setMaxLoop=function(a){return o=a,k},k.setWait=function(a){return m=a,k},k.setTimeout=function(a){if(g)throw new Error("Can't set timeout, the loop has already begun!");g=a;var b=setTimeout(function(){h=!0,l.deliver(new Error("LoopTimeout"))},g);return l.when(function(){clearTimeout(b)}),k},k.run=function(a){return i=Array.prototype.slice.call(arguments),j=a,i[0]=void 0,e.apply(k,i),k},k.when=l.when,k.whenever=l.whenever}function d(a){return new c(a)}this.module&&module.exports?module.exports=d:this.Loop=d}(),function(){function a(a){return a instanceof b}function b(c){function d(){var a=Array.prototype.slice.call(arguments),b=i.shift();return f=arguments,b?(a.unshift(d),b.callback.apply(b._context,a),void 0):(h=!0,void 0)}function e(a,b){if("function"!=typeof a)throw new Error("`Sequence().then(callback [context])` requires that `callback` be a function and that `context` be `null`, an object, or a function");return i.push({callback:a,_context:null===b?null:b||c,index:i.length}),h&&(h=!1,d.apply(null,f)),g}var f,g=this,h=!0,i=[];return a(this)?(c=c||null,g.next=d,g.then=e,void 0):new b(c)}function c(a){return new b(a)}b.create=c,b.isSequence=a,this.module&&module.exports?module.exports=b:this.Sequence=b}(),function(){var a=a||{},b={},c=function(a){if("undefined"!=typeof b[a])return b[a];console.log("create logger "+a);var c=3,d={setLevel:function(a){c=a},error:function(b){c>0&&console.log("["+a+"][ERROR] "+b)},warning:function(b){c>1&&console.log("["+a+"][WARN] "+b)},info:function(b){c>2&&console.log("["+a+"][INFO] "+b)},debug:function(b){c>3&&console.log("["+a+"][DEBUG] "+b)},fine:function(b){c>4&&console.log("["+a+"][FINE] "+b)}};return b[a]=d,d},d=function(){this.initialize&&this.initialize.apply(this,arguments)};d.extend=function(a){var b=this,c=function(){return b.apply(this,arguments)};c.extend=b.extend;var d=function(){};d.prototype=b.prototype,c.prototype=new d;for(var e in a)c.prototype[e]=a[e];return c},a.logger=c,a.Class=d,this.module&&module.exports?module.exports=a:this.macchiato=a}(),function(){"undefined"!=typeof require&&require("./macchiato-commons.js");var a=macchiato.logger("eb"),b=function(){var a=function(){return Math.floor(65536*Math.random()).toString(16)};return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()},c={all:function(){return!0}},d=function(){var d={};this.subscribe=function(a,b){var c=this.silentSubscribe(a,b,!0);return this.publish({subscription:c}),c},this.silentSubscribe=function(c,e,f){if(!c)throw"handler not specified";var g=b();a.fine("subscribe handler with id : "+g+" and filter : "+JSON.stringify(e)),d[g]={filter:e,handler:c,local:f};var h={id:g,filter:e};return h},this.unsubscribe=function(b){if(!b||!b.id)throw"you cannot register handler without subscription";if(!d[b.id])throw"unable to unregister handler: not found";a.fine("unsubscribe handler "+b.id),delete d[b.id],this.publish({unsubscription:{id:b.id}})};var e=function(a,b){var c,d=!0;if("undefined"==typeof a)return!1;if("string"==typeof a){c=new RegExp(a);try{return c.test(JSON.stringify(b))}catch(f){return!1}}else if("function"==typeof a)return a(b);for(var g in a){c=new RegExp(g);var h=!1;for(var i in b)c.test(i)&&(h=!0,d&=e(a[g],b[i]));d&=h}return d};this.publish=function(a){var b=new Future;for(var c in d){var f=d[c].filter;if(e(f,a)){var g=d[c].handler(a);b.deliver(g)}}return b.fulfill("done"),b};var f=this;f.silentSubscribe(function(a){for(var b in d)d[b].local===!0&&a.channel.sendSubscription({subscription:{id:b,filter:d[b].filter}});f.silentSubscribe(function(b){a.channel.sendSubscription(b)},{subscription:".*"}),a.channel.send(JSON.stringify({initialized:!0}))},{channel:c.all}),this.createChannel=function(a){var b=this,c=new g(a,this),d=new Future;return d.when(function(a){b.publish({channel:a})}),c.onConnected(d.fulfill),c.connect(),d}},e=macchiato.logger("channel"),f=macchiato.Class.extend({initialization:function(a){this.bus=a},cleanup:function(){},onConnected:function(a){this.connectionHandler=a},onReceived:function(a){var b=this,c=JSON.parse(a);"undefined"!=typeof c.subscription?b.bus.silentSubscribe(function(a){b.sendMessage(a)},c.subscription.filter):"undefined"!=typeof c.initialized?"undefined"!=typeof this.connectionHandler&&this.connectionHandler(this):"undefined"!=typeof c.content&&b.bus.publish(c.content)},sendMessage:function(a){this.send(JSON.stringify({content:a}))},sendSubscription:function(a){this.send(JSON.stringify(a))},send:function(){throw"not defined"},onDisconnect:function(){}}),g=f.extend({initialize:function(a,b){this.url=a,this.bus=b},connect:function(){e.fine("connect channel "+this.id+" to destination "+this.url),this.socket=new WebSocket(this.url,"macchiato-protocol");var a=this;this.socket.onopen=function(){e.info("connected to "+a.url)},this.socket.onclose=function(){e.info("connection closed : "+this.url),a.onDisconnect(),a.cleanup()},this.socket.onmessage=function(b){e.info("received message : "+b.data),a.onReceived(b.data)},this.socket.onerror=function(a){e.fine("websocket error : "+a)},this.send=function(b){e.fine("send message "+b+" to "+a.url),a.socket.send(b)},this.close=function(){a.socket.close()}}}),h=function(a){var b=new d;a(b)};macchiato.createEventApplication=h,macchiato.filters=c,macchiato.Channel=f,this.module&&module.exports?module.exports=macchiato:this.macchiato=macchiato}();