mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-04-30 03:24:42 +00:00
1 line
No EOL
4.5 KiB
JavaScript
1 line
No EOL
4.5 KiB
JavaScript
define(["underscore"],function(a){var b="/",c=50,d=0,e="postal",f=function(){},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.channel=a,this.topic=b||""};h.prototype={subscribe:function(a){return new i(this.channel,this.topic,a)},publish:function(a,b){var c=b||{};c.channel=this.channel,c.timeStamp=new Date,c.topic=c.topic||this.topic,m.configuration.bus.publish(c,a)}};var i=function(a,b,g){this.channel=a,this.topic=b,this.callback=g,this.priority=c,this.constraints=new Array(0),this.maxCalls=d,this.onHandled=f,this.context=null,m.configuration.bus.subscribe(this),m.publish({channel:e,topic:"subscription.created"},{event:"subscription.created",channel:a,topic:b})};i.prototype={unsubscribe:function(){m.configuration.bus.unsubscribe(this),m.publish({channel:e,topic:"subscription.removed"},{event:"subscription.removed",channel:this.channel,topic:this.topic})},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("^"+a.replace(/\./g,"\\.").replace(/\*/g,".*").replace(/#/g,"[A-Z,a-z,0-9]*")+"$"),d=c.test(b);return d&&(this.cache[b]||(this.cache[b]={}),this.cache[b][a]=!0),d}},k={subscriptions:{},wireTaps:new Array(0),publish:function(b,c){a.each(this.wireTaps,function(a){a(b,c)}),a.each(this.subscriptions[b.channel],function(d){a.each(d,function(d){m.configuration.resolver.compare(d.topic,b.topic)&&a.all(d.constraints,function(a){return a(c)})&&typeof d.callback=="function"&&(d.callback.apply(d.context,[c,b]),d.onHandled())})})},subscribe:function(a){var b,c,d,e=this.subscriptions[a.channel],f;e||(e=this.subscriptions[a.channel]={}),f=this.subscriptions[a.channel][a.topic],f||(f=this.subscriptions[a.channel][a.topic]=new Array(0)),b=f.length-1;for(;b>=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}return c||f.unshift(a),a},unsubscribe:function(a){if(this.subscriptions[a.channel][a.topic]){var b=this.subscriptions[a.channel][a.topic].length,c=0;for(;c<b;c++)if(this.subscriptions[a.channel][a.topic][c]===a){this.subscriptions[a.channel][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)}}},l={2:function(a,c){a.channel||(a.channel=b),m.configuration.bus.publish(a,c)},3:function(a,b,c){m.configuration.bus.publish({channel:a,topic:b},c)}};k.subscriptions[e]={};var m={configuration:{bus:k,resolver:j},channel:function(a){var c=a.channel||b,d=a.topic;return new h(c,d)},subscribe:function(a){var c=a.callback,d=a.topic,e=a.channel||b;return new i(e,d,c)},publish:function(){var a=arguments.length;l[a]&&l[a].apply(this,arguments)},addWireTap:function(a){return this.configuration.bus.addWireTap(a)},linkChannels:function(c,d){var e=[];return a.isArray(c)||(c=[c]),a.isArray(d)||(d=[d]),a.each(c,function(c){var f=c.topic||"*";a.each(d,function(d){var f=d.channel||b;e.push(m.subscribe({channel:c.channel||b,topic:c.topic||"*",callback:function(b,c){var e=c;e.topic=a.isFunction(d.topic)?d.topic(c.topic):d.topic||c.topic,e.channel=f,m.publish(e,b)}}))})}),e}};return m}) |