mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-04-09 01:21:07 +00:00
Updated build artifacts
This commit is contained in:
parent
5946df996f
commit
fbcfe70291
11 changed files with 617 additions and 437 deletions
|
|
@ -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; });
|
||||
|
|
@ -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);
|
||||
Binary file not shown.
|
|
@ -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; });
|
||||
Binary file not shown.
2
lib/browser/amd/postal.min.js
vendored
2
lib/browser/amd/postal.min.js
vendored
File diff suppressed because one or more lines are too long
Binary file not shown.
|
|
@ -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);
|
||||
Binary file not shown.
2
lib/browser/standard/postal.min.js
vendored
2
lib/browser/standard/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -9,6 +9,7 @@ var _ = require('underscore');
|
|||
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;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = postal;
|
||||
Loading…
Reference in a new issue