Renames $.Channel as $.Topic to be on par with usual terminology of existing pub/sub implementations.

1.7/callbacks^2
jaubourg 2011-05-24 21:37:38 +02:00
parent 55df216125
commit 3e7c04ec94
5 changed files with 62 additions and 62 deletions

View File

@ -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\

View File

@ -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 );

45
src/topic.js Normal file
View File

@ -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 );

View File

@ -10,7 +10,7 @@
<script src="../src/core.js"></script>
<script src="../src/callbacks.js"></script>
<script src="../src/channel.js"></script>
<script src="../src/topic.js"></script>
<script src="../src/deferred.js"></script>
<script src="../src/support.js"></script>
<script src="../src/data.js"></script>
@ -38,7 +38,7 @@
<script src="unit/core.js"></script>
<script src="unit/support.js"></script>
<script src="unit/callbacks.js"></script>
<script src="unit/channel.js"></script>
<script src="unit/topic.js"></script>
<script src="unit/deferred.js"></script>
<script src="unit/data.js"></script>
<script src="unit/queue.js"></script>

View File

@ -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 );