diff --git a/spec/Postal.spec.js b/spec/Postal.spec.js index cf16168..a178ace 100644 --- a/spec/Postal.spec.js +++ b/spec/Postal.spec.js @@ -2,14 +2,29 @@ QUnit.specify("postal.js", function(){ describe("Postal", function(){ var subscription, sub, - channel; + channel, + caughtSubscribeEvent = false, + caughtUnsubscribeEvent = false; + describe("when creating basic subscription", function() { + var systemSubscription = {}; before(function(){ + + systemSubscription = postal.subscribe( "postal", "subscription.created", function(x){ + console.log("on subscription " + JSON.stringify(x)); + if( x.event && + x.event == "subscription.created" && + x.exchange == "MyExchange" && + x.topic == "MyTopic") { + 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(){ @@ -36,11 +51,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(caughtSubscribeEvent).isTrue(); + }); }); describe("when unsubscribing", function() { var subExistsBefore = false, subExistsAfter = true; + var systemSubscription = {}; before(function(){ + systemSubscription = postal.subscribe( "postal", "subscription.*", function(x){ + console.log("on unsubscription " + JSON.stringify(x)); + if( x.event && + x.event == "subscription.removed" && + x.exchange == "MyExchange" && + x.topic == "MyTopic") { + caughtUnsubscribeEvent = true; + }; + }); subscription = postal.channel("MyExchange","MyTopic") .subscribe(function() { }); subExistsBefore = postal.configuration.bus.subscriptions.MyExchange.MyTopic[0] !== undefined; @@ -48,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(){ @@ -56,6 +85,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(caughtUnsubscribeEvent).isTrue(); + }); }); describe("When publishing a message", function(){ var msgReceivedCnt = 0, diff --git a/src/main/Constants.js b/src/main/Constants.js index 447453a..a44bbe8 100644 --- a/src/main/Constants.js +++ b/src/main/Constants.js @@ -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; diff --git a/src/main/LocalBus.js b/src/main/LocalBus.js index 6eeb0de..2a4734e 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){ @@ -50,6 +48,12 @@ var localBus = { 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, diff --git a/src/main/SubscriptionDefinition.js b/src/main/SubscriptionDefinition.js index 9f55dd7..cab8af3 100644 --- a/src/main/SubscriptionDefinition.js +++ b/src/main/SubscriptionDefinition.js @@ -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() {