diff --git a/build.json b/build.json index 39e605b..2f61b51 100644 --- a/build.json +++ b/build.json @@ -11,7 +11,7 @@ "output": { "full": "lib", "partial": { - "lib/postal.*" : [ "./example/standard/js", "./example/amd/js/libs/postal" ] + "**/lib/postal.*" : [ "example/standard/js", "example/amd/js/libs/postal" ] } }, "dependencies": [ "anvil.http", "anvil.uglify", "anvil.jshint" ] diff --git a/example/amd/js/libs/postal/postal.js b/example/amd/js/libs/postal/postal.js index 84c2659..99c82dc 100644 --- a/example/amd/js/libs/postal/postal.js +++ b/example/amd/js/libs/postal/postal.js @@ -2,27 +2,29 @@ postal Author: Jim Cowart (http://freshbrewedcode.com/jimcowart) License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license) - Version 0.7.3 + Version 0.8.0 */ (function ( root, factory ) { - if ( typeof define === "function" && define.amd ) { + if ( typeof module === "object" && module.exports ) { + // Node, or CommonJS-Like environments + module.exports = function ( _ ) { + _ = _ || require( "underscore" ); + return factory( _ ); + } + } else if ( typeof define === "function" && define.amd ) { // AMD. Register as an anonymous module. define( ["underscore"], function ( _ ) { return factory( _, root ); } ); } else { // Browser globals - factory( root._, root ); + root.postal = factory( root._, root ); } }( this, function ( _, global, undefined ) { var DEFAULT_CHANNEL = "/", - DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, - SYSTEM_CHANNEL = "postal", - NO_OP = function () { - }; - + SYSTEM_CHANNEL = "postal"; var ConsecutiveDistinctPredicate = function () { var previous; return function ( data ) { @@ -39,62 +41,54 @@ }; }; var DistinctPredicate = function () { - var previous = []; + var previous = []; - return function (data) { - var isDistinct = !_.any(previous, function (p) { - if (_.isObject(data) || _.isArray(data)) { - return _.isEqual(data, p); - } - return data === p; - }); - if (isDistinct) { - previous.push(data); - } - return isDistinct; - }; + return function ( data ) { + var isDistinct = !_.any( previous, function ( p ) { + if ( _.isObject( data ) || _.isArray( data ) ) { + return _.isEqual( data, p ); + } + return data === p; + } ); + if ( isDistinct ) { + previous.push( data ); + } + return isDistinct; + }; }; var ChannelDefinition = function ( channelName ) { this.channel = channelName || DEFAULT_CHANNEL; }; - ChannelDefinition.prototype = { - subscribe : function () { - return arguments.length === 1 ? - new SubscriptionDefinition( this.channel, arguments[0].topic, arguments[0].callback ) : - new SubscriptionDefinition( this.channel, arguments[0], arguments[1] ); - }, - - publish : function () { - var envelope = arguments.length === 1 ? arguments[0] : { topic: arguments[0], data: arguments[1] }; - envelope.channel = this.channel; - postal.configuration.bus.publish( envelope ); - return envelope; - } + ChannelDefinition.prototype.subscribe = function () { + return arguments.length === 1 ? + new SubscriptionDefinition( this.channel, arguments[0].topic, arguments[0].callback ) : + new SubscriptionDefinition( this.channel, arguments[0], arguments[1] ); }; + ChannelDefinition.prototype.publish = function () { + var envelope = arguments.length === 1 ? + (Object.prototype.toString.call(arguments[0]) === '[object String]' ? + arguments[0] : { topic: arguments[0] }) : { topic : arguments[0], data : arguments[1] }; + envelope.channel = this.channel; + return postal.configuration.bus.publish( envelope ); + }; var SubscriptionDefinition = function ( channel, topic, callback ) { this.channel = channel; this.topic = topic; this.callback = callback; - this.priority = DEFAULT_PRIORITY; - this.constraints = new Array( 0 ); - this.maxCalls = DEFAULT_DISPOSEAFTER; - this.onHandled = NO_OP; + this.constraints = []; this.context = null; postal.configuration.bus.publish( { channel : SYSTEM_CHANNEL, topic : "subscription.created", - timeStamp : new Date(), data : { event : "subscription.created", channel : channel, topic : topic } } ); - postal.configuration.bus.subscribe( this ); - }; SubscriptionDefinition.prototype = { @@ -103,7 +97,6 @@ postal.configuration.bus.publish( { channel : SYSTEM_CHANNEL, topic : "subscription.removed", - timeStamp : new Date(), data : { event : "subscription.removed", channel : this.channel, @@ -124,20 +117,19 @@ if ( _.isNaN( maxCalls ) || maxCalls <= 0 ) { throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; } - - var fn = this.onHandled; + var fn = this.callback; var dispose = _.after( maxCalls, _.bind( function () { - this.unsubscribe( this ); + this.unsubscribe(); }, this ) ); - this.onHandled = function () { + this.callback = function () { fn.apply( this.context, arguments ); dispose(); }; return this; }, - distinctUntilChanged : function () { + distinctUntilChanged : function () { this.withConstraint( new ConsecutiveDistinctPredicate() ); return this; }, @@ -147,9 +139,9 @@ return this; }, - once: function() { - this.disposeAfter(1); - }, + once : function () { + this.disposeAfter( 1 ); + }, withConstraint : function ( predicate ) { if ( !_.isFunction( predicate ) ) { @@ -196,15 +188,6 @@ return this; }, - withPriority : function ( priority ) { - if ( _.isNaN( priority ) ) { - throw "Priority must be a number"; - } - this.priority = priority; - postal.configuration.bus.changePriority( this ); - return this; - }, - withThrottle : function ( milliseconds ) { if ( _.isNaN( milliseconds ) ) { throw "Milliseconds must be a number"; @@ -219,7 +202,6 @@ return this; } }; - var bindingsResolver = { cache : { }, @@ -228,10 +210,10 @@ return true; } var pattern = ("^" + binding.replace( /\./g, "\\." ) // escape actual periods - .replace( /\*/g, "[A-Z,a-z,0-9]*" ) // asterisks match any alpha-numeric 'word' - .replace( /#/g, ".*" ) + "$") // hash matches 'n' # of words (+ optional on start/end of topic) - .replace( "\\..*$", "(\\..*)*$" ) // fix end of topic matching on hash wildcards - .replace( "^.*\\.", "^(.*\\.)*" ); // fix beginning of topic matching on hash wildcards + .replace( /\*/g, "[A-Z,a-z,0-9]*" ) // asterisks match any alpha-numeric 'word' + .replace( /#/g, ".*" ) + "$") // hash matches 'n' # of words (+ optional on start/end of topic) + .replace( "\\..*$", "(\\..*)*$" ) // fix end of topic matching on hash wildcards + .replace( "^.*\\.", "^(.*\\.)*" ); // fix beginning of topic matching on hash wildcards var rgx = new RegExp( pattern ); var result = rgx.test( topic ); if ( result ) { @@ -247,9 +229,7 @@ this.cache = {}; } }; - var localBus = { - addWireTap : function ( callback ) { var self = this; self.wireTaps.push( callback ); @@ -261,48 +241,28 @@ }; }, - changePriority : function ( subDef ) { - var idx, found; - if ( this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic] ) { - this.subscriptions[subDef.channel][subDef.topic] = _.without( this.subscriptions[subDef.channel][subDef.topic], subDef ); - idx = this.subscriptions[subDef.channel][subDef.topic].length - 1; - for ( ; idx >= 0; idx-- ) { - if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) { - this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef ); - found = true; - break; - } - } - if ( !found ) { - this.subscriptions[subDef.channel][subDef.topic].unshift( subDef ); - } - } - }, - publish : function ( envelope ) { - envelope.timeStamp = new Date(); + envelope.timeStamp = new Date(); _.each( this.wireTaps, function ( tap ) { tap( envelope.data, envelope ); } ); - if ( this.subscriptions[envelope.channel] ) { _.each( this.subscriptions[envelope.channel], function ( topic ) { // TODO: research faster ways to handle this than _.clone _.each( _.clone( topic ), function ( subDef ) { if ( postal.configuration.resolver.compare( subDef.topic, envelope.topic ) ) { if ( _.all( subDef.constraints, function ( constraint ) { - return constraint.call(subDef.context, envelope.data, envelope ); + return constraint.call( subDef.context, envelope.data, envelope ); } ) ) { if ( typeof subDef.callback === 'function' ) { subDef.callback.call( subDef.context, envelope.data, envelope ); - subDef.onHandled(); } } } } ); } ); } - + return envelope; }, reset : function () { @@ -320,13 +280,12 @@ subscribe : function ( subDef ) { var idx, found, fn, channel = this.subscriptions[subDef.channel], subs; - if ( !channel ) { channel = this.subscriptions[subDef.channel] = {}; } subs = this.subscriptions[subDef.channel][subDef.topic]; if ( !subs ) { - subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 ); + subs = this.subscriptions[subDef.channel][subDef.topic] = []; } subs.push( subDef ); return subDef; @@ -334,7 +293,7 @@ subscriptions : {}, - wireTaps : new Array( 0 ), + wireTaps : [], unsubscribe : function ( config ) { if ( this.subscriptions[config.channel][config.topic] ) { @@ -349,28 +308,20 @@ } } }; - - var sessionInfo = {}; - - // save some setup time, albeit tiny localBus.subscriptions[SYSTEM_CHANNEL] = {}; - var postal = { configuration : { bus : localBus, resolver : bindingsResolver, DEFAULT_CHANNEL : DEFAULT_CHANNEL, - DEFAULT_PRIORITY : DEFAULT_PRIORITY, - DEFAULT_DISPOSEAFTER : DEFAULT_DISPOSEAFTER, SYSTEM_CHANNEL : SYSTEM_CHANNEL }, - ChannelDefinition : ChannelDefinition, - - SubscriptionDefinition: SubscriptionDefinition, + ChannelDefinition : ChannelDefinition, + SubscriptionDefinition : SubscriptionDefinition, channel : function ( channelName ) { - return new ChannelDefinition( channelName ); + return new ChannelDefinition( channelName ); }, subscribe : function ( options ) { @@ -378,9 +329,8 @@ }, publish : function ( envelope ) { - envelope.channel = envelope.channel || DEFAULT_CHANNEL; - postal.configuration.bus.publish( envelope ); - return envelope; + envelope.channel = envelope.channel || DEFAULT_CHANNEL; + return postal.configuration.bus.publish( envelope ); }, addWireTap : function ( callback ) { @@ -388,13 +338,9 @@ }, linkChannels : function ( sources, destinations ) { - var result = []; - if ( !_.isArray( sources ) ) { - sources = [sources]; - } - if ( !_.isArray( destinations ) ) { - destinations = [destinations]; - } + var result = []; + sources = !_.isArray( sources ) ? [sources] : sources; + destinations = !_.isArray( destinations ) ? [destinations] : destinations; _.each( sources, function ( source ) { var sourceTopic = source.topic || "#"; _.each( destinations, function ( destination ) { @@ -420,23 +366,16 @@ utils : { getSubscribersFor : function () { var channel = arguments[ 0 ], - tpc = arguments[ 1 ], - result = []; + tpc = arguments[ 1 ]; if ( arguments.length === 1 ) { - if ( Object.prototype.toString.call( channel ) === "[object String]" ) { - channel = postal.configuration.DEFAULT_CHANNEL; - tpc = arguments[ 0 ]; - } - else { - channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL; - tpc = arguments[ 0 ].topic; - } + channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL; + tpc = arguments[ 0 ].topic; } if ( postal.configuration.bus.subscriptions[ channel ] && - postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) { - result = postal.configuration.bus.subscriptions[ channel ][ tpc ]; + postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) { + return postal.configuration.bus.subscriptions[ channel ][ tpc ]; } - return result; + return []; }, reset : function () { @@ -446,6 +385,5 @@ } }; - global.postal = postal; return postal; } )); \ No newline at end of file diff --git a/example/amd/js/libs/postal/postal.min.js b/example/amd/js/libs/postal/postal.min.js deleted file mode 100644 index d54f028..0000000 --- a/example/amd/js/libs/postal/postal.min.js +++ /dev/null @@ -1,7 +0,0 @@ -/* - postal - Author: Jim Cowart (http://freshbrewedcode.com/jimcowart) - License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license) - Version 0.7.3 - */ -(function(e,t){typeof define=="function"&&define.amd?define(["underscore"],function(n){return t(n,e)}):t(e._,e)})(this,function(e,t,n){var r="/",i=50,s=0,o="postal",u=function(){},a=function(){var t;return function(n){var r=!1;return e.isString(n)?(r=n===t,t=n):(r=e.isEqual(n,t),t=e.clone(n)),!r}},f=function(){var t=[];return function(n){var r=!e.any(t,function(t){return e.isObject(n)||e.isArray(n)?e.isEqual(n,t):n===t});return r&&t.push(n),r}},l=function(e){this.channel=e||r};l.prototype={subscribe:function(){return arguments.length===1?new c(this.channel,arguments[0].topic,arguments[0].callback):new c(this.channel,arguments[0],arguments[1])},publish:function(){var e=arguments.length===1?arguments[0]:{topic:arguments[0],data:arguments[1]};return e.channel=this.channel,v.configuration.bus.publish(e),e}};var c=function(e,t,n){this.channel=e,this.topic=t,this.callback=n,this.priority=i,this.constraints=new Array(0),this.maxCalls=s,this.onHandled=u,this.context=null,v.configuration.bus.publish({channel:o,topic:"subscription.created",timeStamp:new Date,data:{event:"subscription.created",channel:e,topic:t}}),v.configuration.bus.subscribe(this)};c.prototype={unsubscribe:function(){v.configuration.bus.unsubscribe(this),v.configuration.bus.publish({channel:o,topic:"subscription.removed",timeStamp:new Date,data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var e=this.callback;return this.callback=function(t){setTimeout(e,0,t)},this},disposeAfter:function(t){if(e.isNaN(t)||t<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var n=this.onHandled,r=e.after(t,e.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){n.apply(this.context,arguments),r()},this},distinctUntilChanged:function(){return this.withConstraint(new a),this},distinct:function(){return this.withConstraint(new f),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!e.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var n=this;return e.isArray(t)&&e.each(t,function(e){n.withConstraint(e)}),n},withContext:function(e){return this.context=e,this},withDebounce:function(t){if(e.isNaN(t))throw"Milliseconds must be a number";var n=this.callback;return this.callback=e.debounce(n,t),this},withDelay:function(t){if(e.isNaN(t))throw"Milliseconds must be a number";var n=this.callback;return this.callback=function(e){setTimeout(function(){n(e)},t)},this},withPriority:function(t){if(e.isNaN(t))throw"Priority must be a number";return this.priority=t,v.configuration.bus.changePriority(this),this},withThrottle:function(t){if(e.isNaN(t))throw"Milliseconds must be a number";var n=this.callback;return this.callback=e.throttle(n,t),this},subscribe:function(e){return this.callback=e,this}};var h={cache:{},compare:function(e,t){if(this.cache[t]&&this.cache[t][e])return!0;var n=("^"+e.replace(/\./g,"\\.").replace(/\*/g,"[A-Z,a-z,0-9]*").replace(/#/g,".*")+"$").replace("\\..*$","(\\..*)*$").replace("^.*\\.","^(.*\\.)*"),r=new RegExp(n),i=r.test(t);return i&&(this.cache[t]||(this.cache[t]={}),this.cache[t][e]=!0),i},reset:function(){this.cache={}}},p={addWireTap:function(e){var t=this;return t.wireTaps.push(e),function(){var n=t.wireTaps.indexOf(e);n!==-1&&t.wireTaps.splice(n,1)}},changePriority:function(t){var n,r;if(this.subscriptions[t.channel]&&this.subscriptions[t.channel][t.topic]){this.subscriptions[t.channel][t.topic]=e.without(this.subscriptions[t.channel][t.topic],t),n=this.subscriptions[t.channel][t.topic].length-1;for(;n>=0;n--)if(this.subscriptions[t.channel][t.topic][n].priority<=t.priority){this.subscriptions[t.channel][t.topic].splice(n+1,0,t),r=!0;break}r||this.subscriptions[t.channel][t.topic].unshift(t)}},publish:function(t){t.timeStamp=new Date,e.each(this.wireTaps,function(e){e(t.data,t)}),this.subscriptions[t.channel]&&e.each(this.subscriptions[t.channel],function(n){e.each(e.clone(n),function(n){v.configuration.resolver.compare(n.topic,t.topic)&&e.all(n.constraints,function(e){return e.call(n.context,t.data,t)})&&typeof n.callback=="function"&&(n.callback.call(n.context,t.data,t),n.onHandled())})})},reset:function(){this.subscriptions&&(e.each(this.subscriptions,function(t){e.each(t,function(e){while(e.length)e.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(e){var t,n,r,i=this.subscriptions[e.channel],s;return i||(i=this.subscriptions[e.channel]={}),s=this.subscriptions[e.channel][e.topic],s||(s=this.subscriptions[e.channel][e.topic]=new Array(0)),s.push(e),e},subscriptions:{},wireTaps:new Array(0),unsubscribe:function(e){if(this.subscriptions[e.channel][e.topic]){var t=this.subscriptions[e.channel][e.topic].length,n=0;for(;n= 0; idx-- ) { - if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) { - this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef ); - found = true; - break; - } - } - if ( !found ) { - this.subscriptions[subDef.channel][subDef.topic].unshift( subDef ); - } - } - }, - publish : function ( envelope ) { - envelope.timeStamp = new Date(); + envelope.timeStamp = new Date(); _.each( this.wireTaps, function ( tap ) { tap( envelope.data, envelope ); } ); - if ( this.subscriptions[envelope.channel] ) { _.each( this.subscriptions[envelope.channel], function ( topic ) { // TODO: research faster ways to handle this than _.clone _.each( _.clone( topic ), function ( subDef ) { if ( postal.configuration.resolver.compare( subDef.topic, envelope.topic ) ) { if ( _.all( subDef.constraints, function ( constraint ) { - return constraint.call(subDef.context, envelope.data, envelope ); + return constraint.call( subDef.context, envelope.data, envelope ); } ) ) { if ( typeof subDef.callback === 'function' ) { subDef.callback.call( subDef.context, envelope.data, envelope ); - subDef.onHandled(); } } } } ); } ); } - + return envelope; }, reset : function () { @@ -320,13 +280,12 @@ subscribe : function ( subDef ) { var idx, found, fn, channel = this.subscriptions[subDef.channel], subs; - if ( !channel ) { channel = this.subscriptions[subDef.channel] = {}; } subs = this.subscriptions[subDef.channel][subDef.topic]; if ( !subs ) { - subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 ); + subs = this.subscriptions[subDef.channel][subDef.topic] = []; } subs.push( subDef ); return subDef; @@ -334,7 +293,7 @@ subscriptions : {}, - wireTaps : new Array( 0 ), + wireTaps : [], unsubscribe : function ( config ) { if ( this.subscriptions[config.channel][config.topic] ) { @@ -349,28 +308,20 @@ } } }; - - var sessionInfo = {}; - - // save some setup time, albeit tiny localBus.subscriptions[SYSTEM_CHANNEL] = {}; - var postal = { configuration : { bus : localBus, resolver : bindingsResolver, DEFAULT_CHANNEL : DEFAULT_CHANNEL, - DEFAULT_PRIORITY : DEFAULT_PRIORITY, - DEFAULT_DISPOSEAFTER : DEFAULT_DISPOSEAFTER, SYSTEM_CHANNEL : SYSTEM_CHANNEL }, - ChannelDefinition : ChannelDefinition, - - SubscriptionDefinition: SubscriptionDefinition, + ChannelDefinition : ChannelDefinition, + SubscriptionDefinition : SubscriptionDefinition, channel : function ( channelName ) { - return new ChannelDefinition( channelName ); + return new ChannelDefinition( channelName ); }, subscribe : function ( options ) { @@ -378,9 +329,8 @@ }, publish : function ( envelope ) { - envelope.channel = envelope.channel || DEFAULT_CHANNEL; - postal.configuration.bus.publish( envelope ); - return envelope; + envelope.channel = envelope.channel || DEFAULT_CHANNEL; + return postal.configuration.bus.publish( envelope ); }, addWireTap : function ( callback ) { @@ -388,13 +338,9 @@ }, linkChannels : function ( sources, destinations ) { - var result = []; - if ( !_.isArray( sources ) ) { - sources = [sources]; - } - if ( !_.isArray( destinations ) ) { - destinations = [destinations]; - } + var result = []; + sources = !_.isArray( sources ) ? [sources] : sources; + destinations = !_.isArray( destinations ) ? [destinations] : destinations; _.each( sources, function ( source ) { var sourceTopic = source.topic || "#"; _.each( destinations, function ( destination ) { @@ -420,23 +366,16 @@ utils : { getSubscribersFor : function () { var channel = arguments[ 0 ], - tpc = arguments[ 1 ], - result = []; + tpc = arguments[ 1 ]; if ( arguments.length === 1 ) { - if ( Object.prototype.toString.call( channel ) === "[object String]" ) { - channel = postal.configuration.DEFAULT_CHANNEL; - tpc = arguments[ 0 ]; - } - else { - channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL; - tpc = arguments[ 0 ].topic; - } + channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL; + tpc = arguments[ 0 ].topic; } if ( postal.configuration.bus.subscriptions[ channel ] && - postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) { - result = postal.configuration.bus.subscriptions[ channel ][ tpc ]; + postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) { + return postal.configuration.bus.subscriptions[ channel ][ tpc ]; } - return result; + return []; }, reset : function () { @@ -446,6 +385,5 @@ } }; - global.postal = postal; return postal; } )); \ No newline at end of file diff --git a/example/standard/js/postal.min.js b/example/standard/js/postal.min.js deleted file mode 100644 index d54f028..0000000 --- a/example/standard/js/postal.min.js +++ /dev/null @@ -1,7 +0,0 @@ -/* - postal - Author: Jim Cowart (http://freshbrewedcode.com/jimcowart) - License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license) - Version 0.7.3 - */ -(function(e,t){typeof define=="function"&&define.amd?define(["underscore"],function(n){return t(n,e)}):t(e._,e)})(this,function(e,t,n){var r="/",i=50,s=0,o="postal",u=function(){},a=function(){var t;return function(n){var r=!1;return e.isString(n)?(r=n===t,t=n):(r=e.isEqual(n,t),t=e.clone(n)),!r}},f=function(){var t=[];return function(n){var r=!e.any(t,function(t){return e.isObject(n)||e.isArray(n)?e.isEqual(n,t):n===t});return r&&t.push(n),r}},l=function(e){this.channel=e||r};l.prototype={subscribe:function(){return arguments.length===1?new c(this.channel,arguments[0].topic,arguments[0].callback):new c(this.channel,arguments[0],arguments[1])},publish:function(){var e=arguments.length===1?arguments[0]:{topic:arguments[0],data:arguments[1]};return e.channel=this.channel,v.configuration.bus.publish(e),e}};var c=function(e,t,n){this.channel=e,this.topic=t,this.callback=n,this.priority=i,this.constraints=new Array(0),this.maxCalls=s,this.onHandled=u,this.context=null,v.configuration.bus.publish({channel:o,topic:"subscription.created",timeStamp:new Date,data:{event:"subscription.created",channel:e,topic:t}}),v.configuration.bus.subscribe(this)};c.prototype={unsubscribe:function(){v.configuration.bus.unsubscribe(this),v.configuration.bus.publish({channel:o,topic:"subscription.removed",timeStamp:new Date,data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var e=this.callback;return this.callback=function(t){setTimeout(e,0,t)},this},disposeAfter:function(t){if(e.isNaN(t)||t<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var n=this.onHandled,r=e.after(t,e.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){n.apply(this.context,arguments),r()},this},distinctUntilChanged:function(){return this.withConstraint(new a),this},distinct:function(){return this.withConstraint(new f),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!e.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var n=this;return e.isArray(t)&&e.each(t,function(e){n.withConstraint(e)}),n},withContext:function(e){return this.context=e,this},withDebounce:function(t){if(e.isNaN(t))throw"Milliseconds must be a number";var n=this.callback;return this.callback=e.debounce(n,t),this},withDelay:function(t){if(e.isNaN(t))throw"Milliseconds must be a number";var n=this.callback;return this.callback=function(e){setTimeout(function(){n(e)},t)},this},withPriority:function(t){if(e.isNaN(t))throw"Priority must be a number";return this.priority=t,v.configuration.bus.changePriority(this),this},withThrottle:function(t){if(e.isNaN(t))throw"Milliseconds must be a number";var n=this.callback;return this.callback=e.throttle(n,t),this},subscribe:function(e){return this.callback=e,this}};var h={cache:{},compare:function(e,t){if(this.cache[t]&&this.cache[t][e])return!0;var n=("^"+e.replace(/\./g,"\\.").replace(/\*/g,"[A-Z,a-z,0-9]*").replace(/#/g,".*")+"$").replace("\\..*$","(\\..*)*$").replace("^.*\\.","^(.*\\.)*"),r=new RegExp(n),i=r.test(t);return i&&(this.cache[t]||(this.cache[t]={}),this.cache[t][e]=!0),i},reset:function(){this.cache={}}},p={addWireTap:function(e){var t=this;return t.wireTaps.push(e),function(){var n=t.wireTaps.indexOf(e);n!==-1&&t.wireTaps.splice(n,1)}},changePriority:function(t){var n,r;if(this.subscriptions[t.channel]&&this.subscriptions[t.channel][t.topic]){this.subscriptions[t.channel][t.topic]=e.without(this.subscriptions[t.channel][t.topic],t),n=this.subscriptions[t.channel][t.topic].length-1;for(;n>=0;n--)if(this.subscriptions[t.channel][t.topic][n].priority<=t.priority){this.subscriptions[t.channel][t.topic].splice(n+1,0,t),r=!0;break}r||this.subscriptions[t.channel][t.topic].unshift(t)}},publish:function(t){t.timeStamp=new Date,e.each(this.wireTaps,function(e){e(t.data,t)}),this.subscriptions[t.channel]&&e.each(this.subscriptions[t.channel],function(n){e.each(e.clone(n),function(n){v.configuration.resolver.compare(n.topic,t.topic)&&e.all(n.constraints,function(e){return e.call(n.context,t.data,t)})&&typeof n.callback=="function"&&(n.callback.call(n.context,t.data,t),n.onHandled())})})},reset:function(){this.subscriptions&&(e.each(this.subscriptions,function(t){e.each(t,function(e){while(e.length)e.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(e){var t,n,r,i=this.subscriptions[e.channel],s;return i||(i=this.subscriptions[e.channel]={}),s=this.subscriptions[e.channel][e.topic],s||(s=this.subscriptions[e.channel][e.topic]=new Array(0)),s.push(e),e},subscriptions:{},wireTaps:new Array(0),unsubscribe:function(e){if(this.subscriptions[e.channel][e.topic]){var t=this.subscriptions[e.channel][e.topic].length,n=0;for(;n