diff --git a/spec/Postal.spec.js b/spec/Postal.spec.js index cf16168..9459bdb 100644 --- a/spec/Postal.spec.js +++ b/spec/Postal.spec.js @@ -2,9 +2,18 @@ QUnit.specify("postal.js", function(){ describe("Postal", function(){ var subscription, sub, - channel; + channel, + gotSubscriptionFromTap; describe("when creating basic subscription", function() { before(function(){ + postal.addWireTap(function(x,y){ + if( x.event && + x.event == "subscription-created" && + x.exchange == "MyExchange" && + x.topic == "MyTopic") { + gotSubscriptionFromTap = true; + } + }); subscription = postal.channel("MyExchange","MyTopic") .subscribe(function() { }); sub = postal.configuration.bus.subscriptions.MyExchange.MyTopic[0]; @@ -36,11 +45,24 @@ QUnit.specify("postal.js", function(){ it("should have defaulted the subscription context value", function() { assert(sub.context).isNull(); }); + it("should have captured subscription creation event in wire-tap", function() { + assert(gotSubscriptionFromTap).isTrue(); + }); }); describe("when unsubscribing", function() { var subExistsBefore = false, - subExistsAfter = true; + subExistsAfter = true, + gotUnsubscriptionFromTap = false; + before(function(){ + postal.addWireTap(function(x,y){ + if( x.event && + x.event == "subscription-removed" && + x.exchange == "MyExchange" && + x.topic == "MyTopic") { + gotUnsubscriptionFromTap = true; + } + }); subscription = postal.channel("MyExchange","MyTopic") .subscribe(function() { }); subExistsBefore = postal.configuration.bus.subscriptions.MyExchange.MyTopic[0] !== undefined; @@ -56,6 +78,9 @@ QUnit.specify("postal.js", function(){ it("subscription should not exist after unsubscribe", function(){ assert(subExistsAfter).isFalse(); }); + it("should have captured unsubscription creation event in wire-tap", function() { + assert(gotUnsubscriptionFromTap).isTrue(); + }); }); describe("When publishing a message", function(){ var msgReceivedCnt = 0, diff --git a/src/main/LocalBus.js b/src/main/LocalBus.js index 6eeb0de..ecc01df 100644 --- a/src/main/LocalBus.js +++ b/src/main/LocalBus.js @@ -5,9 +5,7 @@ var localBus = { wireTaps: [], publish: function(data, envelope) { - _.each(this.wireTaps,function(tap) { - tap(data, envelope); - }); + this.notifyTaps(data, envelope); _.each(this.subscriptions[envelope.exchange], function(topic) { _.each(topic, function(binding){ @@ -44,16 +42,32 @@ var localBus = { } if(!found) { this.subscriptions[subDef.exchange][subDef.topic].unshift(subDef); + this.notifyTaps({ + event: "subscription-created", + exchange: subDef.exchange, + topic: subDef.topic + }, {}); } } 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; + 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 );