mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
Adding subscribtion-created, subcsription-removed event publishes to wiretaps on subscribe/unsubscribe.
This commit is contained in:
parent
14864b5135
commit
8d0c8d361e
2 changed files with 44 additions and 5 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
|
|
|||
Loading…
Reference in a new issue