diff --git a/example/amd/js/libs/postal/postal.js b/example/amd/js/libs/postal/postal.js index c2e46ad..5d49000 100644 --- a/example/amd/js/libs/postal/postal.js +++ b/example/amd/js/libs/postal/postal.js @@ -10,39 +10,7 @@ var DEFAULT_EXCHANGE = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, SYSTEM_EXCHANGE = "postal", - NO_OP = function() { }, - parsePublishArgs = function(args) { - var parsed = { envelope: { } }, env; - switch(args.length) { - case 3: - if(typeof args[1] === "Object" && typeof args[2] === "Object") { - parsed.envelope.exchange = DEFAULT_EXCHANGE; - parsed.envelope.topic = args[0]; - parsed.payload = args[1]; - env = parsed.envelope; - parsed.envelope = _.extend(env, args[2]); - } - else { - parsed.envelope.exchange = args[0]; - parsed.envelope.topic = args[1]; - parsed.payload = args[2]; - } - break; - case 4: - parsed.envelope.exchange = args[0]; - parsed.envelope.topic = args[1]; - parsed.payload = args[2]; - env = parsed.envelope; - parsed.envelope = _.extend(env, args[3]); - break; - default: - parsed.envelope.exchange = DEFAULT_EXCHANGE; - parsed.envelope.topic = args[0]; - parsed.payload = args[1]; - break; - } - return parsed; - }; + NO_OP = function() { }; var DistinctPredicate = function() { var previous; @@ -73,12 +41,11 @@ ChannelDefinition.prototype = { }, publish: function(data, envelope) { - var env = _.extend({ - exchange: this.exchange, - timeStamp: new Date(), - topic: this.topic - }, envelope); - postal.configuration.bus.publish(data, env); + var env = envelope || {}; + env.exchange = this.exchange; + env.timeStamp = new Date(); + env.topic = this.topic; + postal.configuration.bus.publish(env, data); } }; @@ -87,12 +54,15 @@ var SubscriptionDefinition = function(exchange, topic, callback) { this.topic = topic; this.callback = callback; this.priority = DEFAULT_PRIORITY; - this.constraints = []; + this.constraints = new Array(0); this.maxCalls = DEFAULT_DISPOSEAFTER; this.onHandled = NO_OP; this.context = null; - postal.publish(SYSTEM_EXCHANGE, "subscription.created", + postal.publish({ + exchange: SYSTEM_EXCHANGE, + topic: "subscription.created" + }, { event: "subscription.created", exchange: exchange, @@ -103,7 +73,10 @@ var SubscriptionDefinition = function(exchange, topic, callback) { SubscriptionDefinition.prototype = { unsubscribe: function() { postal.configuration.bus.unsubscribe(this); - postal.publish(SYSTEM_EXCHANGE, "subscription.removed", + postal.publish({ + exchange: SYSTEM_EXCHANGE, + topic: "subscription.removed" + }, { event: "subscription.removed", exchange: this.exchange, @@ -215,7 +188,10 @@ var bindingsResolver = { if(this.cache[topic] && this.cache[topic][binding]) { return true; } - var rgx = new RegExp("^" + this.regexify(binding) + "$"), // match from start to end of string + // binding.replace(/\./g,"\\.") // escape actual periods + // .replace(/\*/g, ".*") // asterisks match any value + // .replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word' + var rgx = new RegExp("^" + binding.replace(/\./g,"\\.").replace(/\*/g, ".*").replace(/#/g, "[A-Z,a-z,0-9]*") + "$"), result = rgx.test(topic); if(result) { if(!this.cache[topic]) { @@ -224,12 +200,6 @@ var bindingsResolver = { this.cache[topic][binding] = true; } return result; - }, - - regexify: function(binding) { - return binding.replace(/\./g,"\\.") // escape actual periods - .replace(/\*/g, ".*") // asterisks match any value - .replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word' } }; @@ -237,10 +207,12 @@ var localBus = { subscriptions: {}, - wireTaps: [], + wireTaps: new Array(0), - publish: function(data, envelope) { - this.notifyTaps(data, envelope); + publish: function(envelope, data) { + _.each(this.wireTaps,function(tap) { + tap(envelope, data); + }); _.each(this.subscriptions[envelope.exchange], function(topic) { _.each(topic, function(binding){ @@ -257,10 +229,15 @@ var localBus = { }, subscribe: function(subDef) { - var idx, found, fn, exch, subs; + var idx, found, fn, exch = this.subscriptions[subDef.exchange], subs; - exch = this.subscriptions[subDef.exchange] = this.subscriptions[subDef.exchange] || {}; - subs = this.subscriptions[subDef.exchange][subDef.topic] = this.subscriptions[subDef.exchange][subDef.topic] || []; + if(!exch) { + exch = this.subscriptions[subDef.exchange] = {}; + } + subs = this.subscriptions[subDef.exchange][subDef.topic] + if(!subs) { + subs = this.subscriptions[subDef.exchange][subDef.topic] = new Array(0); + } idx = subs.length - 1; //if(!_.any(subs, function(cfg) { return cfg === subDef; })) { @@ -277,12 +254,6 @@ var localBus = { //} }, - notifyTaps: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); - }, - unsubscribe: function(config) { if(this.subscriptions[config.exchange][config.topic]) { var len = this.subscriptions[config.exchange][config.topic].length, @@ -308,30 +279,42 @@ var localBus = { } }; +var publishPicker = { + "2" : function(envelope, payload) { + if(!envelope.exchange) { + envelope.exchange = DEFAULT_EXCHANGE; + } + postal.configuration.bus.publish(envelope, payload); + }, + "3" : function(exchange, topic, payload) { + postal.configuration.bus.publish({ exchange: exchange, topic: topic }, payload); + } +}; + var postal = { configuration: { bus: localBus, resolver: bindingsResolver }, - channel: function(exchange, topic) { - var exch = topic ? exchange : DEFAULT_EXCHANGE, - tpc = topic || exchange; + channel: function(options) { + var exch = options.exchange || DEFAULT_EXCHANGE, + tpc = options.topic; return new ChannelDefinition(exch, tpc); }, - subscribe: function(exchange, topic, callback) { - var callbk = callback || topic, - tpc = callback ? topic : exchange, - exch = callback ? exchange : DEFAULT_EXCHANGE; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); + subscribe: function(options) { + var callback = options.callback, + topic = options.topic, + exchange = options.exchange || DEFAULT_EXCHANGE; + return new ChannelDefinition(exchange, topic).subscribe(callback); }, - publish: function(exchange, topic, payload, envelopeOptions) { - var parsedArgs = parsePublishArgs([].slice.call(arguments,0)); - var channel = this.channel(parsedArgs.envelope.exchange, parsedArgs.envelope.topic); - channel.publish(parsedArgs.payload, parsedArgs.envelope); + publish: function() { + var len = arguments.length; + if(publishPicker[len]) { + publishPicker[len].apply(this, arguments); + } }, addWireTap: function(callback) { @@ -339,21 +322,28 @@ var postal = { }, bindExchanges: function(sources, destinations) { - var subscriptions = []; + var subscriptions; if(!_.isArray(sources)) { sources = [sources]; } if(!_.isArray(destinations)) { destinations = [destinations]; } + subscriptions = new Array(sources.length * destinations.length); _.each(sources, function(source){ var sourceTopic = source.topic || "*"; _.each(destinations, function(destination) { var destExchange = destination.exchange || DEFAULT_EXCHANGE; subscriptions.push( - postal.subscribe(source.exchange || DEFAULT_EXCHANGE, source.topic || "*", function(msg, env) { - var destTopic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; - postal.publish(destExchange, destTopic, msg); + postal.subscribe({ + exchange: source.exchange || DEFAULT_EXCHANGE, + topic: source.topic || "*", + callback : function(msg, env) { + var newEnv = env; + newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; + newEnv.exchange = destExchange; + postal.publish(newEnv, msg); + } }) ); }); diff --git a/example/standard/js/postal.js b/example/standard/js/postal.js index 4fc5ba3..be4fc1c 100644 --- a/example/standard/js/postal.js +++ b/example/standard/js/postal.js @@ -10,39 +10,7 @@ var DEFAULT_EXCHANGE = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, SYSTEM_EXCHANGE = "postal", - NO_OP = function() { }, - parsePublishArgs = function(args) { - var parsed = { envelope: { } }, env; - switch(args.length) { - case 3: - if(typeof args[1] === "Object" && typeof args[2] === "Object") { - parsed.envelope.exchange = DEFAULT_EXCHANGE; - parsed.envelope.topic = args[0]; - parsed.payload = args[1]; - env = parsed.envelope; - parsed.envelope = _.extend(env, args[2]); - } - else { - parsed.envelope.exchange = args[0]; - parsed.envelope.topic = args[1]; - parsed.payload = args[2]; - } - break; - case 4: - parsed.envelope.exchange = args[0]; - parsed.envelope.topic = args[1]; - parsed.payload = args[2]; - env = parsed.envelope; - parsed.envelope = _.extend(env, args[3]); - break; - default: - parsed.envelope.exchange = DEFAULT_EXCHANGE; - parsed.envelope.topic = args[0]; - parsed.payload = args[1]; - break; - } - return parsed; - }; + NO_OP = function() { }; var DistinctPredicate = function() { var previous; @@ -73,12 +41,11 @@ ChannelDefinition.prototype = { }, publish: function(data, envelope) { - var env = _.extend({ - exchange: this.exchange, - timeStamp: new Date(), - topic: this.topic - }, envelope); - postal.configuration.bus.publish(data, env); + var env = envelope || {}; + env.exchange = this.exchange; + env.timeStamp = new Date(); + env.topic = this.topic; + postal.configuration.bus.publish(env, data); } }; @@ -87,12 +54,15 @@ var SubscriptionDefinition = function(exchange, topic, callback) { this.topic = topic; this.callback = callback; this.priority = DEFAULT_PRIORITY; - this.constraints = []; + this.constraints = new Array(0); this.maxCalls = DEFAULT_DISPOSEAFTER; this.onHandled = NO_OP; this.context = null; - postal.publish(SYSTEM_EXCHANGE, "subscription.created", + postal.publish({ + exchange: SYSTEM_EXCHANGE, + topic: "subscription.created" + }, { event: "subscription.created", exchange: exchange, @@ -103,7 +73,10 @@ var SubscriptionDefinition = function(exchange, topic, callback) { SubscriptionDefinition.prototype = { unsubscribe: function() { postal.configuration.bus.unsubscribe(this); - postal.publish(SYSTEM_EXCHANGE, "subscription.removed", + postal.publish({ + exchange: SYSTEM_EXCHANGE, + topic: "subscription.removed" + }, { event: "subscription.removed", exchange: this.exchange, @@ -215,7 +188,10 @@ var bindingsResolver = { if(this.cache[topic] && this.cache[topic][binding]) { return true; } - var rgx = new RegExp("^" + this.regexify(binding) + "$"), // match from start to end of string + // binding.replace(/\./g,"\\.") // escape actual periods + // .replace(/\*/g, ".*") // asterisks match any value + // .replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word' + var rgx = new RegExp("^" + binding.replace(/\./g,"\\.").replace(/\*/g, ".*").replace(/#/g, "[A-Z,a-z,0-9]*") + "$"), result = rgx.test(topic); if(result) { if(!this.cache[topic]) { @@ -224,12 +200,6 @@ var bindingsResolver = { this.cache[topic][binding] = true; } return result; - }, - - regexify: function(binding) { - return binding.replace(/\./g,"\\.") // escape actual periods - .replace(/\*/g, ".*") // asterisks match any value - .replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word' } }; @@ -237,10 +207,12 @@ var localBus = { subscriptions: {}, - wireTaps: [], + wireTaps: new Array(0), - publish: function(data, envelope) { - this.notifyTaps(data, envelope); + publish: function(envelope, data) { + _.each(this.wireTaps,function(tap) { + tap(envelope, data); + }); _.each(this.subscriptions[envelope.exchange], function(topic) { _.each(topic, function(binding){ @@ -257,10 +229,15 @@ var localBus = { }, subscribe: function(subDef) { - var idx, found, fn, exch, subs; + var idx, found, fn, exch = this.subscriptions[subDef.exchange], subs; - exch = this.subscriptions[subDef.exchange] = this.subscriptions[subDef.exchange] || {}; - subs = this.subscriptions[subDef.exchange][subDef.topic] = this.subscriptions[subDef.exchange][subDef.topic] || []; + if(!exch) { + exch = this.subscriptions[subDef.exchange] = {}; + } + subs = this.subscriptions[subDef.exchange][subDef.topic] + if(!subs) { + subs = this.subscriptions[subDef.exchange][subDef.topic] = new Array(0); + } idx = subs.length - 1; //if(!_.any(subs, function(cfg) { return cfg === subDef; })) { @@ -277,12 +254,6 @@ var localBus = { //} }, - notifyTaps: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); - }, - unsubscribe: function(config) { if(this.subscriptions[config.exchange][config.topic]) { var len = this.subscriptions[config.exchange][config.topic].length, @@ -308,30 +279,42 @@ var localBus = { } }; +var publishPicker = { + "2" : function(envelope, payload) { + if(!envelope.exchange) { + envelope.exchange = DEFAULT_EXCHANGE; + } + postal.configuration.bus.publish(envelope, payload); + }, + "3" : function(exchange, topic, payload) { + postal.configuration.bus.publish({ exchange: exchange, topic: topic }, payload); + } +}; + var postal = { configuration: { bus: localBus, resolver: bindingsResolver }, - channel: function(exchange, topic) { - var exch = topic ? exchange : DEFAULT_EXCHANGE, - tpc = topic || exchange; + channel: function(options) { + var exch = options.exchange || DEFAULT_EXCHANGE, + tpc = options.topic; return new ChannelDefinition(exch, tpc); }, - subscribe: function(exchange, topic, callback) { - var callbk = callback || topic, - tpc = callback ? topic : exchange, - exch = callback ? exchange : DEFAULT_EXCHANGE; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); + subscribe: function(options) { + var callback = options.callback, + topic = options.topic, + exchange = options.exchange || DEFAULT_EXCHANGE; + return new ChannelDefinition(exchange, topic).subscribe(callback); }, - publish: function(exchange, topic, payload, envelopeOptions) { - var parsedArgs = parsePublishArgs([].slice.call(arguments,0)); - var channel = this.channel(parsedArgs.envelope.exchange, parsedArgs.envelope.topic); - channel.publish(parsedArgs.payload, parsedArgs.envelope); + publish: function() { + var len = arguments.length; + if(publishPicker[len]) { + publishPicker[len].apply(this, arguments); + } }, addWireTap: function(callback) { @@ -339,21 +322,28 @@ var postal = { }, bindExchanges: function(sources, destinations) { - var subscriptions = []; + var subscriptions; if(!_.isArray(sources)) { sources = [sources]; } if(!_.isArray(destinations)) { destinations = [destinations]; } + subscriptions = new Array(sources.length * destinations.length); _.each(sources, function(source){ var sourceTopic = source.topic || "*"; _.each(destinations, function(destination) { var destExchange = destination.exchange || DEFAULT_EXCHANGE; subscriptions.push( - postal.subscribe(source.exchange || DEFAULT_EXCHANGE, source.topic || "*", function(msg, env) { - var destTopic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; - postal.publish(destExchange, destTopic, msg); + postal.subscribe({ + exchange: source.exchange || DEFAULT_EXCHANGE, + topic: source.topic || "*", + callback : function(msg, env) { + var newEnv = env; + newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; + newEnv.exchange = destExchange; + postal.publish(newEnv, msg); + } }) ); }); diff --git a/lib/browser/amd/postal.diagnostics.min.gz.js b/lib/browser/amd/postal.diagnostics.min.gz.js index a1b9e18..f79dcd2 100644 Binary files a/lib/browser/amd/postal.diagnostics.min.gz.js and b/lib/browser/amd/postal.diagnostics.min.gz.js differ diff --git a/lib/browser/amd/postal.js b/lib/browser/amd/postal.js index c2e46ad..5d49000 100644 --- a/lib/browser/amd/postal.js +++ b/lib/browser/amd/postal.js @@ -10,39 +10,7 @@ var DEFAULT_EXCHANGE = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, SYSTEM_EXCHANGE = "postal", - NO_OP = function() { }, - parsePublishArgs = function(args) { - var parsed = { envelope: { } }, env; - switch(args.length) { - case 3: - if(typeof args[1] === "Object" && typeof args[2] === "Object") { - parsed.envelope.exchange = DEFAULT_EXCHANGE; - parsed.envelope.topic = args[0]; - parsed.payload = args[1]; - env = parsed.envelope; - parsed.envelope = _.extend(env, args[2]); - } - else { - parsed.envelope.exchange = args[0]; - parsed.envelope.topic = args[1]; - parsed.payload = args[2]; - } - break; - case 4: - parsed.envelope.exchange = args[0]; - parsed.envelope.topic = args[1]; - parsed.payload = args[2]; - env = parsed.envelope; - parsed.envelope = _.extend(env, args[3]); - break; - default: - parsed.envelope.exchange = DEFAULT_EXCHANGE; - parsed.envelope.topic = args[0]; - parsed.payload = args[1]; - break; - } - return parsed; - }; + NO_OP = function() { }; var DistinctPredicate = function() { var previous; @@ -73,12 +41,11 @@ ChannelDefinition.prototype = { }, publish: function(data, envelope) { - var env = _.extend({ - exchange: this.exchange, - timeStamp: new Date(), - topic: this.topic - }, envelope); - postal.configuration.bus.publish(data, env); + var env = envelope || {}; + env.exchange = this.exchange; + env.timeStamp = new Date(); + env.topic = this.topic; + postal.configuration.bus.publish(env, data); } }; @@ -87,12 +54,15 @@ var SubscriptionDefinition = function(exchange, topic, callback) { this.topic = topic; this.callback = callback; this.priority = DEFAULT_PRIORITY; - this.constraints = []; + this.constraints = new Array(0); this.maxCalls = DEFAULT_DISPOSEAFTER; this.onHandled = NO_OP; this.context = null; - postal.publish(SYSTEM_EXCHANGE, "subscription.created", + postal.publish({ + exchange: SYSTEM_EXCHANGE, + topic: "subscription.created" + }, { event: "subscription.created", exchange: exchange, @@ -103,7 +73,10 @@ var SubscriptionDefinition = function(exchange, topic, callback) { SubscriptionDefinition.prototype = { unsubscribe: function() { postal.configuration.bus.unsubscribe(this); - postal.publish(SYSTEM_EXCHANGE, "subscription.removed", + postal.publish({ + exchange: SYSTEM_EXCHANGE, + topic: "subscription.removed" + }, { event: "subscription.removed", exchange: this.exchange, @@ -215,7 +188,10 @@ var bindingsResolver = { if(this.cache[topic] && this.cache[topic][binding]) { return true; } - var rgx = new RegExp("^" + this.regexify(binding) + "$"), // match from start to end of string + // binding.replace(/\./g,"\\.") // escape actual periods + // .replace(/\*/g, ".*") // asterisks match any value + // .replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word' + var rgx = new RegExp("^" + binding.replace(/\./g,"\\.").replace(/\*/g, ".*").replace(/#/g, "[A-Z,a-z,0-9]*") + "$"), result = rgx.test(topic); if(result) { if(!this.cache[topic]) { @@ -224,12 +200,6 @@ var bindingsResolver = { this.cache[topic][binding] = true; } return result; - }, - - regexify: function(binding) { - return binding.replace(/\./g,"\\.") // escape actual periods - .replace(/\*/g, ".*") // asterisks match any value - .replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word' } }; @@ -237,10 +207,12 @@ var localBus = { subscriptions: {}, - wireTaps: [], + wireTaps: new Array(0), - publish: function(data, envelope) { - this.notifyTaps(data, envelope); + publish: function(envelope, data) { + _.each(this.wireTaps,function(tap) { + tap(envelope, data); + }); _.each(this.subscriptions[envelope.exchange], function(topic) { _.each(topic, function(binding){ @@ -257,10 +229,15 @@ var localBus = { }, subscribe: function(subDef) { - var idx, found, fn, exch, subs; + var idx, found, fn, exch = this.subscriptions[subDef.exchange], subs; - exch = this.subscriptions[subDef.exchange] = this.subscriptions[subDef.exchange] || {}; - subs = this.subscriptions[subDef.exchange][subDef.topic] = this.subscriptions[subDef.exchange][subDef.topic] || []; + if(!exch) { + exch = this.subscriptions[subDef.exchange] = {}; + } + subs = this.subscriptions[subDef.exchange][subDef.topic] + if(!subs) { + subs = this.subscriptions[subDef.exchange][subDef.topic] = new Array(0); + } idx = subs.length - 1; //if(!_.any(subs, function(cfg) { return cfg === subDef; })) { @@ -277,12 +254,6 @@ var localBus = { //} }, - notifyTaps: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); - }, - unsubscribe: function(config) { if(this.subscriptions[config.exchange][config.topic]) { var len = this.subscriptions[config.exchange][config.topic].length, @@ -308,30 +279,42 @@ var localBus = { } }; +var publishPicker = { + "2" : function(envelope, payload) { + if(!envelope.exchange) { + envelope.exchange = DEFAULT_EXCHANGE; + } + postal.configuration.bus.publish(envelope, payload); + }, + "3" : function(exchange, topic, payload) { + postal.configuration.bus.publish({ exchange: exchange, topic: topic }, payload); + } +}; + var postal = { configuration: { bus: localBus, resolver: bindingsResolver }, - channel: function(exchange, topic) { - var exch = topic ? exchange : DEFAULT_EXCHANGE, - tpc = topic || exchange; + channel: function(options) { + var exch = options.exchange || DEFAULT_EXCHANGE, + tpc = options.topic; return new ChannelDefinition(exch, tpc); }, - subscribe: function(exchange, topic, callback) { - var callbk = callback || topic, - tpc = callback ? topic : exchange, - exch = callback ? exchange : DEFAULT_EXCHANGE; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); + subscribe: function(options) { + var callback = options.callback, + topic = options.topic, + exchange = options.exchange || DEFAULT_EXCHANGE; + return new ChannelDefinition(exchange, topic).subscribe(callback); }, - publish: function(exchange, topic, payload, envelopeOptions) { - var parsedArgs = parsePublishArgs([].slice.call(arguments,0)); - var channel = this.channel(parsedArgs.envelope.exchange, parsedArgs.envelope.topic); - channel.publish(parsedArgs.payload, parsedArgs.envelope); + publish: function() { + var len = arguments.length; + if(publishPicker[len]) { + publishPicker[len].apply(this, arguments); + } }, addWireTap: function(callback) { @@ -339,21 +322,28 @@ var postal = { }, bindExchanges: function(sources, destinations) { - var subscriptions = []; + var subscriptions; if(!_.isArray(sources)) { sources = [sources]; } if(!_.isArray(destinations)) { destinations = [destinations]; } + subscriptions = new Array(sources.length * destinations.length); _.each(sources, function(source){ var sourceTopic = source.topic || "*"; _.each(destinations, function(destination) { var destExchange = destination.exchange || DEFAULT_EXCHANGE; subscriptions.push( - postal.subscribe(source.exchange || DEFAULT_EXCHANGE, source.topic || "*", function(msg, env) { - var destTopic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; - postal.publish(destExchange, destTopic, msg); + postal.subscribe({ + exchange: source.exchange || DEFAULT_EXCHANGE, + topic: source.topic || "*", + callback : function(msg, env) { + var newEnv = env; + newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; + newEnv.exchange = destExchange; + postal.publish(newEnv, msg); + } }) ); }); diff --git a/lib/browser/amd/postal.min.gz.js b/lib/browser/amd/postal.min.gz.js index 6d503d2..df51567 100644 Binary files a/lib/browser/amd/postal.min.gz.js and b/lib/browser/amd/postal.min.gz.js differ diff --git a/lib/browser/amd/postal.min.js b/lib/browser/amd/postal.min.js index 0059a3e..1a12081 100644 --- a/lib/browser/amd/postal.min.js +++ b/lib/browser/amd/postal.min.js @@ -1 +1 @@ -define(["underscore"],function(a){var b="/",c=50,d=0,e="postal",f=function(){},g=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},h=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}},i=function(a,b){this.exchange=a,this.topic=b};i.prototype={subscribe:function(a){var b=new j(this.exchange,this.topic,a);return m.configuration.bus.subscribe(b),b},publish:function(b,c){var d=a.extend({exchange:this.exchange,timeStamp:new Date,topic:this.topic},c);m.configuration.bus.publish(b,d)}};var j=function(a,b,g){this.exchange=a,this.topic=b,this.callback=g,this.priority=c,this.constraints=[],this.maxCalls=d,this.onHandled=f,this.context=null,m.publish(e,"subscription.created",{event:"subscription.created",exchange:a,topic:b})};j.prototype={unsubscribe:function(){m.configuration.bus.unsubscribe(this),m.publish(e,"subscription.removed",{event:"subscription.removed",exchange:this.exchange,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 h),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 k={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]*")}},l={subscriptions:{},wireTaps:[],publish:function(b,c){this.notifyTaps(b,c),a.each(this.subscriptions[c.exchange],function(d){a.each(d,function(d){m.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(a){var b,c,d,e,f;e=this.subscriptions[a.exchange]=this.subscriptions[a.exchange]||{},f=this.subscriptions[a.exchange][a.topic]=this.subscriptions[a.exchange][a.topic]||[],b=f.length-1;for(;b>=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}c||f.unshift(a)},notifyTaps:function(b,c){a.each(this.wireTaps,function(a){a(b,c)})},unsubscribe:function(a){if(this.subscriptions[a.exchange][a.topic]){var b=this.subscriptions[a.exchange][a.topic].length,c=0;for(;c=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}c||f.unshift(a)},unsubscribe:function(a){if(this.subscriptions[a.exchange][a.topic]){var b=this.subscriptions[a.exchange][a.topic].length,c=0;for(;c=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}c||f.unshift(a)},notifyTaps:function(a,b){_.each(this.wireTaps,function(c){c(a,b)})},unsubscribe:function(a){if(this.subscriptions[a.exchange][a.topic]){var b=this.subscriptions[a.exchange][a.topic].length,c=0;for(;c=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}c||f.unshift(a)},unsubscribe:function(a){if(this.subscriptions[a.exchange][a.topic]){var b=this.subscriptions[a.exchange][a.topic].length,c=0;for(;c