postal.js/lib/browser/standard/postal.min.js
2012-03-01 03:40:47 -05:00

1 line
No EOL
4.8 KiB
JavaScript

(function(a,b){var c="/",d=50,e=0,f="postal",g=function(){},h=function(a){var b={envelope:{}},d;switch(a.length){case 3:typeof a[1]=="Object"&&typeof a[2]=="Object"?(b.envelope.exchange=c,b.envelope.topic=a[0],b.payload=a[1],d=b.envelope,b.envelope=_.extend(d,a[2])):(b.envelope.exchange=a[0],b.envelope.topic=a[1],b.payload=a[2]);break;case 4:b.envelope.exchange=a[0],b.envelope.topic=a[1],b.payload=a[2],d=b.envelope,b.envelope=_.extend(d,a[3]);break;default:b.envelope.exchange=c,b.envelope.topic=a[0],b.payload=a[1]}return b},i=function(){var a;return function(b){var c=!1;return _.isString(b)?(c=b===a,a=b):(c=_.isEqual(b,a),a=_.clone(b)),!c}},j=function(a,b){this.exchange=a,this.topic=b};j.prototype={subscribe:function(a){var b=new k(this.exchange,this.topic,a);return n.configuration.bus.subscribe(b),b},publish:function(a,b){var c=_.extend({exchange:this.exchange,timeStamp:new Date,topic:this.topic},b);n.configuration.bus.publish(a,c)}};var k=function(a,b,c){this.exchange=a,this.topic=b,this.callback=c,this.priority=d,this.constraints=[],this.maxCalls=e,this.onHandled=g,this.context=null,n.publish(f,"subscription.created",{event:"subscription.created",exchange:a,topic:b})};k.prototype={unsubscribe:function(){n.configuration.bus.unsubscribe(this),n.publish(f,"subscription.removed",{event:"subscription.removed",exchange:this.exchange,topic:this.topic})},defer:function(){var a=this.callback;return this.callback=function(b){setTimeout(a,0,b)},this},disposeAfter:function(a){if(_.isNaN(a)||a<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var b=this.onHandled,c=_.after(a,_.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){b.apply(this.context,arguments),c()},this},ignoreDuplicates:function(){return this.withConstraint(new i),this},whenHandledThenExecute:function(a){if(!_.isFunction(a))throw"Value provided to 'whenHandledThenExecute' must be a function";return this.onHandled=a,this},withConstraint:function(a){if(!_.isFunction(a))throw"Predicate constraint must be a function";return this.constraints.push(a),this},withConstraints:function(a){var b=this;return _.isArray(a)&&_.each(a,function(a){b.withConstraint(a)}),b},withContext:function(a){return this.context=a,this},withDebounce:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=_.debounce(b,a),this},withDelay:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=function(c){setTimeout(b,a,c)},this},withPriority:function(a){if(_.isNaN(a))throw"Priority must be a number";return this.priority=a,this},withThrottle:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=_.throttle(b,a),this}};var l={cache:{},compare:function(a,b){if(this.cache[b]&&this.cache[b][a])return!0;var c=new RegExp("^"+this.regexify(a)+"$"),d=c.test(b);return d&&(this.cache[b]||(this.cache[b]={}),this.cache[b][a]=!0),d},regexify:function(a){return a.replace(/\./g,"\\.").replace(/\*/g,".*").replace(/#/g,"[A-Z,a-z,0-9]*")}},m={subscriptions:{},wireTaps:[],publish:function(a,b){this.notifyTaps(a,b),_.each(this.subscriptions[b.exchange],function(c){_.each(c,function(c){n.configuration.resolver.compare(c.topic,b.topic)&&_.all(c.constraints,function(b){return b(a)})&&typeof c.callback=="function"&&(c.callback.apply(c.context,[a,b]),c.onHandled())})})},subscribe:function(a){var b,c,d,e,f;e=this.subscriptions[a.exchange]=this.subscriptions[a.exchange]||{},f=this.subscriptions[a.exchange][a.topic]=this.subscriptions[a.exchange][a.topic]||[],b=f.length-1;for(;b>=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}c||f.unshift(a)},notifyTaps:function(a,b){_.each(this.wireTaps,function(c){c(a,b)})},unsubscribe:function(a){if(this.subscriptions[a.exchange][a.topic]){var b=this.subscriptions[a.exchange][a.topic].length,c=0;for(;c<b;c++)if(this.subscriptions[a.exchange][a.topic][c]===a){this.subscriptions[a.exchange][a.topic].splice(c,1);break}}},addWireTap:function(a){var b=this;return b.wireTaps.push(a),function(){var c=b.wireTaps.indexOf(a);c!==-1&&b.wireTaps.splice(c,1)}}},n={configuration:{bus:m,resolver:l},channel:function(a,b){var d=b?a:c,e=b||a;return new j(d,e)},subscribe:function(a,b,d){var e=d||b,f=d?b:a,g=d?a:c,h=this.channel(g,f);return h.subscribe(e)},publish:function(a,b,c,d){var e=h([].slice.call(arguments,0)),f=this.channel(e.envelope.exchange,e.envelope.topic);f.publish(e.payload,e.envelope)},addWireTap:function(a){return this.configuration.bus.addWireTap(a)},bindExchanges:function(a,b){var d=[];return _.isArray(a)||(a=[a]),_.isArray(b)||(b=[b]),_.each(a,function(a){var e=a.topic||"*";_.each(b,function(b){var e=b.exchange||c;d.push(n.subscribe(a.exchange||c,a.topic||"*",function(a,c){var d=_.isFunction(b.topic)?b.topic(c.topic):b.topic||c.topic;n.publish(e,d,a)}))})}),d}};a.postal=n})(window)