From 0c9c9fb3cff41bb5deca0220457819498f40f020 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Tue, 24 May 2011 21:37:38 +0200 Subject: [PATCH] Renames $.Channel as $.Topic to be on par with usual terminology of existing pub/sub implementations. --- Makefile | 2 +- src/channel.js | 45 ------------------------------ src/topic.js | 45 ++++++++++++++++++++++++++++++ test/index.html | 4 +-- test/unit/{channel.js => topic.js} | 28 +++++++++---------- 5 files changed, 62 insertions(+), 62 deletions(-) delete mode 100644 src/channel.js create mode 100644 src/topic.js rename test/unit/{channel.js => topic.js} (63%) diff --git a/Makefile b/Makefile index a21a7f38..4a0a80ba 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ POST_COMPILER = ${JS_ENGINE} ${BUILD_DIR}/post-compile.js BASE_FILES = ${SRC_DIR}/core.js\ ${SRC_DIR}/callbacks.js\ - ${SRC_DIR}/channel.js\ + ${SRC_DIR}/topic.js\ ${SRC_DIR}/deferred.js\ ${SRC_DIR}/support.js\ ${SRC_DIR}/data.js\ diff --git a/src/channel.js b/src/channel.js deleted file mode 100644 index 052c3f8f..00000000 --- a/src/channel.js +++ /dev/null @@ -1,45 +0,0 @@ -(function( jQuery ) { - - var channels = {}, - sliceChannel = [].slice; - - jQuery.Channel = function( name ) { - var callbacks, - method, - channel = name && channels[ name ]; - if ( !channel ) { - callbacks = jQuery.Callbacks(); - channel = { - publish: callbacks.fire, - subscribe: callbacks.add, - unsubscribe: callbacks.remove - }; - if ( name ) { - channels[ name ] = channel; - } - } - return channel; - }; - - jQuery.extend({ - subscribe: function( id ) { - var channel = jQuery.Channel( id ), - args = sliceChannel.call( arguments, 1 ); - channel.subscribe.apply( channel, args ); - return { - channel: channel, - args: args - }; - }, - unsubscribe: function( id ) { - var channel = id && id.channel || jQuery.Channel( id ); - channel.unsubscribe.apply( channel, id && id.args || - sliceChannel.call( arguments, 1 ) ); - }, - publish: function( id ) { - var channel = jQuery.Channel( id ); - channel.publish.apply( channel, sliceChannel.call( arguments, 1 ) ); - } - }); - -})( jQuery ); diff --git a/src/topic.js b/src/topic.js new file mode 100644 index 00000000..c856db8c --- /dev/null +++ b/src/topic.js @@ -0,0 +1,45 @@ +(function( jQuery ) { + + var topics = {}, + sliceTopic = [].slice; + + jQuery.Topic = function( id ) { + var callbacks, + method, + topic = id && topics[ id ]; + if ( !topic ) { + callbacks = jQuery.Callbacks(); + topic = { + publish: callbacks.fire, + subscribe: callbacks.add, + unsubscribe: callbacks.remove + }; + if ( id ) { + topics[ id ] = topic; + } + } + return topic; + }; + + jQuery.extend({ + subscribe: function( id ) { + var topic = jQuery.Topic( id ), + args = sliceTopic.call( arguments, 1 ); + topic.subscribe.apply( topic, args ); + return { + topic: topic, + args: args + }; + }, + unsubscribe: function( id ) { + var topic = id && id.topic || jQuery.Topic( id ); + topic.unsubscribe.apply( topic, id && id.args || + sliceTopic.call( arguments, 1 ) ); + }, + publish: function( id ) { + var topic = jQuery.Topic( id ); + topic.publish.apply( topic, sliceTopic.call( arguments, 1 ) ); + } + }); + +})( jQuery ); diff --git a/test/index.html b/test/index.html index 86b21821..0314092a 100644 --- a/test/index.html +++ b/test/index.html @@ -10,7 +10,7 @@ - + @@ -37,7 +37,7 @@ - + diff --git a/test/unit/channel.js b/test/unit/topic.js similarity index 63% rename from test/unit/channel.js rename to test/unit/topic.js index b5e7a726..0b126fe3 100644 --- a/test/unit/channel.js +++ b/test/unit/topic.js @@ -1,10 +1,10 @@ -module("channel", { teardown: moduleTeardown }); +module("topic", { teardown: moduleTeardown }); -test( "jQuery.Channel - Anonymous Channel", function() { +test( "jQuery.Topic - Anonymous Topic", function() { expect( 4 ); - var channel = jQuery.Channel(), + var topic = jQuery.Topic(), count = 0; function firstCallback( value ) { @@ -13,19 +13,19 @@ test( "jQuery.Channel - Anonymous Channel", function() { } count++; - channel.subscribe( firstCallback ); - channel.publish( "test" ); - channel.unsubscribe( firstCallback ); + topic.subscribe( firstCallback ); + topic.publish( "test" ); + topic.unsubscribe( firstCallback ); count++; - channel.subscribe(function( value ) { + topic.subscribe(function( value ) { strictEqual( count, 2, "Callback called when needed" ); strictEqual( value, "test", "Published value received" ); }); - channel.publish( "test" ); + topic.publish( "test" ); }); -test( "jQuery.Channel - Named Channel", function() { +test( "jQuery.Topic - Named Topic", function() { expect( 2 ); @@ -34,13 +34,13 @@ test( "jQuery.Channel - Named Channel", function() { strictEqual( value, "test", "Proper value received" ); } - jQuery.Channel( "test" ).subscribe( callback ); - jQuery.Channel( "test" ).publish( "test" ); - jQuery.Channel( "test" ).unsubscribe( callback ); - jQuery.Channel( "test" ).publish( "test" ); + jQuery.Topic( "test" ).subscribe( callback ); + jQuery.Topic( "test" ).publish( "test" ); + jQuery.Topic( "test" ).unsubscribe( callback ); + jQuery.Topic( "test" ).publish( "test" ); }); -test( "jQuery.Channel - Helpers", function() { +test( "jQuery.Topic - Helpers", function() { expect( 4 );