diff --git a/spec/Postal.spec.js b/spec/Postal.spec.js
index 877f880..2d02e12 100644
--- a/spec/Postal.spec.js
+++ b/spec/Postal.spec.js
@@ -535,8 +535,8 @@ describe( "Postal", function () {
var msgReceivedCnt = 0,
msgData;
before( function () {
- channel = postal.channel( { channel : "MyGlobalChannel", topic : "MyTopic" } );
- subscription = channel.subscribe( function ( data ) {
+ channel = postal.channel( "MyGlobalChannel" );
+ subscription = channel.subscribe( "MyTopic", function ( data ) {
msgReceivedCnt++;
msgData = data;
} );
@@ -604,43 +604,7 @@ describe( "Postal", function () {
describe( "With no channel name provided", function () {
describe( "Using string argument", function () {
before( function () {
- gch = postal.channel( "SomeTopic" );
- } );
- after( function () {
- gch = undefined;
- } );
- it( "channel should be of type ChannelDefinition", function () {
- expect( gch instanceof ChannelDefinition ).to.be.ok();
- } );
- it( "should set channel name to DEFAULT_CHANNEL", function () {
- expect( gch.channel ).to.be( DEFAULT_CHANNEL );
- } );
- it( "should set topic to SomeTopic", function () {
- expect( gch._topic ).to.be( "SomeTopic" );
- } );
- } );
- describe( "Using options (object) argument", function () {
- before( function () {
- gch = postal.channel( { topic : "SomeTopic" } );
- } );
- after( function () {
- gch = undefined;
- } );
- it( "channel should be of type ChannelDefinition", function () {
- expect( gch instanceof ChannelDefinition ).to.be.ok();
- } );
- it( "should set channel name to DEFAULT_CHANNEL", function () {
- expect( gch.channel ).to.be( DEFAULT_CHANNEL );
- } );
- it( "should set topic to SomeTopic", function () {
- expect( gch._topic ).to.be( "SomeTopic" );
- } );
- } );
- } );
- describe( "With channel name provided", function () {
- describe( "Using string arguments", function () {
- before( function () {
- gch = postal.channel( "SomeChannel", "SomeTopic" );
+ gch = postal.channel( "SomeChannel" );
} );
after( function () {
gch = undefined;
@@ -651,26 +615,6 @@ describe( "Postal", function () {
it( "should set channel name to SomeChannel", function () {
expect( gch.channel ).to.be( "SomeChannel" );
} );
- it( "should set topic to SomeTopic", function () {
- expect( gch._topic ).to.be( "SomeTopic" );
- } );
- } );
- describe( "Using options (object) argument", function () {
- before( function () {
- gch = postal.channel( { channel : "SomeChannel", topic : "SomeTopic" } );
- } );
- after( function () {
- gch = undefined;
- } );
- it( "channel should be of type ChannelDefinition", function () {
- expect( gch instanceof ChannelDefinition ).to.be.ok();
- } );
- it( "should set channel name to SomeChannel", function () {
- expect( gch.channel ).to.be( "SomeChannel" );
- } );
- it( "should set topic to SomeTopic", function () {
- expect( gch._topic ).to.be( "SomeTopic" );
- } );
} );
} );
} );
@@ -705,124 +649,6 @@ describe( "Postal", function () {
expect( wireTapEnvelope[0].topic ).to.be( "Oh.Hai.There" );
} );
} );
- describe( "When binding channel - one source to one destination", function () {
- describe( "with only channel values provided", function () {
- var destData = [],
- destEnv = [],
- linkages;
- before( function () {
- linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel" } );
- subscription = postal.subscribe( { channel : "destinationChannel", topic : "Oh.Hai.There", callback : function ( data, env ) {
- destData.push( data );
- destEnv.push( env );
- }} );
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- linkages[0].unsubscribe();
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- } );
- after( function () {
- postal.utils.reset();
- } );
- it( "linked subscription should only have been invoked once", function () {
- expect( destData.length ).to.be( 1 );
- expect( destEnv.length ).to.be( 1 );
- } );
- it( "linked subscription data should match expected results", function () {
- expect( destData[0].data ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
- } );
- it( "linked subscription envelope should match expected results", function () {
- expect( destEnv[0].channel ).to.be( "destinationChannel" );
- expect( destEnv[0].topic ).to.be( "Oh.Hai.There" );
- } );
- } );
- describe( "with channel and static topic values provided", function () {
- var destData = [],
- destEnv = [],
- linkages;
- before( function () {
- linkages = postal.linkChannels( { channel : "sourceChannel", topic : "Oh.Hai.There" }, { channel : "destinationChannel", topic : "kthxbye" } );
- subscription = postal.subscribe( { channel : "destinationChannel", topic : "kthxbye", callback : function ( data, env ) {
- destData.push( data );
- destEnv.push( env );
- }} );
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- linkages[0].unsubscribe();
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- } );
- after( function () {
- postal.utils.reset();
- } );
- it( "linked subscription should only have been invoked once", function () {
- expect( destData.length ).to.be( 1 );
- expect( destEnv.length ).to.be( 1 );
- } );
- it( "linked subscription data should match expected results", function () {
- expect( destData[0].data ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
- } );
- it( "linked subscription envelope should match expected results", function () {
- expect( destEnv[0].channel ).to.be( "destinationChannel" );
- expect( destEnv[0].topic ).to.be( "kthxbye" );
- } );
- } );
- describe( "with channel and topic transform values provided", function () {
- var destData = [],
- destEnv = [],
- linkages;
- before( function () {
- linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel", topic : function ( tpc ) {
- return "NewTopic." + tpc;
- } } );
- subscription = postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : function ( data, env ) {
- destData.push( data );
- destEnv.push( env );
- }} );
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- linkages[0].unsubscribe();
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- } );
- after( function () {
- postal.utils.reset();
- } );
- it( "linked subscription should only have been invoked once", function () {
- expect( destData.length ).to.be( 1 );
- expect( destEnv.length ).to.be( 1 );
- } );
- it( "linked subscription data should match expected results", function () {
- expect( destData[0].data ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
- } );
- it( "linked subscription envelope should match expected results", function () {
- expect( destEnv[0].channel ).to.be( "destinationChannel" );
- expect( destEnv[0].topic ).to.be( "NewTopic.Oh.Hai.There" );
- } );
- } );
- } );
- describe( "When binding channel - one source to multiple destinations", function () {
- var destData = [],
- destEnv = [],
- callback = function ( data, env ) {
- destData.push( data );
- destEnv.push( env );
- };
-
- before( function () {
- linkages = postal.linkChannels(
- { channel : "sourceChannel", topic: "Oh.Hai.There" },
- [
- { channel : "destinationChannel", topic: "NewTopic.Oh.Hai" },
- { channel : "destinationChannel", topic: "NewTopic.Oh.Hai.There" }
- ]);
- postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai", callback : callback} );
- postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : callback } );
- postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
- } );
- after( function () {
- postal.utils.reset();
- } );
- it( "linked subscriptions should each have been called once", function () {
- expect( destData.length ).to.be( 2 );
- expect( destEnv.length ).to.be( 2 );
- } );
- });
describe( "When calling postal.utils.reset", function () {
var resolver;
before( function () {
diff --git a/spec/index.html b/spec/index.html
index 06a804d..e659ae7 100644
--- a/spec/index.html
+++ b/spec/index.html
@@ -31,6 +31,7 @@
+
diff --git a/spec/linkedChannels.spec.js b/spec/linkedChannels.spec.js
new file mode 100644
index 0000000..79b3b9d
--- /dev/null
+++ b/spec/linkedChannels.spec.js
@@ -0,0 +1,120 @@
+describe("Linked Channels", function(){
+ describe( "When binding channel - one source to one destination", function () {
+ describe( "with only channel values provided", function () {
+ var destData = [],
+ destEnv = [],
+ linkages;
+ before( function () {
+ linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel" } );
+ subscription = postal.subscribe( { channel : "destinationChannel", topic : "Oh.Hai.There", callback : function ( data, env ) {
+ destData.push( data );
+ destEnv.push( env );
+ }} );
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ linkages[0].unsubscribe();
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ } );
+ after( function () {
+ postal.utils.reset();
+ } );
+ it( "linked subscription should only have been invoked once", function () {
+ expect( destData.length ).to.be( 1 );
+ expect( destEnv.length ).to.be( 1 );
+ } );
+ it( "linked subscription data should match expected results", function () {
+ expect( destData[0].data ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
+ } );
+ it( "linked subscription envelope should match expected results", function () {
+ expect( destEnv[0].channel ).to.be( "destinationChannel" );
+ expect( destEnv[0].topic ).to.be( "Oh.Hai.There" );
+ } );
+ } );
+ describe( "with channel and static topic values provided", function () {
+ var destData = [],
+ destEnv = [],
+ linkages;
+ before( function () {
+ linkages = postal.linkChannels( { channel : "sourceChannel", topic : "Oh.Hai.There" }, { channel : "destinationChannel", topic : "kthxbye" } );
+ subscription = postal.subscribe( { channel : "destinationChannel", topic : "kthxbye", callback : function ( data, env ) {
+ destData.push( data );
+ destEnv.push( env );
+ }} );
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ linkages[0].unsubscribe();
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ } );
+ after( function () {
+ postal.utils.reset();
+ } );
+ it( "linked subscription should only have been invoked once", function () {
+ expect( destData.length ).to.be( 1 );
+ expect( destEnv.length ).to.be( 1 );
+ } );
+ it( "linked subscription data should match expected results", function () {
+ expect( destData[0].data ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
+ } );
+ it( "linked subscription envelope should match expected results", function () {
+ expect( destEnv[0].channel ).to.be( "destinationChannel" );
+ expect( destEnv[0].topic ).to.be( "kthxbye" );
+ } );
+ } );
+ describe( "with channel and topic transform values provided", function () {
+ var destData = [],
+ destEnv = [],
+ linkages;
+ before( function () {
+ linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel", topic : function ( tpc ) {
+ return "NewTopic." + tpc;
+ } } );
+ subscription = postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : function ( data, env ) {
+ destData.push( data );
+ destEnv.push( env );
+ }} );
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ linkages[0].unsubscribe();
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ } );
+ after( function () {
+ postal.utils.reset();
+ } );
+ it( "linked subscription should only have been invoked once", function () {
+ expect( destData.length ).to.be( 1 );
+ expect( destEnv.length ).to.be( 1 );
+ } );
+ it( "linked subscription data should match expected results", function () {
+ expect( destData[0].data ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
+ } );
+ it( "linked subscription envelope should match expected results", function () {
+ expect( destEnv[0].channel ).to.be( "destinationChannel" );
+ expect( destEnv[0].topic ).to.be( "NewTopic.Oh.Hai.There" );
+ } );
+ } );
+ } );
+ describe( "When binding channel - one source to multiple destinations", function () {
+ var destData = [],
+ destEnv = [],
+ callback = function ( data, env ) {
+ destData.push( data );
+ destEnv.push( env );
+ };
+
+ before( function () {
+ linkages = postal.linkChannels(
+ { channel : "sourceChannel", topic: "Oh.Hai.There" },
+ [
+ { channel : "destinationChannel", topic: "NewTopic.Oh.Hai" },
+ { channel : "destinationChannel", topic: "NewTopic.Oh.Hai.There" }
+ ]);
+ postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai", callback : callback} );
+ postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : callback } );
+ postal.publish( "sourceChannel", "Oh.Hai.There", { data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
+ } );
+ after( function () {
+ postal.utils.reset();
+ } );
+ it( "linked subscriptions should each have been called once", function () {
+ expect( destData.length ).to.be( 2 );
+ expect( destEnv.length ).to.be( 2 );
+ } );
+ });
+});
\ No newline at end of file
diff --git a/src/Api.js b/src/Api.js
index 79812fc..2db4e85 100644
--- a/src/Api.js
+++ b/src/Api.js
@@ -1,52 +1,4 @@
-var publishPicker = {
- "1" : function ( envelope ) {
- if ( !envelope ) {
- throw new Error( "publishing from the 'global' postal.publish call requires a valid envelope." );
- }
- envelope.channel = envelope.channel || DEFAULT_CHANNEL;
- envelope.timeStamp = new Date();
- postal.configuration.bus.publish( envelope );
- return envelope;
- },
- "2" : function ( topic, data ) {
- var envelope = { channel : DEFAULT_CHANNEL, topic : topic, timeStamp : new Date(), data : data };
- postal.configuration.bus.publish( envelope );
- return envelope;
- },
- "3" : function ( channel, topic, data ) {
- var envelope = { channel : channel, topic : topic, timeStamp : new Date(), data : data };
- postal.configuration.bus.publish( envelope );
- return envelope;
- }
- },
- channelPicker = {
- "1" : function ( chn ) {
- var channel = chn, topic, options = {};
- if ( Object.prototype.toString.call( channel ) === "[object String]" ) {
- channel = DEFAULT_CHANNEL;
- topic = chn;
- }
- else {
- channel = chn.channel || DEFAULT_CHANNEL;
- topic = chn.topic;
- options = chn.options || options;
- }
- return new postal.channelTypes[ options.type || "local" ]( channel, topic );
- },
- "2" : function ( chn, tpc ) {
- var channel = chn, topic = tpc, options = {};
- if ( Object.prototype.toString.call( tpc ) === "[object Object]" ) {
- channel = DEFAULT_CHANNEL;
- topic = chn;
- options = tpc;
- }
- return new postal.channelTypes[ options.type || "local" ]( channel, topic );
- },
- "3" : function ( channel, topic, options ) {
- return new postal.channelTypes[ options.type || "local" ]( channel, topic );
- }
- },
- sessionInfo = {};
+var sessionInfo = {};
// save some setup time, albeit tiny
localBus.subscriptions[SYSTEM_CHANNEL] = {};
@@ -61,29 +13,22 @@ var postal = {
SYSTEM_CHANNEL : SYSTEM_CHANNEL
},
- channelTypes : {
- local : ChannelDefinition
- },
+ ChannelDefinition : ChannelDefinition,
- channel : function () {
- var len = arguments.length;
- if ( channelPicker[len] ) {
- return channelPicker[len].apply( this, arguments );
- }
+ SubscriptionDefinition: SubscriptionDefinition,
+
+ channel : function ( channelName ) {
+ return new ChannelDefinition( channelName );
},
subscribe : function ( options ) {
- var callback = options.callback,
- topic = options.topic,
- channel = options.channel || DEFAULT_CHANNEL;
- return new SubscriptionDefinition( channel, topic, callback );
+ return new SubscriptionDefinition( options.channel || DEFAULT_CHANNEL, options.topic, options.callback );
},
- publish : function () {
- var len = arguments.length;
- if ( publishPicker[len] ) {
- return publishPicker[len].apply( this, arguments );
- }
+ publish : function ( envelope ) {
+ envelope.channel = envelope.channel || DEFAULT_CHANNEL;
+ postal.configuration.bus.publish( envelope );
+ return envelope;
},
addWireTap : function ( callback ) {
diff --git a/src/ChannelDefinition.js b/src/ChannelDefinition.js
index f053433..8a556d7 100644
--- a/src/ChannelDefinition.js
+++ b/src/ChannelDefinition.js
@@ -1,40 +1,18 @@
-var ChannelDefinition = function ( channelName, defaultTopic ) {
+var ChannelDefinition = function ( channelName ) {
this.channel = channelName || DEFAULT_CHANNEL;
- this._topic = defaultTopic || "";
};
ChannelDefinition.prototype = {
subscribe : function () {
- var len = arguments.length;
- if ( len === 1 ) {
- return new SubscriptionDefinition( this.channel, this._topic, arguments[0] );
- }
- else if ( len === 2 ) {
- return new SubscriptionDefinition( this.channel, arguments[0], arguments[1] );
- }
+ return arguments.length === 1 ?
+ new SubscriptionDefinition( this.channel, arguments[0].topic, arguments[0].callback ) :
+ new SubscriptionDefinition( this.channel, arguments[0], arguments[1] );
},
- publish : function ( obj ) {
- var _obj = obj || {};
- var envelope = {
- channel : this.channel,
- topic : this._topic,
- data : _obj
- };
- // If this is an envelope....
- if ( _obj.topic && _obj.data ) {
- envelope = _obj;
- envelope.channel = envelope.channel || this.channel;
- }
- envelope.timeStamp = new Date();
+ publish : function () {
+ var envelope = arguments.length === 1 ? arguments[0] : { topic: arguments[0], data: arguments[1] };
+ envelope.channel = this.channel;
postal.configuration.bus.publish( envelope );
return envelope;
- },
-
- topic : function ( topic ) {
- if ( topic === this._topic ) {
- return this;
- }
- return new ChannelDefinition( this.channel, topic );
}
};
diff --git a/src/LocalBus.js b/src/LocalBus.js
index 15b56fe..7712766 100644
--- a/src/LocalBus.js
+++ b/src/LocalBus.js
@@ -30,6 +30,7 @@ var localBus = {
},
publish : function ( envelope ) {
+ envelope.timeStamp = new Date();
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
} );