Adds publish/subscribe channels. Unit tests added.

1.7/callbacks
jaubourg 2011-05-24 01:59:00 +02:00
parent 4f3f0e1d4e
commit 57634a2c15
4 changed files with 93 additions and 0 deletions

View File

@ -11,6 +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}/deferred.js\
${SRC_DIR}/support.js\
${SRC_DIR}/data.js\

34
src/channel.js Normal file
View File

@ -0,0 +1,34 @@
(function( jQuery ) {
var channels = {},
channelMethods = {
publish: "fire",
subscribe: "add",
unsubscribe: "remove"
};
jQuery.Channel = function( name ) {
var callbacks,
method,
channel = name && channels[ name ];
if ( !channel ) {
callbacks = jQuery.Callbacks();
channel = {};
for ( method in channelMethods ) {
channel[ method ] = callbacks[ channelMethods[ method ] ];
}
if ( name ) {
channels[ name ] = channel;
}
}
return channel;
};
jQuery.each( channelMethods, function( method ) {
jQuery[ method ] = function( name ) {
var channel = jQuery.Channel( name );
channel[ method ].apply( channel, Array.prototype.slice.call( arguments, 1 ) );
};
});
})( jQuery );

View File

@ -10,6 +10,7 @@
<script src="../src/core.js"></script>
<script src="../src/callbacks.js"></script>
<script src="../src/channel.js"></script>
<script src="../src/deferred.js"></script>
<script src="../src/support.js"></script>
<script src="../src/data.js"></script>
@ -36,6 +37,7 @@
<script src="unit/core.js"></script>
<script src="unit/callbacks.js"></script>
<script src="unit/channel.js"></script>
<script src="unit/deferred.js"></script>
<script src="unit/support.js"></script>
<script src="unit/data.js"></script>

56
test/unit/channel.js Normal file
View File

@ -0,0 +1,56 @@
module("channel", { teardown: moduleTeardown });
test( "jQuery.Channel - Anonymous Channel", function() {
expect( 4 );
var channel = jQuery.Channel(),
count = 0;
function firstCallback( value ) {
strictEqual( count, 1, "Callback called when needed" );
strictEqual( value, "test", "Published value received" );
}
count++;
channel.subscribe( firstCallback );
channel.publish( "test" );
channel.unsubscribe( firstCallback );
count++;
channel.subscribe(function( value ) {
strictEqual( count, 2, "Callback called when needed" );
strictEqual( value, "test", "Published value received" );
});
channel.publish( "test" );
});
test( "jQuery.Channel - Named Channel", function() {
expect( 2 );
function callback( value ) {
ok( true, "Callback called" );
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" );
});
test( "jQuery.Channel - Helpers", function() {
expect( 2 );
function callback( value ) {
ok( true, "Callback called" );
strictEqual( value, "test", "Proper value received" );
}
jQuery.subscribe( "test", callback );
jQuery.publish( "test" , "test" );
jQuery.unsubscribe( "test", callback );
jQuery.publish( "test" , "test" );
});