Changing subscription notifications to a message on the postal channel

This commit is contained in:
Alex Robson 2012-02-22 22:33:15 -05:00
parent 8d0c8d361e
commit 76c0cf337b
4 changed files with 35 additions and 24 deletions

View file

@ -3,22 +3,28 @@ QUnit.specify("postal.js", function(){
var subscription,
sub,
channel,
gotSubscriptionFromTap;
caughtSubscribeEvent = false,
caughtUnsubscribeEvent = false;
describe("when creating basic subscription", function() {
var systemSubscription = {};
before(function(){
postal.addWireTap(function(x,y){
systemSubscription = postal.subscribe( "postal", "subscription.created", function(x){
console.log("on subscription " + JSON.stringify(x));
if( x.event &&
x.event == "subscription-created" &&
x.event == "subscription.created" &&
x.exchange == "MyExchange" &&
x.topic == "MyTopic") {
gotSubscriptionFromTap = true;
}
caughtSubscribeEvent = true;
};
});
subscription = postal.channel("MyExchange","MyTopic")
.subscribe(function() { });
sub = postal.configuration.bus.subscriptions.MyExchange.MyTopic[0];
});
after(function(){
systemSubscription.unsubscribe();
postal.configuration.bus.subscriptions = {};
});
it("should create an exchange called MyExchange", function(){
@ -46,22 +52,22 @@ QUnit.specify("postal.js", function(){
assert(sub.context).isNull();
});
it("should have captured subscription creation event in wire-tap", function() {
assert(gotSubscriptionFromTap).isTrue();
assert(caughtSubscribeEvent).isTrue();
});
});
describe("when unsubscribing", function() {
var subExistsBefore = false,
subExistsAfter = true,
gotUnsubscriptionFromTap = false;
subExistsAfter = true;
var systemSubscription = {};
before(function(){
postal.addWireTap(function(x,y){
systemSubscription = postal.subscribe( "postal", "subscription.*", function(x){
console.log("on unsubscription " + JSON.stringify(x));
if( x.event &&
x.event == "subscription-removed" &&
x.event == "subscription.removed" &&
x.exchange == "MyExchange" &&
x.topic == "MyTopic") {
gotUnsubscriptionFromTap = true;
}
caughtUnsubscribeEvent = true;
};
});
subscription = postal.channel("MyExchange","MyTopic")
.subscribe(function() { });
@ -70,6 +76,7 @@ QUnit.specify("postal.js", function(){
subExistsAfter = postal.configuration.bus.subscriptions.MyExchange.MyTopic.length !== 0;
});
after(function(){
systemSubscription.unsubscribe();
postal.configuration.bus.subscriptions = {};
});
it("subscription should exist before unsubscribe", function(){
@ -79,7 +86,7 @@ QUnit.specify("postal.js", function(){
assert(subExistsAfter).isFalse();
});
it("should have captured unsubscription creation event in wire-tap", function() {
assert(gotUnsubscriptionFromTap).isTrue();
assert(caughtUnsubscribeEvent).isTrue();
});
});
describe("When publishing a message", function(){

View file

@ -1,6 +1,7 @@
var DEFAULT_EXCHANGE = "/",
DEFAULT_PRIORITY = 50,
DEFAULT_DISPOSEAFTER = 0,
SYSTEM_EXCHANGE = "postal",
NO_OP = function() { },
parsePublishArgs = function(args) {
var parsed = { envelope: { } }, env;

View file

@ -42,11 +42,6 @@ var localBus = {
}
if(!found) {
this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef);
this.notifyTaps({
event: "subscription-created",
exchange: subDef.exchange,
topic: subDef.topic
}, {});
}
}
@ -63,11 +58,6 @@ var localBus = {
if(this.subscriptions[config.exchange][config.topic]) {
var len = this.subscriptions[config.exchange][config.topic].length,
idx = 0;
this.notifyTaps({
event: "subscription-removed",
exchange: config.exchange,
topic: config.topic
}, {});
for ( ; idx < len; idx++ ) {
if (this.subscriptions[config.exchange][config.topic][idx] === config) {
this.subscriptions[config.exchange][config.topic].splice( idx, 1 );

View file

@ -7,11 +7,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() {