diff --git a/example/amd/js/libs/postal/postal.js b/example/amd/js/libs/postal/postal.js index 62f4dad..25511c7 100644 --- a/example/amd/js/libs/postal/postal.js +++ b/example/amd/js/libs/postal/postal.js @@ -9,6 +9,7 @@ define(['underscore'], function(_) { var DEFAULT_EXCHANGE = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, + SYSTEM_EXCHANGE = "postal", NO_OP = function() { }, parsePublishArgs = function(args) { var parsed = { envelope: { } }, env; @@ -90,11 +91,24 @@ var SubscriptionDefinition = function(exchange, topic, callback) { this.maxCalls = DEFAULT_DISPOSEAFTER; this.onHandled = NO_OP; this.context = null; + + postal.publish(SYSTEM_EXCHANGE, "subscription.created", + { + event: "subscription.created", + exchange: exchange, + topic: topic + }); }; SubscriptionDefinition.prototype = { unsubscribe: function() { postal.configuration.bus.unsubscribe(this); + postal.publish(SYSTEM_EXCHANGE, "subscription.removed", + { + event: "subscription.removed", + exchange: this.exchange, + topic: this.topic + }); }, defer: function() { @@ -221,109 +235,131 @@ var bindingsResolver = { var localBus = { - subscriptions: {}, + subscriptions: {}, - wireTaps: [], + wireTaps: [], - publish: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); + publish: function(data, envelope) { + this.notifyTaps(data, envelope); - _.each(this.subscriptions[envelope.exchange], function(topic) { - _.each(topic, function(binding){ - if(postal.configuration.resolver.compare(binding.topic, envelope.topic)) { - if(_.all(binding.constraints, function(constraint) { return constraint(data); })) { - if(typeof binding.callback === 'function') { - binding.callback.apply(binding.context, [data, envelope]); - binding.onHandled(); - } - } - } - }); - }); - }, + _.each(this.subscriptions[envelope.exchange], function(topic) { + _.each(topic, function(binding){ + if(postal.configuration.resolver.compare(binding.topic, envelope.topic)) { + if(_.all(binding.constraints, function(constraint) { return constraint(data); })) { + if(typeof binding.callback === 'function') { + binding.callback.apply(binding.context, [data, envelope]); + binding.onHandled(); + } + } + } + }); + }); + }, - subscribe: function(subDef) { - var idx, found, fn; + subscribe: function(subDef) { + var idx, found, fn, exch, subs; - if(!this.subscriptions[subDef.exchange]) { - this.subscriptions[subDef.exchange] = {}; - } - if(!this.subscriptions[subDef.exchange][subDef.topic]) { - this.subscriptions[subDef.exchange][subDef.topic] = []; - } + exch = this.subscriptions[subDef.exchange] = this.subscriptions[subDef.exchange] || {}; + subs = this.subscriptions[subDef.exchange][subDef.topic] = this.subscriptions[subDef.exchange][subDef.topic] || []; - idx = this.subscriptions[subDef.exchange][subDef.topic].length - 1; - if(!_.any(this.subscriptions[subDef.exchange][subDef.topic], function(cfg) { return cfg === subDef; })) { - for(; idx >= 0; idx--) { - if(this.subscriptions[subDef.exchange][subDef.topic][idx].priority <= subDef.priority) { - this.subscriptions[subDef.exchange][subDef.topic].splice(idx + 1, 0, subDef); - found = true; - break; - } - } - if(!found) { - this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef); - } - } + idx = subs.length - 1; + if(!_.any(subs, function(cfg) { return cfg === subDef; })) { + for(; idx >= 0; idx--) { + if(subs[idx].priority <= subDef.priority) { + subs.splice(idx + 1, 0, subDef); + found = true; + break; + } + } + if(!found) { + subs.unshift(subDef); + } + } + }, - return _.bind(function() { this.unsubscribe(subDef); }, this); - }, + 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, - idx = 0; - for ( ; idx < len; idx++ ) { - if (this.subscriptions[config.exchange][config.topic][idx] === config) { - this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); - break; - } - } - } - }, + unsubscribe: function(config) { + if(this.subscriptions[config.exchange][config.topic]) { + var len = this.subscriptions[config.exchange][config.topic].length, + idx = 0; + for ( ; idx < len; idx++ ) { + if (this.subscriptions[config.exchange][config.topic][idx] === config) { + this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); + break; + } + } + } + }, - addWireTap: function(callback) { - this.wireTaps.push(callback); - return function() { - var idx = this.wireTaps.indexOf(callback); - if(idx !== -1) { - this.wireTaps.splice(idx,1); - } - }; - } + addWireTap: function(callback) { + var self = this; + self.wireTaps.push(callback); + return function() { + var idx = self.wireTaps.indexOf(callback); + if(idx !== -1) { + self.wireTaps.splice(idx,1); + } + }; + } }; var postal = { - configuration: { - bus: localBus, - resolver: bindingsResolver - }, + configuration: { + bus: localBus, + resolver: bindingsResolver + }, - channel: function(exchange, topic) { - var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 2 ? topic : exchange; - return new ChannelDefinition(exch, tpc); - }, + channel: function(exchange, topic) { + var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 2 ? topic : exchange; + return new ChannelDefinition(exch, tpc); + }, - subscribe: function(exchange, topic, callback) { - var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 3 ? topic : exchange, - callbk = arguments.length === 3 ? callback : topic; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); - }, + subscribe: function(exchange, topic, callback) { + var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 3 ? topic : exchange, + callbk = arguments.length === 3 ? callback : topic; + var channel = this.channel(exch, tpc); + return channel.subscribe(callbk); + }, - 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(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); + }, - addWireTap: function(callback) { - this.configuration.bus.addWireTap(callback); - } + addWireTap: function(callback) { + return this.configuration.bus.addWireTap(callback); + }, + + bindExchanges: function(sources, destinations) { + var subscriptions = []; + if(!_.isArray(sources)) { + sources = [sources]; + } + if(!_.isArray(destinations)) { + destinations = [destinations]; + } + _.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); + }) + ); + }); + }); + return subscriptions; + } }; return postal; }); \ No newline at end of file diff --git a/example/standard/js/postal.js b/example/standard/js/postal.js index 9894230..2c04166 100644 --- a/example/standard/js/postal.js +++ b/example/standard/js/postal.js @@ -9,6 +9,7 @@ var DEFAULT_EXCHANGE = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, + SYSTEM_EXCHANGE = "postal", NO_OP = function() { }, parsePublishArgs = function(args) { var parsed = { envelope: { } }, env; @@ -90,11 +91,24 @@ var SubscriptionDefinition = function(exchange, topic, callback) { this.maxCalls = DEFAULT_DISPOSEAFTER; this.onHandled = NO_OP; this.context = null; + + postal.publish(SYSTEM_EXCHANGE, "subscription.created", + { + event: "subscription.created", + exchange: exchange, + topic: topic + }); }; SubscriptionDefinition.prototype = { unsubscribe: function() { postal.configuration.bus.unsubscribe(this); + postal.publish(SYSTEM_EXCHANGE, "subscription.removed", + { + event: "subscription.removed", + exchange: this.exchange, + topic: this.topic + }); }, defer: function() { @@ -221,109 +235,131 @@ var bindingsResolver = { var localBus = { - subscriptions: {}, + subscriptions: {}, - wireTaps: [], + wireTaps: [], - publish: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); + publish: function(data, envelope) { + this.notifyTaps(data, envelope); - _.each(this.subscriptions[envelope.exchange], function(topic) { - _.each(topic, function(binding){ - if(postal.configuration.resolver.compare(binding.topic, envelope.topic)) { - if(_.all(binding.constraints, function(constraint) { return constraint(data); })) { - if(typeof binding.callback === 'function') { - binding.callback.apply(binding.context, [data, envelope]); - binding.onHandled(); - } - } - } - }); - }); - }, + _.each(this.subscriptions[envelope.exchange], function(topic) { + _.each(topic, function(binding){ + if(postal.configuration.resolver.compare(binding.topic, envelope.topic)) { + if(_.all(binding.constraints, function(constraint) { return constraint(data); })) { + if(typeof binding.callback === 'function') { + binding.callback.apply(binding.context, [data, envelope]); + binding.onHandled(); + } + } + } + }); + }); + }, - subscribe: function(subDef) { - var idx, found, fn; + subscribe: function(subDef) { + var idx, found, fn, exch, subs; - if(!this.subscriptions[subDef.exchange]) { - this.subscriptions[subDef.exchange] = {}; - } - if(!this.subscriptions[subDef.exchange][subDef.topic]) { - this.subscriptions[subDef.exchange][subDef.topic] = []; - } + exch = this.subscriptions[subDef.exchange] = this.subscriptions[subDef.exchange] || {}; + subs = this.subscriptions[subDef.exchange][subDef.topic] = this.subscriptions[subDef.exchange][subDef.topic] || []; - idx = this.subscriptions[subDef.exchange][subDef.topic].length - 1; - if(!_.any(this.subscriptions[subDef.exchange][subDef.topic], function(cfg) { return cfg === subDef; })) { - for(; idx >= 0; idx--) { - if(this.subscriptions[subDef.exchange][subDef.topic][idx].priority <= subDef.priority) { - this.subscriptions[subDef.exchange][subDef.topic].splice(idx + 1, 0, subDef); - found = true; - break; - } - } - if(!found) { - this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef); - } - } + idx = subs.length - 1; + if(!_.any(subs, function(cfg) { return cfg === subDef; })) { + for(; idx >= 0; idx--) { + if(subs[idx].priority <= subDef.priority) { + subs.splice(idx + 1, 0, subDef); + found = true; + break; + } + } + if(!found) { + subs.unshift(subDef); + } + } + }, - return _.bind(function() { this.unsubscribe(subDef); }, this); - }, + 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, - idx = 0; - for ( ; idx < len; idx++ ) { - if (this.subscriptions[config.exchange][config.topic][idx] === config) { - this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); - break; - } - } - } - }, + unsubscribe: function(config) { + if(this.subscriptions[config.exchange][config.topic]) { + var len = this.subscriptions[config.exchange][config.topic].length, + idx = 0; + for ( ; idx < len; idx++ ) { + if (this.subscriptions[config.exchange][config.topic][idx] === config) { + this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); + break; + } + } + } + }, - addWireTap: function(callback) { - this.wireTaps.push(callback); - return function() { - var idx = this.wireTaps.indexOf(callback); - if(idx !== -1) { - this.wireTaps.splice(idx,1); - } - }; - } + addWireTap: function(callback) { + var self = this; + self.wireTaps.push(callback); + return function() { + var idx = self.wireTaps.indexOf(callback); + if(idx !== -1) { + self.wireTaps.splice(idx,1); + } + }; + } }; var postal = { - configuration: { - bus: localBus, - resolver: bindingsResolver - }, + configuration: { + bus: localBus, + resolver: bindingsResolver + }, - channel: function(exchange, topic) { - var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 2 ? topic : exchange; - return new ChannelDefinition(exch, tpc); - }, + channel: function(exchange, topic) { + var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 2 ? topic : exchange; + return new ChannelDefinition(exch, tpc); + }, - subscribe: function(exchange, topic, callback) { - var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 3 ? topic : exchange, - callbk = arguments.length === 3 ? callback : topic; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); - }, + subscribe: function(exchange, topic, callback) { + var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 3 ? topic : exchange, + callbk = arguments.length === 3 ? callback : topic; + var channel = this.channel(exch, tpc); + return channel.subscribe(callbk); + }, - 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(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); + }, - addWireTap: function(callback) { - this.configuration.bus.addWireTap(callback); - } + addWireTap: function(callback) { + return this.configuration.bus.addWireTap(callback); + }, + + bindExchanges: function(sources, destinations) { + var subscriptions = []; + if(!_.isArray(sources)) { + sources = [sources]; + } + if(!_.isArray(destinations)) { + destinations = [destinations]; + } + _.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); + }) + ); + }); + }); + return subscriptions; + } }; global.postal = postal; })(window); \ No newline at end of file diff --git a/lib/browser/amd/postal.diagnostics.min.gz.js b/lib/browser/amd/postal.diagnostics.min.gz.js index 3a59cdd..4da730f 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 62f4dad..25511c7 100644 --- a/lib/browser/amd/postal.js +++ b/lib/browser/amd/postal.js @@ -9,6 +9,7 @@ define(['underscore'], function(_) { var DEFAULT_EXCHANGE = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, + SYSTEM_EXCHANGE = "postal", NO_OP = function() { }, parsePublishArgs = function(args) { var parsed = { envelope: { } }, env; @@ -90,11 +91,24 @@ var SubscriptionDefinition = function(exchange, topic, callback) { this.maxCalls = DEFAULT_DISPOSEAFTER; this.onHandled = NO_OP; this.context = null; + + postal.publish(SYSTEM_EXCHANGE, "subscription.created", + { + event: "subscription.created", + exchange: exchange, + topic: topic + }); }; SubscriptionDefinition.prototype = { unsubscribe: function() { postal.configuration.bus.unsubscribe(this); + postal.publish(SYSTEM_EXCHANGE, "subscription.removed", + { + event: "subscription.removed", + exchange: this.exchange, + topic: this.topic + }); }, defer: function() { @@ -221,109 +235,131 @@ var bindingsResolver = { var localBus = { - subscriptions: {}, + subscriptions: {}, - wireTaps: [], + wireTaps: [], - publish: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); + publish: function(data, envelope) { + this.notifyTaps(data, envelope); - _.each(this.subscriptions[envelope.exchange], function(topic) { - _.each(topic, function(binding){ - if(postal.configuration.resolver.compare(binding.topic, envelope.topic)) { - if(_.all(binding.constraints, function(constraint) { return constraint(data); })) { - if(typeof binding.callback === 'function') { - binding.callback.apply(binding.context, [data, envelope]); - binding.onHandled(); - } - } - } - }); - }); - }, + _.each(this.subscriptions[envelope.exchange], function(topic) { + _.each(topic, function(binding){ + if(postal.configuration.resolver.compare(binding.topic, envelope.topic)) { + if(_.all(binding.constraints, function(constraint) { return constraint(data); })) { + if(typeof binding.callback === 'function') { + binding.callback.apply(binding.context, [data, envelope]); + binding.onHandled(); + } + } + } + }); + }); + }, - subscribe: function(subDef) { - var idx, found, fn; + subscribe: function(subDef) { + var idx, found, fn, exch, subs; - if(!this.subscriptions[subDef.exchange]) { - this.subscriptions[subDef.exchange] = {}; - } - if(!this.subscriptions[subDef.exchange][subDef.topic]) { - this.subscriptions[subDef.exchange][subDef.topic] = []; - } + exch = this.subscriptions[subDef.exchange] = this.subscriptions[subDef.exchange] || {}; + subs = this.subscriptions[subDef.exchange][subDef.topic] = this.subscriptions[subDef.exchange][subDef.topic] || []; - idx = this.subscriptions[subDef.exchange][subDef.topic].length - 1; - if(!_.any(this.subscriptions[subDef.exchange][subDef.topic], function(cfg) { return cfg === subDef; })) { - for(; idx >= 0; idx--) { - if(this.subscriptions[subDef.exchange][subDef.topic][idx].priority <= subDef.priority) { - this.subscriptions[subDef.exchange][subDef.topic].splice(idx + 1, 0, subDef); - found = true; - break; - } - } - if(!found) { - this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef); - } - } + idx = subs.length - 1; + if(!_.any(subs, function(cfg) { return cfg === subDef; })) { + for(; idx >= 0; idx--) { + if(subs[idx].priority <= subDef.priority) { + subs.splice(idx + 1, 0, subDef); + found = true; + break; + } + } + if(!found) { + subs.unshift(subDef); + } + } + }, - return _.bind(function() { this.unsubscribe(subDef); }, this); - }, + 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, - idx = 0; - for ( ; idx < len; idx++ ) { - if (this.subscriptions[config.exchange][config.topic][idx] === config) { - this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); - break; - } - } - } - }, + unsubscribe: function(config) { + if(this.subscriptions[config.exchange][config.topic]) { + var len = this.subscriptions[config.exchange][config.topic].length, + idx = 0; + for ( ; idx < len; idx++ ) { + if (this.subscriptions[config.exchange][config.topic][idx] === config) { + this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); + break; + } + } + } + }, - addWireTap: function(callback) { - this.wireTaps.push(callback); - return function() { - var idx = this.wireTaps.indexOf(callback); - if(idx !== -1) { - this.wireTaps.splice(idx,1); - } - }; - } + addWireTap: function(callback) { + var self = this; + self.wireTaps.push(callback); + return function() { + var idx = self.wireTaps.indexOf(callback); + if(idx !== -1) { + self.wireTaps.splice(idx,1); + } + }; + } }; var postal = { - configuration: { - bus: localBus, - resolver: bindingsResolver - }, + configuration: { + bus: localBus, + resolver: bindingsResolver + }, - channel: function(exchange, topic) { - var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 2 ? topic : exchange; - return new ChannelDefinition(exch, tpc); - }, + channel: function(exchange, topic) { + var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 2 ? topic : exchange; + return new ChannelDefinition(exch, tpc); + }, - subscribe: function(exchange, topic, callback) { - var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 3 ? topic : exchange, - callbk = arguments.length === 3 ? callback : topic; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); - }, + subscribe: function(exchange, topic, callback) { + var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 3 ? topic : exchange, + callbk = arguments.length === 3 ? callback : topic; + var channel = this.channel(exch, tpc); + return channel.subscribe(callbk); + }, - 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(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); + }, - addWireTap: function(callback) { - this.configuration.bus.addWireTap(callback); - } + addWireTap: function(callback) { + return this.configuration.bus.addWireTap(callback); + }, + + bindExchanges: function(sources, destinations) { + var subscriptions = []; + if(!_.isArray(sources)) { + sources = [sources]; + } + if(!_.isArray(destinations)) { + destinations = [destinations]; + } + _.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); + }) + ); + }); + }); + return subscriptions; + } }; return postal; }); \ No newline at end of file diff --git a/lib/browser/amd/postal.min.gz.js b/lib/browser/amd/postal.min.gz.js index 3d4deb9..f69704d 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 3419f72..3795591 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=function(){},f=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},g=function(){var b;return function(c){var d=!1;return a.isString(c)?(d=c===b,b=c):(d=a.isEqual(c,b),b=a.clone(c)),!d}},h=function(a,b){this.exchange=a,this.topic=b};h.prototype={subscribe:function(a){var b=new i(this.exchange,this.topic,a);return l.configuration.bus.subscribe(b),b},publish:function(b,c){var d=a.extend({exchange:this.exchange,timeStamp:new Date,topic:this.topic},c);l.configuration.bus.publish(b,d)}};var i=function(a,b,f){this.exchange=a,this.topic=b,this.callback=f,this.priority=c,this.constraints=[],this.maxCalls=d,this.onHandled=e,this.context=null};i.prototype={unsubscribe:function(){l.configuration.bus.unsubscribe(this)},defer:function(){var a=this.callback;return this.callback=function(b){setTimeout(a,0,b)},this},disposeAfter:function(b){if(a.isNaN(b)||b<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var c=this.onHandled,d=a.after(b,a.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){c.apply(this.context,arguments),d()},this},ignoreDuplicates:function(){return this.withConstraint(new g),this},whenHandledThenExecute:function(b){if(!a.isFunction(b))throw"Value provided to 'whenHandledThenExecute' must be a function";return this.onHandled=b,this},withConstraint:function(b){if(!a.isFunction(b))throw"Predicate constraint must be a function";return this.constraints.push(b),this},withConstraints:function(b){var c=this;return a.isArray(b)&&a.each(b,function(a){c.withConstraint(a)}),c},withContext:function(a){return this.context=a,this},withDebounce:function(b){if(a.isNaN(b))throw"Milliseconds must be a number";var c=this.callback;return this.callback=a.debounce(c,b),this},withDelay:function(b){if(a.isNaN(b))throw"Milliseconds must be a number";var c=this.callback;return this.callback=function(a){setTimeout(c,b,a)},this},withPriority:function(b){if(a.isNaN(b))throw"Priority must be a number";return this.priority=b,this},withThrottle:function(b){if(a.isNaN(b))throw"Milliseconds must be a number";var c=this.callback;return this.callback=a.throttle(c,b),this}};var j={cache:{},compare:function(a,b){if(this.cache[b]&&this.cache[b][a])return!0;var c=new RegExp("^"+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]*")}},k={subscriptions:{},wireTaps:[],publish:function(b,c){a.each(this.wireTaps,function(a){a(b,c)}),a.each(this.subscriptions[c.exchange],function(d){a.each(d,function(d){l.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(b){var c,d,e;this.subscriptions[b.exchange]||(this.subscriptions[b.exchange]={}),this.subscriptions[b.exchange][b.topic]||(this.subscriptions[b.exchange][b.topic]=[]),c=this.subscriptions[b.exchange][b.topic].length-1;if(!a.any(this.subscriptions[b.exchange][b.topic],function(a){return a===b})){for(;c>=0;c--)if(this.subscriptions[b.exchange][b.topic][c].priority<=b.priority){this.subscriptions[b.exchange][b.topic].splice(c+1,0,b),d=!0;break}d||this.subscriptions[b.exchange][b.topic].unshift(b)}return a.bind(function(){this.unsubscribe(b)},this)},unsubscribe:function(a){if(this.subscriptions[a.exchange][a.topic]){var b=this.subscriptions[a.exchange][a.topic].length,c=0;for(;c=0;c--)if(g[c].priority<=b.priority){g.splice(c+1,0,b),d=!0;break}d||g.unshift(b)}},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; idx--) { - if(this.subscriptions[subDef.exchange][subDef.topic][idx].priority <= subDef.priority) { - this.subscriptions[subDef.exchange][subDef.topic].splice(idx + 1, 0, subDef); - found = true; - break; - } - } - if(!found) { - this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef); - } - } + idx = subs.length - 1; + if(!_.any(subs, function(cfg) { return cfg === subDef; })) { + for(; idx >= 0; idx--) { + if(subs[idx].priority <= subDef.priority) { + subs.splice(idx + 1, 0, subDef); + found = true; + break; + } + } + if(!found) { + subs.unshift(subDef); + } + } + }, - return _.bind(function() { this.unsubscribe(subDef); }, this); - }, + 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, - idx = 0; - for ( ; idx < len; idx++ ) { - if (this.subscriptions[config.exchange][config.topic][idx] === config) { - this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); - break; - } - } - } - }, + unsubscribe: function(config) { + if(this.subscriptions[config.exchange][config.topic]) { + var len = this.subscriptions[config.exchange][config.topic].length, + idx = 0; + for ( ; idx < len; idx++ ) { + if (this.subscriptions[config.exchange][config.topic][idx] === config) { + this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); + break; + } + } + } + }, - addWireTap: function(callback) { - this.wireTaps.push(callback); - return function() { - var idx = this.wireTaps.indexOf(callback); - if(idx !== -1) { - this.wireTaps.splice(idx,1); - } - }; - } + addWireTap: function(callback) { + var self = this; + self.wireTaps.push(callback); + return function() { + var idx = self.wireTaps.indexOf(callback); + if(idx !== -1) { + self.wireTaps.splice(idx,1); + } + }; + } }; var postal = { - configuration: { - bus: localBus, - resolver: bindingsResolver - }, + configuration: { + bus: localBus, + resolver: bindingsResolver + }, - channel: function(exchange, topic) { - var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 2 ? topic : exchange; - return new ChannelDefinition(exch, tpc); - }, + channel: function(exchange, topic) { + var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 2 ? topic : exchange; + return new ChannelDefinition(exch, tpc); + }, - subscribe: function(exchange, topic, callback) { - var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 3 ? topic : exchange, - callbk = arguments.length === 3 ? callback : topic; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); - }, + subscribe: function(exchange, topic, callback) { + var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 3 ? topic : exchange, + callbk = arguments.length === 3 ? callback : topic; + var channel = this.channel(exch, tpc); + return channel.subscribe(callbk); + }, - 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(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); + }, - addWireTap: function(callback) { - this.configuration.bus.addWireTap(callback); - } + addWireTap: function(callback) { + return this.configuration.bus.addWireTap(callback); + }, + + bindExchanges: function(sources, destinations) { + var subscriptions = []; + if(!_.isArray(sources)) { + sources = [sources]; + } + if(!_.isArray(destinations)) { + destinations = [destinations]; + } + _.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); + }) + ); + }); + }); + return subscriptions; + } }; global.postal = postal; })(window); \ No newline at end of file diff --git a/lib/browser/standard/postal.min.gz.js b/lib/browser/standard/postal.min.gz.js index 799945f..0983cb9 100644 Binary files a/lib/browser/standard/postal.min.gz.js and b/lib/browser/standard/postal.min.gz.js differ diff --git a/lib/browser/standard/postal.min.js b/lib/browser/standard/postal.min.js index 9a736d7..8100fd0 100644 --- a/lib/browser/standard/postal.min.js +++ b/lib/browser/standard/postal.min.js @@ -1 +1 @@ -(function(a,b){var c="/",d=50,e=0,f=function(){},g=function(a){var b={envelope:{}},d;switch(a.length){case 3:typeof a[1]=="Object"&&typeof a[2]=="Object"?(b.envelope.exchange=c,b.envelope.topic=a[0],b.payload=a[1],d=b.envelope,b.envelope=_.extend(d,a[2])):(b.envelope.exchange=a[0],b.envelope.topic=a[1],b.payload=a[2]);break;case 4:b.envelope.exchange=a[0],b.envelope.topic=a[1],b.payload=a[2],d=b.envelope,b.envelope=_.extend(d,a[3]);break;default:b.envelope.exchange=c,b.envelope.topic=a[0],b.payload=a[1]}return b},h=function(){var a;return function(b){var c=!1;return _.isString(b)?(c=b===a,a=b):(c=_.isEqual(b,a),a=_.clone(b)),!c}},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(a,b){var c=_.extend({exchange:this.exchange,timeStamp:new Date,topic:this.topic},b);m.configuration.bus.publish(a,c)}};var j=function(a,b,c){this.exchange=a,this.topic=b,this.callback=c,this.priority=d,this.constraints=[],this.maxCalls=e,this.onHandled=f,this.context=null};j.prototype={unsubscribe:function(){m.configuration.bus.unsubscribe(this)},defer:function(){var a=this.callback;return this.callback=function(b){setTimeout(a,0,b)},this},disposeAfter:function(a){if(_.isNaN(a)||a<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var b=this.onHandled,c=_.after(a,_.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){b.apply(this.context,arguments),c()},this},ignoreDuplicates:function(){return this.withConstraint(new h),this},whenHandledThenExecute:function(a){if(!_.isFunction(a))throw"Value provided to 'whenHandledThenExecute' must be a function";return this.onHandled=a,this},withConstraint:function(a){if(!_.isFunction(a))throw"Predicate constraint must be a function";return this.constraints.push(a),this},withConstraints:function(a){var b=this;return _.isArray(a)&&_.each(a,function(a){b.withConstraint(a)}),b},withContext:function(a){return this.context=a,this},withDebounce:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=_.debounce(b,a),this},withDelay:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=function(c){setTimeout(b,a,c)},this},withPriority:function(a){if(_.isNaN(a))throw"Priority must be a number";return this.priority=a,this},withThrottle:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=_.throttle(b,a),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(a,b){_.each(this.wireTaps,function(c){c(a,b)}),_.each(this.subscriptions[b.exchange],function(c){_.each(c,function(c){m.configuration.resolver.compare(c.topic,b.topic)&&_.all(c.constraints,function(b){return b(a)})&&typeof c.callback=="function"&&(c.callback.apply(c.context,[a,b]),c.onHandled())})})},subscribe:function(a){var b,c,d;this.subscriptions[a.exchange]||(this.subscriptions[a.exchange]={}),this.subscriptions[a.exchange][a.topic]||(this.subscriptions[a.exchange][a.topic]=[]),b=this.subscriptions[a.exchange][a.topic].length-1;if(!_.any(this.subscriptions[a.exchange][a.topic],function(b){return b===a})){for(;b>=0;b--)if(this.subscriptions[a.exchange][a.topic][b].priority<=a.priority){this.subscriptions[a.exchange][a.topic].splice(b+1,0,a),c=!0;break}c||this.subscriptions[a.exchange][a.topic].unshift(a)}return _.bind(function(){this.unsubscribe(a)},this)},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; idx--) { - if(this.subscriptions[subDef.exchange][subDef.topic][idx].priority <= subDef.priority) { - this.subscriptions[subDef.exchange][subDef.topic].splice(idx + 1, 0, subDef); - found = true; - break; - } - } - if(!found) { - this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef); - } - } + idx = subs.length - 1; + if(!_.any(subs, function(cfg) { return cfg === subDef; })) { + for(; idx >= 0; idx--) { + if(subs[idx].priority <= subDef.priority) { + subs.splice(idx + 1, 0, subDef); + found = true; + break; + } + } + if(!found) { + subs.unshift(subDef); + } + } + }, - return _.bind(function() { this.unsubscribe(subDef); }, this); - }, + 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, - idx = 0; - for ( ; idx < len; idx++ ) { - if (this.subscriptions[config.exchange][config.topic][idx] === config) { - this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); - break; - } - } - } - }, + unsubscribe: function(config) { + if(this.subscriptions[config.exchange][config.topic]) { + var len = this.subscriptions[config.exchange][config.topic].length, + idx = 0; + for ( ; idx < len; idx++ ) { + if (this.subscriptions[config.exchange][config.topic][idx] === config) { + this.subscriptions[config.exchange][config.topic].splice( idx, 1 ); + break; + } + } + } + }, - addWireTap: function(callback) { - this.wireTaps.push(callback); - return function() { - var idx = this.wireTaps.indexOf(callback); - if(idx !== -1) { - this.wireTaps.splice(idx,1); - } - }; - } + addWireTap: function(callback) { + var self = this; + self.wireTaps.push(callback); + return function() { + var idx = self.wireTaps.indexOf(callback); + if(idx !== -1) { + self.wireTaps.splice(idx,1); + } + }; + } }; var postal = { - configuration: { - bus: localBus, - resolver: bindingsResolver - }, + configuration: { + bus: localBus, + resolver: bindingsResolver + }, - channel: function(exchange, topic) { - var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 2 ? topic : exchange; - return new ChannelDefinition(exch, tpc); - }, + channel: function(exchange, topic) { + var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 2 ? topic : exchange; + return new ChannelDefinition(exch, tpc); + }, - subscribe: function(exchange, topic, callback) { - var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, - tpc = arguments.length === 3 ? topic : exchange, - callbk = arguments.length === 3 ? callback : topic; - var channel = this.channel(exch, tpc); - return channel.subscribe(callbk); - }, + subscribe: function(exchange, topic, callback) { + var exch = arguments.length === 3 ? exchange : DEFAULT_EXCHANGE, + tpc = arguments.length === 3 ? topic : exchange, + callbk = arguments.length === 3 ? callback : topic; + var channel = this.channel(exch, tpc); + return channel.subscribe(callbk); + }, - 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(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); + }, - addWireTap: function(callback) { - this.configuration.bus.addWireTap(callback); - } + addWireTap: function(callback) { + return this.configuration.bus.addWireTap(callback); + }, + + bindExchanges: function(sources, destinations) { + var subscriptions = []; + if(!_.isArray(sources)) { + sources = [sources]; + } + if(!_.isArray(destinations)) { + destinations = [destinations]; + } + _.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); + }) + ); + }); + }); + return subscriptions; + } }; module.exports = postal; \ No newline at end of file