diff --git a/README.md b/README.md index f489421..fb9de7e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Postal.js -## Version 0.8.0 (Dual Licensed [MIT](http://www.opensource.org/licenses/mit-license) & [GPL](http://www.opensource.org/licenses/gpl-license)) +## Version 0.8.1 (Dual Licensed [MIT](http://www.opensource.org/licenses/mit-license) & [GPL](http://www.opensource.org/licenses/gpl-license)) ## What is it? Postal.js is an in-memory message bus - very loosely inspired by [AMQP](http://www.amqp.org/) - written in JavaScript. Postal.js runs in the browser, or on the server-side using Node.js. It takes the familiar "eventing-style" paradigm (of which most JavaScript developers are familiar) and extends it by providing "broker" and subscriber implementations which are more sophisticated than what you typically find in simple event delegation. diff --git a/component.json b/component.json index 85fe342..e826f26 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "postal.js", - "version": "0.8.0", + "version": "0.8.1", "main": ["lib/postal.min.js", "lib/postal.js"], "dependencies": { "underscore": "~1.3.0" diff --git a/lib/postal.js b/lib/postal.js index 10cde69..142ac9b 100644 --- a/lib/postal.js +++ b/lib/postal.js @@ -2,7 +2,7 @@ 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.8.0 + Version 0.8.1 */ (function ( root, factory ) { if ( typeof module === "object" && module.exports ) { @@ -229,6 +229,18 @@ this.cache = {}; } }; + var fireSub = function(subDef, envelope) { + if ( postal.configuration.resolver.compare( subDef.topic, envelope.topic ) ) { + if ( _.all( subDef.constraints, function ( constraint ) { + return constraint.call( subDef.context, envelope.data, envelope ); + } ) ) { + if ( typeof subDef.callback === 'function' ) { + subDef.callback.call( subDef.context, envelope.data, envelope ); + } + } + } + }; + var localBus = { addWireTap : function ( callback ) { var self = this; @@ -247,23 +259,14 @@ tap( envelope.data, envelope ); } ); if ( this.subscriptions[envelope.channel] ) { - _.each( this.subscriptions[envelope.channel], function ( subscribers ) { - // TODO: research faster ways to handle this than _.clone - var idx = 0; - while(idx < subscribers.length) { - (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 ); - } ) ) { - if ( typeof subDef.callback === 'function' ) { - subDef.callback.call( subDef.context, envelope.data, envelope ); - } - } - } - }(subscribers[idx++])); + _.each( this.subscriptions[envelope.channel], function ( subscribers ) { + var idx = 0, len = subscribers.length, subDef; + while(idx < len) { + if( subDef = subscribers[idx++] ){ + fireSub(subDef, envelope); + } } - } ); + } ); } return envelope; }, diff --git a/lib/postal.min.js b/lib/postal.min.js index fd2e23e..bc973cd 100644 --- a/lib/postal.min.js +++ b/lib/postal.min.js @@ -2,6 +2,6 @@ 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.8.0 + Version 0.8.1 */ -(function(e,t){typeof module=="object"&&module.exports?module.exports=function(e){return e=e||require("underscore"),t(e)}:typeof define=="function"&&define.amd?define(["underscore"],function(n){return t(n,e)}):e.postal=t(e._,e)})(this,function(e,t,n){var r="/",i=0,s="postal",o=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}},u=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}},a=function(e){this.channel=e||r};a.prototype.subscribe=function(){return arguments.length===1?new f(this.channel,arguments[0].topic,arguments[0].callback):new f(this.channel,arguments[0],arguments[1])},a.prototype.publish=function(){var e=arguments.length===1?Object.prototype.toString.call(arguments[0])==="[object String]"?arguments[0]:{topic:arguments[0]}:{topic:arguments[0],data:arguments[1]};return e.channel=this.channel,h.configuration.bus.publish(e)};var f=function(e,t,n){this.channel=e,this.topic=t,this.callback=n,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:s,topic:"subscription.created",data:{event:"subscription.created",channel:e,topic:t}}),h.configuration.bus.subscribe(this)};f.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:s,topic:"subscription.removed",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.callback,r=e.after(t,e.bind(function(){this.unsubscribe()},this));return this.callback=function(){n.apply(this.context,arguments),r()},this},distinctUntilChanged:function(){return this.withConstraint(new o),this},distinct:function(){return this.withConstraint(new u),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},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 l={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={}}},c={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)}},publish:function(t){return 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){var r=0;while(r