Refactored where subscription created and removed messages are published. Added unsubscribe to postal object.

This commit is contained in:
ifandelse 2014-01-24 00:09:12 -05:00
parent ab978b9d3e
commit eaccef227e
6 changed files with 63 additions and 78 deletions

View file

@ -66,7 +66,7 @@
};
ChannelDefinition.prototype.subscribe = function () {
return arguments.length === 1 ? new SubscriptionDefinition(this.channel, arguments[0].topic, arguments[0].callback) : new SubscriptionDefinition(this.channel, arguments[0], arguments[1]);
return postal.subscribe(arguments.length === 1 ? new SubscriptionDefinition(this.channel, arguments[0].topic, arguments[0].callback) : new SubscriptionDefinition(this.channel, arguments[0], arguments[1]));
};
ChannelDefinition.prototype.publish = function () {
@ -93,32 +93,13 @@
this.callback = callback;
this.constraints = [];
this.context = null;
postal.configuration.bus.publish({
channel: postal.configuration.SYSTEM_CHANNEL,
topic: "subscription.created",
data: {
event: "subscription.created",
channel: channel,
topic: topic
}
});
postal.configuration.bus.subscribe(this);
};
SubscriptionDefinition.prototype = {
unsubscribe: function () {
if (!this.inactive) {
this.inactive = true;
postal.configuration.bus.unsubscribe(this);
postal.configuration.bus.publish({
channel: postal.configuration.SYSTEM_CHANNEL,
topic: "subscription.removed",
data: {
event: "subscription.removed",
channel: this.channel,
topic: this.topic
}
});
postal.unsubscribe(this);
}
},
@ -357,7 +338,7 @@
unSubQueue.push(config);
return;
}
if (this.subscriptions[config.channel][config.topic]) {
if (this.subscriptions[config.channel] && this.subscriptions[config.channel][config.topic]) {
var len = this.subscriptions[config.channel][config.topic].length,
idx = 0;
while (idx < len) {
@ -388,7 +369,17 @@
},
subscribe: function (options) {
return new SubscriptionDefinition(options.channel || postal.configuration.DEFAULT_CHANNEL, options.topic, options.callback);
var subDef = new SubscriptionDefinition(options.channel || postal.configuration.DEFAULT_CHANNEL, options.topic, options.callback);
postal.configuration.bus.publish({
channel: postal.configuration.SYSTEM_CHANNEL,
topic: "subscription.created",
data: {
event: "subscription.created",
channel: subDef.channel,
topic: subDef.topic
}
});
return postal.configuration.bus.subscribe(subDef);
},
publish: function (envelope) {
@ -396,6 +387,19 @@
return postal.configuration.bus.publish(envelope);
},
unsubscribe: function (subdef) {
postal.configuration.bus.unsubscribe(subdef);
postal.configuration.bus.publish({
channel: postal.configuration.SYSTEM_CHANNEL,
topic: "subscription.removed",
data: {
event: "subscription.removed",
channel: subdef.channel,
topic: subdef.topic
}
});
},
addWireTap: function (callback) {
return this.configuration.bus.addWireTap(callback);
},

2
lib/postal.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -6,28 +6,12 @@
var SubscriptionDefinition = postal.SubscriptionDefinition;
describe( "SubscriptionDefinition", function () {
describe( "When initializing SubscriptionDefinition", function () {
var sDef,
caughtSubscribeEvent,
systemSubscription;
var sDef;
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.created",
callback : function ( data, envelope ) {
if ( data.event &&
data.event === "subscription.created" &&
data.channel === "SubDefTestChannel" &&
data.topic === "SubDefTestTopic" ) {
caughtSubscribeEvent = true;
}
}
} );
sDef = new SubscriptionDefinition( "SubDefTestChannel", "SubDefTestTopic", NO_OP );
} );
after( function () {
sDef.unsubscribe();
systemSubscription.unsubscribe();
caughtSubscribeEvent = false;
} );
it( "should set the channel to SubDefTestChannel", function () {
expect( sDef.channel ).to.be( "SubDefTestChannel" );
@ -44,9 +28,6 @@
it( "should default the context", function () {
expect( sDef.context ).to.be( null );
} );
it( "should fire the subscription.created message", function () {
expect( caughtSubscribeEvent ).to.be( true );
} );
} );
describe( "When setting distinctUntilChanged", function () {
@ -67,10 +48,12 @@
} );
describe( "When adding multiple constraints", function () {
var sDefc = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).withConstraints( [function () {
}, function () {
}, function () {
}] );
var sDefc = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP )
.withConstraints( [
function () {},
function () {},
function () {}
]);
it( "Should add a constraint", function () {
expect( sDefc.constraints.length ).to.be( 3 );
@ -87,20 +70,14 @@
return true;
} );
postal.publish( { channel : "TestChannel", topic : "TestTopic", data : "Oh, hai"} );
it( "Should set context", function () {
expect( sDefd.context ).to.be( obj );
} );
it( "Should apply context to predicate/constraint", function () {
expect( name ).to.be( "Rose" );
} );
} );
describe( "When calling subscribe to set the callback", function () {
var sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ),
fn = function () {
};
fn = function () {};
sDefe.subscribe( fn );
it( "Should set the callback", function () {

View file

@ -16,7 +16,17 @@ postal = {
},
subscribe : function ( options ) {
return new SubscriptionDefinition( options.channel || postal.configuration.DEFAULT_CHANNEL, options.topic, options.callback );
var subDef = new SubscriptionDefinition( options.channel || postal.configuration.DEFAULT_CHANNEL, options.topic, options.callback );
postal.configuration.bus.publish( {
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.created",
data : {
event : "subscription.created",
channel : subDef.channel,
topic : subDef.topic
}
} );
return postal.configuration.bus.subscribe( subDef );
},
publish : function ( envelope ) {
@ -24,6 +34,19 @@ postal = {
return postal.configuration.bus.publish( envelope );
},
unsubscribe: function( subdef ) {
postal.configuration.bus.unsubscribe( subdef );
postal.configuration.bus.publish( {
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.removed",
data : {
event : "subscription.removed",
channel : subdef.channel,
topic : subdef.topic
}
});
},
addWireTap : function ( callback ) {
return this.configuration.bus.addWireTap( callback );
},

View file

@ -4,9 +4,9 @@ var ChannelDefinition = function ( channelName ) {
};
ChannelDefinition.prototype.subscribe = function () {
return arguments.length === 1 ?
return postal.subscribe(arguments.length === 1 ?
new SubscriptionDefinition( this.channel, arguments[0].topic, arguments[0].callback ) :
new SubscriptionDefinition( this.channel, arguments[0], arguments[1] );
new SubscriptionDefinition( this.channel, arguments[0], arguments[1] ));
};
ChannelDefinition.prototype.publish = function () {

View file

@ -12,32 +12,13 @@ var SubscriptionDefinition = function ( channel, topic, callback ) {
this.callback = callback;
this.constraints = [];
this.context = null;
postal.configuration.bus.publish( {
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.created",
data : {
event : "subscription.created",
channel : channel,
topic : topic
}
} );
postal.configuration.bus.subscribe( this );
};
SubscriptionDefinition.prototype = {
unsubscribe : function () {
if ( !this.inactive ) {
this.inactive = true;
postal.configuration.bus.unsubscribe( this );
postal.configuration.bus.publish( {
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.removed",
data : {
event : "subscription.removed",
channel : this.channel,
topic : this.topic
}
} );
postal.unsubscribe( this );
}
},