mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-04-24 00:24:48 +00:00
1 line
No EOL
4.6 KiB
JavaScript
1 line
No EOL
4.6 KiB
JavaScript
define(["underscore"],function(a){var b="/",c=50,d=0,e=function(){},f=function(c){var d={envelope:{}},e;switch(c.length){case 3:typeof c[1]=="Object"&&typeof c[2]=="Object"?(d.envelope.exchange=b,d.envelope.topic=c[0],d.payload=c[1],e=d.envelope,d.envelope=a.extend(e,c[2])):(d.envelope.exchange=c[0],d.envelope.topic=c[1],d.payload=c[2]);break;case 4:d.envelope.exchange=c[0],d.envelope.topic=c[1],d.payload=c[2],e=d.envelope,d.envelope=a.extend(e,c[3]);break;default:d.envelope.exchange=b,d.envelope.topic=c[0],d.payload=c[1]}return d},g=function(){var b;return function(c){var d=!1;return a.isString(c)?(d=c===b,b=c):(d=a.isEqual(c,b),b=a.clone(c)),!d}},h=function(a,b){this.exchange=a,this.topic=b};h.prototype={subscribe:function(a){var b=new i(this.exchange,this.topic,a);return l.configuration.bus.subscribe(b),b},publish:function(b,c){var d=a.extend({exchange:this.exchange,timeStamp:new Date,topic:this.topic},c);l.configuration.bus.publish(b,d)}};var i=function(a,b,f){this.exchange=a,this.topic=b,this.callback=f,this.priority=c,this.constraints=[],this.maxCalls=d,this.onHandled=e,this.context=null};i.prototype={unsubscribe:function(){l.configuration.bus.unsubscribe(this)},defer:function(){var a=this.callback;return this.callback=function(b){setTimeout(a,0,b)},this},disposeAfter:function(b){if(a.isNaN(b)||b<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var c=this.onHandled,d=a.after(b,a.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){c.apply(this.context,arguments),d()},this},ignoreDuplicates:function(){return this.withConstraint(new g),this},whenHandledThenExecute:function(b){if(!a.isFunction(b))throw"Value provided to 'whenHandledThenExecute' must be a function";return this.onHandled=b,this},withConstraint:function(b){if(!a.isFunction(b))throw"Predicate constraint must be a function";return this.constraints.push(b),this},withConstraints:function(b){var c=this;return a.isArray(b)&&a.each(b,function(a){c.withConstraint(a)}),c},withContext:function(a){return this.context=a,this},withDebounce:function(b){if(a.isNaN(b))throw"Milliseconds must be a number";var c=this.callback;return this.callback=a.debounce(c,b),this},withDelay:function(b){if(a.isNaN(b))throw"Milliseconds must be a number";var c=this.callback;return this.callback=function(a){setTimeout(c,b,a)},this},withPriority:function(b){if(a.isNaN(b))throw"Priority must be a number";return this.priority=b,this},withThrottle:function(b){if(a.isNaN(b))throw"Milliseconds must be a number";var c=this.callback;return this.callback=a.throttle(c,b),this}};var j={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]*")}},k={subscriptions:{},wireTaps:[],publish:function(b,c){a.each(this.wireTaps,function(a){a(b,c)}),a.each(this.subscriptions[c.exchange],function(d){a.each(d,function(d){l.configuration.resolver.compare(d.topic,c.topic)&&a.all(d.constraints,function(a){return a(b)})&&typeof d.callback=="function"&&(d.callback.apply(d.context,[b,c]),d.onHandled())})})},subscribe:function(b){var c,d,e;this.subscriptions[b.exchange]||(this.subscriptions[b.exchange]={}),this.subscriptions[b.exchange][b.topic]||(this.subscriptions[b.exchange][b.topic]=[]),c=this.subscriptions[b.exchange][b.topic].length-1;if(!a.any(this.subscriptions[b.exchange][b.topic],function(a){return a===b})){for(;c>=0;c--)if(this.subscriptions[b.exchange][b.topic][c].priority<=b.priority){this.subscriptions[b.exchange][b.topic].splice(c+1,0,b),d=!0;break}d||this.subscriptions[b.exchange][b.topic].unshift(b)}return a.bind(function(){this.unsubscribe(b)},this)},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){return this.wireTaps.push(a),function(){var b=this.wireTaps.indexOf(a);b!==-1&&this.wireTaps.splice(b,1)}}},l={configuration:{bus:k,resolver:j},channel:function(a,c){var d=arguments.length===2?a:b,e=arguments.length===2?c:a;return new h(d,e)},subscribe:function(a,c,d){var e=arguments.length===3?a:b,f=arguments.length===3?c:a,g=arguments.length===3?d:c,h=this.channel(e,f);return h.subscribe(g)},publish:function(a,b,c,d){var e=f([].slice.call(arguments,0)),g=this.channel(e.envelope.exchange,e.envelope.topic);g.publish(e.payload,e.envelope)},addWireTap:function(a){this.configuration.bus.addWireTap(a)}};return l}) |