Adds publish/subscribe channels. Unit tests added.
This commit is contained in:
parent
9edc3d4f39
commit
96b0089c9c
4 changed files with 93 additions and 0 deletions
1
Makefile
1
Makefile
|
@ -11,6 +11,7 @@ POST_COMPILER = ${JS_ENGINE} ${BUILD_DIR}/post-compile.js
|
||||||
|
|
||||||
BASE_FILES = ${SRC_DIR}/core.js\
|
BASE_FILES = ${SRC_DIR}/core.js\
|
||||||
${SRC_DIR}/callbacks.js\
|
${SRC_DIR}/callbacks.js\
|
||||||
|
${SRC_DIR}/channel.js\
|
||||||
${SRC_DIR}/deferred.js\
|
${SRC_DIR}/deferred.js\
|
||||||
${SRC_DIR}/support.js\
|
${SRC_DIR}/support.js\
|
||||||
${SRC_DIR}/data.js\
|
${SRC_DIR}/data.js\
|
||||||
|
|
34
src/channel.js
Normal file
34
src/channel.js
Normal 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 );
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
<script src="../src/core.js"></script>
|
<script src="../src/core.js"></script>
|
||||||
<script src="../src/callbacks.js"></script>
|
<script src="../src/callbacks.js"></script>
|
||||||
|
<script src="../src/channel.js"></script>
|
||||||
<script src="../src/deferred.js"></script>
|
<script src="../src/deferred.js"></script>
|
||||||
<script src="../src/support.js"></script>
|
<script src="../src/support.js"></script>
|
||||||
<script src="../src/data.js"></script>
|
<script src="../src/data.js"></script>
|
||||||
|
@ -37,6 +38,7 @@
|
||||||
<script src="unit/core.js"></script>
|
<script src="unit/core.js"></script>
|
||||||
<script src="unit/support.js"></script>
|
<script src="unit/support.js"></script>
|
||||||
<script src="unit/callbacks.js"></script>
|
<script src="unit/callbacks.js"></script>
|
||||||
|
<script src="unit/channel.js"></script>
|
||||||
<script src="unit/deferred.js"></script>
|
<script src="unit/deferred.js"></script>
|
||||||
<script src="unit/data.js"></script>
|
<script src="unit/data.js"></script>
|
||||||
<script src="unit/queue.js"></script>
|
<script src="unit/queue.js"></script>
|
||||||
|
|
56
test/unit/channel.js
Normal file
56
test/unit/channel.js
Normal 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" );
|
||||||
|
});
|
Loading…
Reference in a new issue