From f46f153982a5558ac1b32c861d9e5059f1c17d1d Mon Sep 17 00:00:00 2001 From: Doug Neiner Date: Tue, 30 Apr 2013 21:26:48 -0500 Subject: [PATCH] Made the resolver more permissive. Fixed issue where topics starting with a / would never resolve. --- README.md | 2 +- component.json | 2 +- example/amd/js/libs/postal/postal.js | 19 ++++++++++++------- example/amd/js/libs/postal/postal.min.js | 4 ++-- example/standard/js/postal.js | 19 ++++++++++++------- example/standard/js/postal.min.js | 4 ++-- lib/postal.js | 15 +++++++++------ lib/postal.min.js | 4 ++-- package.json | 2 +- src/AmqpBindingsResolver.js | 13 ++++++++----- 10 files changed, 50 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 373ce9a..ec0747d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Postal.js -## Version 0.8.3 (Dual Licensed [MIT](http://www.opensource.org/licenses/mit-license) & [GPL](http://www.opensource.org/licenses/gpl-license)) +## Version 0.8.4 (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 de498d0..fe3a3bc 100755 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "postal.js", - "version": "0.8.3", + "version": "0.8.4", "main": ["lib/postal.min.js", "lib/postal.js"], "dependencies": { "underscore": "~1.3.0" diff --git a/example/amd/js/libs/postal/postal.js b/example/amd/js/libs/postal/postal.js index fa7eaf1..c7f0224 100755 --- a/example/amd/js/libs/postal/postal.js +++ b/example/amd/js/libs/postal/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.3 + Version 0.8.4 */ (function ( root, factory ) { if ( typeof module === "object" && module.exports ) { @@ -108,7 +108,9 @@ defer : function () { var fn = this.callback; this.callback = function ( data ) { - setTimeout( fn, 0, data ); + setTimeout( function () { + fn( data ); + }, 0 ); }; return this; }, @@ -207,21 +209,24 @@ regex : {}, compare : function ( binding, topic ) { - var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]); + var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]); if(typeof result !== "undefined") { return result; } if(!(rgx = this.regex[binding])) { pattern = "^" + _.map(binding.split('.'), function(segment) { - var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b"; + var res = ""; + if (!!prevSegment) { + res = prevSegment !== "#" ? "\\.\\b" : "\\b"; + } if(segment === "#") { - res += "[A-Z,a-z,0-9,\\.]*" + res += "[\\s\\S]*" } else if (segment === "*") { - res += "[A-Z,a-z,0-9]+" + res += "[^.]+" } else { res += segment; } - prev = segment; + prevSegment = segment; return res; } ).join('') + "$"; rgx = this.regex[binding] = new RegExp( pattern ); diff --git a/example/amd/js/libs/postal/postal.min.js b/example/amd/js/libs/postal/postal.min.js index 747ced6..3db2218 100755 --- a/example/amd/js/libs/postal/postal.min.js +++ b/example/amd/js/libs/postal/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.3 + Version 0.8.4 */ -(function(t,n){"object"==typeof module&&module.exports?module.exports=function(t){return t=t||require("underscore"),n(t)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return n(i,t)}):t.postal=n(t._,t)})(this,function(t){var n="/",i="postal",s=function(){var n;return function(i){var s=!1;return t.isString(i)?(s=i===n,n=i):(s=t.isEqual(i,n),n=t.clone(i)),!s}},c=function(){var n=[];return function(i){var s=!t.any(n,function(n){return t.isObject(i)||t.isArray(i)?t.isEqual(i,n):i===n});return s&&n.push(i),s}},e=function(t){this.channel=t||n};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var t=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return t.channel=this.channel,h.configuration.bus.publish(t)};var r=function(t,n,s){this.channel=t,this.topic=n,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:t,topic:n}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var t=this.callback;return this.callback=function(n){setTimeout(t,0,n)},this},disposeAfter:function(n){if(t.isNaN(n)||0>=n)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=t.after(n,t.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(n){if(!t.isFunction(n))throw"Predicate constraint must be a function";return this.constraints.push(n),this},withConstraints:function(n){var i=this;return t.isArray(n)&&t.each(n,function(t){i.withConstraint(t)}),i},withContext:function(t){return this.context=t,this},withDebounce:function(n){if(t.isNaN(n))throw"Milliseconds must be a number";var i=this.callback;return this.callback=t.debounce(i,n),this},withDelay:function(n){if(t.isNaN(n))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(t){setTimeout(function(){i(t)},n)},this},withThrottle:function(n){if(t.isNaN(n))throw"Milliseconds must be a number";var i=this.callback;return this.callback=t.throttle(i,n),this},subscribe:function(t){return this.callback=t,this}};var o={cache:{},regex:{},compare:function(n,i){var s,c,e,r=this.cache[i]&&this.cache[i][n];return r!==undefined?r:((c=this.regex[n])||(s="^"+t.map(n.split("."),function(t){var n=e&&"#"!==e?"\\.\\b":"\\b";return n+="#"===t?"[A-Z,a-z,0-9,\\.]*":"*"===t?"[A-Z,a-z,0-9]+":t,e=t,n}).join("")+"$",c=this.regex[n]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][n]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(n,i){h.configuration.resolver.compare(n.topic,i.topic)&&t.all(n.constraints,function(t){return t.call(n.context,i.data,i)})&&"function"==typeof n.callback&&n.callback.call(n.context,i.data,i)},a={addWireTap:function(t){var n=this;return n.wireTaps.push(t),function(){var i=n.wireTaps.indexOf(t);-1!==i&&n.wireTaps.splice(i,1)}},publish:function(n){return n.timeStamp=new Date,t.each(this.wireTaps,function(t){t(n.data,n)}),this.subscriptions[n.channel]&&t.each(this.subscriptions[n.channel],function(t){for(var i,s=0,c=t.length;c>s;)(i=t[s++])&&u(i,n)}),n},reset:function(){this.subscriptions&&(t.each(this.subscriptions,function(n){t.each(n,function(t){for(;t.length;)t.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(t){var n,i=this.subscriptions[t.channel];return i||(i=this.subscriptions[t.channel]={}),n=this.subscriptions[t.channel][t.topic],n||(n=this.subscriptions[t.channel][t.topic]=[]),n.push(t),t},subscriptions:{},wireTaps:[],unsubscribe:function(t){if(this.subscriptions[t.channel][t.topic])for(var n=this.subscriptions[t.channel][t.topic].length,i=0;n>i;i++)if(this.subscriptions[t.channel][t.topic][i]===t){this.subscriptions[t.channel][t.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:n,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(t){return new e(t)},subscribe:function(t){return new r(t.channel||n,t.topic,t.callback)},publish:function(t){return t.channel=t.channel||n,h.configuration.bus.publish(t)},addWireTap:function(t){return this.configuration.bus.addWireTap(t)},linkChannels:function(i,s){var c=[];return i=t.isArray(i)?i:[i],s=t.isArray(s)?s:[s],t.each(i,function(i){i.topic||"#",t.each(s,function(s){var e=s.channel||n;c.push(h.subscribe({channel:i.channel||n,topic:i.topic||"#",callback:function(n,i){var c=t.clone(i);c.topic=t.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=n,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var t=arguments[0],n=arguments[1];return 1===arguments.length&&(t=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,n=arguments[0].topic),h.configuration.bus.subscriptions[t]&&h.configuration.bus.subscriptions[t].hasOwnProperty(n)?h.configuration.bus.subscriptions[t][n]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h}); \ No newline at end of file +(function(n,t){"object"==typeof module&&module.exports?module.exports=function(n){return n=n||require("underscore"),t(n)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return t(i,n)}):n.postal=t(n._,n)})(this,function(n){var t="/",i="postal",s=function(){var t;return function(i){var s=!1;return n.isString(i)?(s=i===t,t=i):(s=n.isEqual(i,t),t=n.clone(i)),!s}},c=function(){var t=[];return function(i){var s=!n.any(t,function(t){return n.isObject(i)||n.isArray(i)?n.isEqual(i,t):i===t});return s&&t.push(i),s}},e=function(n){this.channel=n||t};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var n=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return n.channel=this.channel,h.configuration.bus.publish(n)};var r=function(n,t,s){this.channel=n,this.topic=t,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:n,topic:t}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var n=this.callback;return this.callback=function(t){setTimeout(function(){n(t)},0)},this},disposeAfter:function(t){if(n.isNaN(t)||0>=t)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=n.after(t,n.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!n.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var i=this;return n.isArray(t)&&n.each(t,function(n){i.withConstraint(n)}),i},withContext:function(n){return this.context=n,this},withDebounce:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.debounce(i,t),this},withDelay:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(n){setTimeout(function(){i(n)},t)},this},withThrottle:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.throttle(i,t),this},subscribe:function(n){return this.callback=n,this}};var o={cache:{},regex:{},compare:function(t,i){var s,c,e,r=this.cache[i]&&this.cache[i][t];return r!==undefined?r:((c=this.regex[t])||(s="^"+n.map(t.split("."),function(n){var t="";return e&&(t="#"!==e?"\\.\\b":"\\b"),t+="#"===n?"[\\s\\S]*":"*"===n?"[^.]+":n,e=n,t}).join("")+"$",c=this.regex[t]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][t]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(t,i){h.configuration.resolver.compare(t.topic,i.topic)&&n.all(t.constraints,function(n){return n.call(t.context,i.data,i)})&&"function"==typeof t.callback&&t.callback.call(t.context,i.data,i)},a={addWireTap:function(n){var t=this;return t.wireTaps.push(n),function(){var i=t.wireTaps.indexOf(n);-1!==i&&t.wireTaps.splice(i,1)}},publish:function(t){return t.timeStamp=new Date,n.each(this.wireTaps,function(n){n(t.data,t)}),this.subscriptions[t.channel]&&n.each(this.subscriptions[t.channel],function(n){for(var i,s=0,c=n.length;c>s;)(i=n[s++])&&u(i,t)}),t},reset:function(){this.subscriptions&&(n.each(this.subscriptions,function(t){n.each(t,function(n){for(;n.length;)n.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(n){var t,i=this.subscriptions[n.channel];return i||(i=this.subscriptions[n.channel]={}),t=this.subscriptions[n.channel][n.topic],t||(t=this.subscriptions[n.channel][n.topic]=[]),t.push(n),n},subscriptions:{},wireTaps:[],unsubscribe:function(n){if(this.subscriptions[n.channel][n.topic])for(var t=this.subscriptions[n.channel][n.topic].length,i=0;t>i;i++)if(this.subscriptions[n.channel][n.topic][i]===n){this.subscriptions[n.channel][n.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:t,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(n){return new e(n)},subscribe:function(n){return new r(n.channel||t,n.topic,n.callback)},publish:function(n){return n.channel=n.channel||t,h.configuration.bus.publish(n)},addWireTap:function(n){return this.configuration.bus.addWireTap(n)},linkChannels:function(i,s){var c=[];return i=n.isArray(i)?i:[i],s=n.isArray(s)?s:[s],n.each(i,function(i){i.topic||"#",n.each(s,function(s){var e=s.channel||t;c.push(h.subscribe({channel:i.channel||t,topic:i.topic||"#",callback:function(t,i){var c=n.clone(i);c.topic=n.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=t,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var n=arguments[0],t=arguments[1];return 1===arguments.length&&(n=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,t=arguments[0].topic),h.configuration.bus.subscriptions[n]&&h.configuration.bus.subscriptions[n].hasOwnProperty(t)?h.configuration.bus.subscriptions[n][t]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h}); \ No newline at end of file diff --git a/example/standard/js/postal.js b/example/standard/js/postal.js index fa7eaf1..c7f0224 100755 --- a/example/standard/js/postal.js +++ b/example/standard/js/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.3 + Version 0.8.4 */ (function ( root, factory ) { if ( typeof module === "object" && module.exports ) { @@ -108,7 +108,9 @@ defer : function () { var fn = this.callback; this.callback = function ( data ) { - setTimeout( fn, 0, data ); + setTimeout( function () { + fn( data ); + }, 0 ); }; return this; }, @@ -207,21 +209,24 @@ regex : {}, compare : function ( binding, topic ) { - var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]); + var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]); if(typeof result !== "undefined") { return result; } if(!(rgx = this.regex[binding])) { pattern = "^" + _.map(binding.split('.'), function(segment) { - var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b"; + var res = ""; + if (!!prevSegment) { + res = prevSegment !== "#" ? "\\.\\b" : "\\b"; + } if(segment === "#") { - res += "[A-Z,a-z,0-9,\\.]*" + res += "[\\s\\S]*" } else if (segment === "*") { - res += "[A-Z,a-z,0-9]+" + res += "[^.]+" } else { res += segment; } - prev = segment; + prevSegment = segment; return res; } ).join('') + "$"; rgx = this.regex[binding] = new RegExp( pattern ); diff --git a/example/standard/js/postal.min.js b/example/standard/js/postal.min.js index 747ced6..3db2218 100755 --- a/example/standard/js/postal.min.js +++ b/example/standard/js/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.3 + Version 0.8.4 */ -(function(t,n){"object"==typeof module&&module.exports?module.exports=function(t){return t=t||require("underscore"),n(t)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return n(i,t)}):t.postal=n(t._,t)})(this,function(t){var n="/",i="postal",s=function(){var n;return function(i){var s=!1;return t.isString(i)?(s=i===n,n=i):(s=t.isEqual(i,n),n=t.clone(i)),!s}},c=function(){var n=[];return function(i){var s=!t.any(n,function(n){return t.isObject(i)||t.isArray(i)?t.isEqual(i,n):i===n});return s&&n.push(i),s}},e=function(t){this.channel=t||n};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var t=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return t.channel=this.channel,h.configuration.bus.publish(t)};var r=function(t,n,s){this.channel=t,this.topic=n,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:t,topic:n}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var t=this.callback;return this.callback=function(n){setTimeout(t,0,n)},this},disposeAfter:function(n){if(t.isNaN(n)||0>=n)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=t.after(n,t.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(n){if(!t.isFunction(n))throw"Predicate constraint must be a function";return this.constraints.push(n),this},withConstraints:function(n){var i=this;return t.isArray(n)&&t.each(n,function(t){i.withConstraint(t)}),i},withContext:function(t){return this.context=t,this},withDebounce:function(n){if(t.isNaN(n))throw"Milliseconds must be a number";var i=this.callback;return this.callback=t.debounce(i,n),this},withDelay:function(n){if(t.isNaN(n))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(t){setTimeout(function(){i(t)},n)},this},withThrottle:function(n){if(t.isNaN(n))throw"Milliseconds must be a number";var i=this.callback;return this.callback=t.throttle(i,n),this},subscribe:function(t){return this.callback=t,this}};var o={cache:{},regex:{},compare:function(n,i){var s,c,e,r=this.cache[i]&&this.cache[i][n];return r!==undefined?r:((c=this.regex[n])||(s="^"+t.map(n.split("."),function(t){var n=e&&"#"!==e?"\\.\\b":"\\b";return n+="#"===t?"[A-Z,a-z,0-9,\\.]*":"*"===t?"[A-Z,a-z,0-9]+":t,e=t,n}).join("")+"$",c=this.regex[n]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][n]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(n,i){h.configuration.resolver.compare(n.topic,i.topic)&&t.all(n.constraints,function(t){return t.call(n.context,i.data,i)})&&"function"==typeof n.callback&&n.callback.call(n.context,i.data,i)},a={addWireTap:function(t){var n=this;return n.wireTaps.push(t),function(){var i=n.wireTaps.indexOf(t);-1!==i&&n.wireTaps.splice(i,1)}},publish:function(n){return n.timeStamp=new Date,t.each(this.wireTaps,function(t){t(n.data,n)}),this.subscriptions[n.channel]&&t.each(this.subscriptions[n.channel],function(t){for(var i,s=0,c=t.length;c>s;)(i=t[s++])&&u(i,n)}),n},reset:function(){this.subscriptions&&(t.each(this.subscriptions,function(n){t.each(n,function(t){for(;t.length;)t.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(t){var n,i=this.subscriptions[t.channel];return i||(i=this.subscriptions[t.channel]={}),n=this.subscriptions[t.channel][t.topic],n||(n=this.subscriptions[t.channel][t.topic]=[]),n.push(t),t},subscriptions:{},wireTaps:[],unsubscribe:function(t){if(this.subscriptions[t.channel][t.topic])for(var n=this.subscriptions[t.channel][t.topic].length,i=0;n>i;i++)if(this.subscriptions[t.channel][t.topic][i]===t){this.subscriptions[t.channel][t.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:n,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(t){return new e(t)},subscribe:function(t){return new r(t.channel||n,t.topic,t.callback)},publish:function(t){return t.channel=t.channel||n,h.configuration.bus.publish(t)},addWireTap:function(t){return this.configuration.bus.addWireTap(t)},linkChannels:function(i,s){var c=[];return i=t.isArray(i)?i:[i],s=t.isArray(s)?s:[s],t.each(i,function(i){i.topic||"#",t.each(s,function(s){var e=s.channel||n;c.push(h.subscribe({channel:i.channel||n,topic:i.topic||"#",callback:function(n,i){var c=t.clone(i);c.topic=t.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=n,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var t=arguments[0],n=arguments[1];return 1===arguments.length&&(t=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,n=arguments[0].topic),h.configuration.bus.subscriptions[t]&&h.configuration.bus.subscriptions[t].hasOwnProperty(n)?h.configuration.bus.subscriptions[t][n]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h}); \ No newline at end of file +(function(n,t){"object"==typeof module&&module.exports?module.exports=function(n){return n=n||require("underscore"),t(n)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return t(i,n)}):n.postal=t(n._,n)})(this,function(n){var t="/",i="postal",s=function(){var t;return function(i){var s=!1;return n.isString(i)?(s=i===t,t=i):(s=n.isEqual(i,t),t=n.clone(i)),!s}},c=function(){var t=[];return function(i){var s=!n.any(t,function(t){return n.isObject(i)||n.isArray(i)?n.isEqual(i,t):i===t});return s&&t.push(i),s}},e=function(n){this.channel=n||t};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var n=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return n.channel=this.channel,h.configuration.bus.publish(n)};var r=function(n,t,s){this.channel=n,this.topic=t,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:n,topic:t}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var n=this.callback;return this.callback=function(t){setTimeout(function(){n(t)},0)},this},disposeAfter:function(t){if(n.isNaN(t)||0>=t)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=n.after(t,n.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!n.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var i=this;return n.isArray(t)&&n.each(t,function(n){i.withConstraint(n)}),i},withContext:function(n){return this.context=n,this},withDebounce:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.debounce(i,t),this},withDelay:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(n){setTimeout(function(){i(n)},t)},this},withThrottle:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.throttle(i,t),this},subscribe:function(n){return this.callback=n,this}};var o={cache:{},regex:{},compare:function(t,i){var s,c,e,r=this.cache[i]&&this.cache[i][t];return r!==undefined?r:((c=this.regex[t])||(s="^"+n.map(t.split("."),function(n){var t="";return e&&(t="#"!==e?"\\.\\b":"\\b"),t+="#"===n?"[\\s\\S]*":"*"===n?"[^.]+":n,e=n,t}).join("")+"$",c=this.regex[t]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][t]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(t,i){h.configuration.resolver.compare(t.topic,i.topic)&&n.all(t.constraints,function(n){return n.call(t.context,i.data,i)})&&"function"==typeof t.callback&&t.callback.call(t.context,i.data,i)},a={addWireTap:function(n){var t=this;return t.wireTaps.push(n),function(){var i=t.wireTaps.indexOf(n);-1!==i&&t.wireTaps.splice(i,1)}},publish:function(t){return t.timeStamp=new Date,n.each(this.wireTaps,function(n){n(t.data,t)}),this.subscriptions[t.channel]&&n.each(this.subscriptions[t.channel],function(n){for(var i,s=0,c=n.length;c>s;)(i=n[s++])&&u(i,t)}),t},reset:function(){this.subscriptions&&(n.each(this.subscriptions,function(t){n.each(t,function(n){for(;n.length;)n.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(n){var t,i=this.subscriptions[n.channel];return i||(i=this.subscriptions[n.channel]={}),t=this.subscriptions[n.channel][n.topic],t||(t=this.subscriptions[n.channel][n.topic]=[]),t.push(n),n},subscriptions:{},wireTaps:[],unsubscribe:function(n){if(this.subscriptions[n.channel][n.topic])for(var t=this.subscriptions[n.channel][n.topic].length,i=0;t>i;i++)if(this.subscriptions[n.channel][n.topic][i]===n){this.subscriptions[n.channel][n.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:t,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(n){return new e(n)},subscribe:function(n){return new r(n.channel||t,n.topic,n.callback)},publish:function(n){return n.channel=n.channel||t,h.configuration.bus.publish(n)},addWireTap:function(n){return this.configuration.bus.addWireTap(n)},linkChannels:function(i,s){var c=[];return i=n.isArray(i)?i:[i],s=n.isArray(s)?s:[s],n.each(i,function(i){i.topic||"#",n.each(s,function(s){var e=s.channel||t;c.push(h.subscribe({channel:i.channel||t,topic:i.topic||"#",callback:function(t,i){var c=n.clone(i);c.topic=n.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=t,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var n=arguments[0],t=arguments[1];return 1===arguments.length&&(n=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,t=arguments[0].topic),h.configuration.bus.subscriptions[n]&&h.configuration.bus.subscriptions[n].hasOwnProperty(t)?h.configuration.bus.subscriptions[n][t]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h}); \ No newline at end of file diff --git a/lib/postal.js b/lib/postal.js index c279d9b..c7f0224 100755 --- 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.3 + Version 0.8.4 */ (function ( root, factory ) { if ( typeof module === "object" && module.exports ) { @@ -209,21 +209,24 @@ regex : {}, compare : function ( binding, topic ) { - var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]); + var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]); if(typeof result !== "undefined") { return result; } if(!(rgx = this.regex[binding])) { pattern = "^" + _.map(binding.split('.'), function(segment) { - var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b"; + var res = ""; + if (!!prevSegment) { + res = prevSegment !== "#" ? "\\.\\b" : "\\b"; + } if(segment === "#") { - res += "[A-Z,a-z,0-9,\\.]*" + res += "[\\s\\S]*" } else if (segment === "*") { - res += "[A-Z,a-z,0-9]+" + res += "[^.]+" } else { res += segment; } - prev = segment; + prevSegment = segment; return res; } ).join('') + "$"; rgx = this.regex[binding] = new RegExp( pattern ); diff --git a/lib/postal.min.js b/lib/postal.min.js index 50d0975..3db2218 100755 --- 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.3 + Version 0.8.4 */ -(function(n,t){"object"==typeof module&&module.exports?module.exports=function(n){return n=n||require("underscore"),t(n)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return t(i,n)}):n.postal=t(n._,n)})(this,function(n){var t="/",i="postal",s=function(){var t;return function(i){var s=!1;return n.isString(i)?(s=i===t,t=i):(s=n.isEqual(i,t),t=n.clone(i)),!s}},c=function(){var t=[];return function(i){var s=!n.any(t,function(t){return n.isObject(i)||n.isArray(i)?n.isEqual(i,t):i===t});return s&&t.push(i),s}},e=function(n){this.channel=n||t};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var n=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return n.channel=this.channel,h.configuration.bus.publish(n)};var r=function(n,t,s){this.channel=n,this.topic=t,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:n,topic:t}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var n=this.callback;return this.callback=function(t){setTimeout(function(){n(t)},0)},this},disposeAfter:function(t){if(n.isNaN(t)||0>=t)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=n.after(t,n.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!n.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var i=this;return n.isArray(t)&&n.each(t,function(n){i.withConstraint(n)}),i},withContext:function(n){return this.context=n,this},withDebounce:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.debounce(i,t),this},withDelay:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(n){setTimeout(function(){i(n)},t)},this},withThrottle:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.throttle(i,t),this},subscribe:function(n){return this.callback=n,this}};var o={cache:{},regex:{},compare:function(t,i){var s,c,e,r=this.cache[i]&&this.cache[i][t];return r!==undefined?r:((c=this.regex[t])||(s="^"+n.map(t.split("."),function(n){var t=e&&"#"!==e?"\\.\\b":"\\b";return t+="#"===n?"[A-Z,a-z,0-9,\\.]*":"*"===n?"[A-Z,a-z,0-9]+":n,e=n,t}).join("")+"$",c=this.regex[t]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][t]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(t,i){h.configuration.resolver.compare(t.topic,i.topic)&&n.all(t.constraints,function(n){return n.call(t.context,i.data,i)})&&"function"==typeof t.callback&&t.callback.call(t.context,i.data,i)},a={addWireTap:function(n){var t=this;return t.wireTaps.push(n),function(){var i=t.wireTaps.indexOf(n);-1!==i&&t.wireTaps.splice(i,1)}},publish:function(t){return t.timeStamp=new Date,n.each(this.wireTaps,function(n){n(t.data,t)}),this.subscriptions[t.channel]&&n.each(this.subscriptions[t.channel],function(n){for(var i,s=0,c=n.length;c>s;)(i=n[s++])&&u(i,t)}),t},reset:function(){this.subscriptions&&(n.each(this.subscriptions,function(t){n.each(t,function(n){for(;n.length;)n.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(n){var t,i=this.subscriptions[n.channel];return i||(i=this.subscriptions[n.channel]={}),t=this.subscriptions[n.channel][n.topic],t||(t=this.subscriptions[n.channel][n.topic]=[]),t.push(n),n},subscriptions:{},wireTaps:[],unsubscribe:function(n){if(this.subscriptions[n.channel][n.topic])for(var t=this.subscriptions[n.channel][n.topic].length,i=0;t>i;i++)if(this.subscriptions[n.channel][n.topic][i]===n){this.subscriptions[n.channel][n.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:t,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(n){return new e(n)},subscribe:function(n){return new r(n.channel||t,n.topic,n.callback)},publish:function(n){return n.channel=n.channel||t,h.configuration.bus.publish(n)},addWireTap:function(n){return this.configuration.bus.addWireTap(n)},linkChannels:function(i,s){var c=[];return i=n.isArray(i)?i:[i],s=n.isArray(s)?s:[s],n.each(i,function(i){i.topic||"#",n.each(s,function(s){var e=s.channel||t;c.push(h.subscribe({channel:i.channel||t,topic:i.topic||"#",callback:function(t,i){var c=n.clone(i);c.topic=n.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=t,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var n=arguments[0],t=arguments[1];return 1===arguments.length&&(n=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,t=arguments[0].topic),h.configuration.bus.subscriptions[n]&&h.configuration.bus.subscriptions[n].hasOwnProperty(t)?h.configuration.bus.subscriptions[n][t]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h}); \ No newline at end of file +(function(n,t){"object"==typeof module&&module.exports?module.exports=function(n){return n=n||require("underscore"),t(n)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return t(i,n)}):n.postal=t(n._,n)})(this,function(n){var t="/",i="postal",s=function(){var t;return function(i){var s=!1;return n.isString(i)?(s=i===t,t=i):(s=n.isEqual(i,t),t=n.clone(i)),!s}},c=function(){var t=[];return function(i){var s=!n.any(t,function(t){return n.isObject(i)||n.isArray(i)?n.isEqual(i,t):i===t});return s&&t.push(i),s}},e=function(n){this.channel=n||t};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var n=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return n.channel=this.channel,h.configuration.bus.publish(n)};var r=function(n,t,s){this.channel=n,this.topic=t,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:n,topic:t}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var n=this.callback;return this.callback=function(t){setTimeout(function(){n(t)},0)},this},disposeAfter:function(t){if(n.isNaN(t)||0>=t)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=n.after(t,n.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!n.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var i=this;return n.isArray(t)&&n.each(t,function(n){i.withConstraint(n)}),i},withContext:function(n){return this.context=n,this},withDebounce:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.debounce(i,t),this},withDelay:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(n){setTimeout(function(){i(n)},t)},this},withThrottle:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.throttle(i,t),this},subscribe:function(n){return this.callback=n,this}};var o={cache:{},regex:{},compare:function(t,i){var s,c,e,r=this.cache[i]&&this.cache[i][t];return r!==undefined?r:((c=this.regex[t])||(s="^"+n.map(t.split("."),function(n){var t="";return e&&(t="#"!==e?"\\.\\b":"\\b"),t+="#"===n?"[\\s\\S]*":"*"===n?"[^.]+":n,e=n,t}).join("")+"$",c=this.regex[t]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][t]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(t,i){h.configuration.resolver.compare(t.topic,i.topic)&&n.all(t.constraints,function(n){return n.call(t.context,i.data,i)})&&"function"==typeof t.callback&&t.callback.call(t.context,i.data,i)},a={addWireTap:function(n){var t=this;return t.wireTaps.push(n),function(){var i=t.wireTaps.indexOf(n);-1!==i&&t.wireTaps.splice(i,1)}},publish:function(t){return t.timeStamp=new Date,n.each(this.wireTaps,function(n){n(t.data,t)}),this.subscriptions[t.channel]&&n.each(this.subscriptions[t.channel],function(n){for(var i,s=0,c=n.length;c>s;)(i=n[s++])&&u(i,t)}),t},reset:function(){this.subscriptions&&(n.each(this.subscriptions,function(t){n.each(t,function(n){for(;n.length;)n.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(n){var t,i=this.subscriptions[n.channel];return i||(i=this.subscriptions[n.channel]={}),t=this.subscriptions[n.channel][n.topic],t||(t=this.subscriptions[n.channel][n.topic]=[]),t.push(n),n},subscriptions:{},wireTaps:[],unsubscribe:function(n){if(this.subscriptions[n.channel][n.topic])for(var t=this.subscriptions[n.channel][n.topic].length,i=0;t>i;i++)if(this.subscriptions[n.channel][n.topic][i]===n){this.subscriptions[n.channel][n.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:t,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(n){return new e(n)},subscribe:function(n){return new r(n.channel||t,n.topic,n.callback)},publish:function(n){return n.channel=n.channel||t,h.configuration.bus.publish(n)},addWireTap:function(n){return this.configuration.bus.addWireTap(n)},linkChannels:function(i,s){var c=[];return i=n.isArray(i)?i:[i],s=n.isArray(s)?s:[s],n.each(i,function(i){i.topic||"#",n.each(s,function(s){var e=s.channel||t;c.push(h.subscribe({channel:i.channel||t,topic:i.topic||"#",callback:function(t,i){var c=n.clone(i);c.topic=n.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=t,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var n=arguments[0],t=arguments[1];return 1===arguments.length&&(n=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,t=arguments[0].topic),h.configuration.bus.subscriptions[n]&&h.configuration.bus.subscriptions[n].hasOwnProperty(t)?h.configuration.bus.subscriptions[n][t]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h}); \ No newline at end of file diff --git a/package.json b/package.json index 258793f..6213e72 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "postal", "description" : "Pub/Sub library providing wildcard subscriptions, complex message handling, etc. Works server and client-side.", - "version" : "0.8.3", + "version" : "0.8.4", "homepage" : "http://github.com/postaljs/postal.js", "repository" : { "type" : "git", diff --git a/src/AmqpBindingsResolver.js b/src/AmqpBindingsResolver.js index b19f96b..2106742 100644 --- a/src/AmqpBindingsResolver.js +++ b/src/AmqpBindingsResolver.js @@ -3,21 +3,24 @@ var bindingsResolver = { regex : {}, compare : function ( binding, topic ) { - var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]); + var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]); if(typeof result !== "undefined") { return result; } if(!(rgx = this.regex[binding])) { pattern = "^" + _.map(binding.split('.'), function(segment) { - var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b"; + var res = ""; + if (!!prevSegment) { + res = prevSegment !== "#" ? "\\.\\b" : "\\b"; + } if(segment === "#") { - res += "[A-Z,a-z,0-9,\\.]*" + res += "[\\s\\S]*" } else if (segment === "*") { - res += "[A-Z,a-z,0-9]+" + res += "[^.]+" } else { res += segment; } - prev = segment; + prevSegment = segment; return res; } ).join('') + "$"; rgx = this.regex[binding] = new RegExp( pattern );