Skip to content

Commit 96b0089

Browse files
committed
Adds publish/subscribe channels. Unit tests added.
1 parent 9edc3d4 commit 96b0089

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ POST_COMPILER = ${JS_ENGINE} ${BUILD_DIR}/post-compile.js
1111

1212
BASE_FILES = ${SRC_DIR}/core.js\
1313
${SRC_DIR}/callbacks.js\
14+
${SRC_DIR}/channel.js\
1415
${SRC_DIR}/deferred.js\
1516
${SRC_DIR}/support.js\
1617
${SRC_DIR}/data.js\

src/channel.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(function( jQuery ) {
2+
3+
var channels = {},
4+
channelMethods = {
5+
publish: "fire",
6+
subscribe: "add",
7+
unsubscribe: "remove"
8+
};
9+
10+
jQuery.Channel = function( name ) {
11+
var callbacks,
12+
method,
13+
channel = name && channels[ name ];
14+
if ( !channel ) {
15+
callbacks = jQuery.Callbacks();
16+
channel = {};
17+
for ( method in channelMethods ) {
18+
channel[ method ] = callbacks[ channelMethods[ method ] ];
19+
}
20+
if ( name ) {
21+
channels[ name ] = channel;
22+
}
23+
}
24+
return channel;
25+
};
26+
27+
jQuery.each( channelMethods, function( method ) {
28+
jQuery[ method ] = function( name ) {
29+
var channel = jQuery.Channel( name );
30+
channel[ method ].apply( channel, Array.prototype.slice.call( arguments, 1 ) );
31+
};
32+
});
33+
34+
})( jQuery );

test/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<script src="../src/core.js"></script>
1212
<script src="../src/callbacks.js"></script>
13+
<script src="../src/channel.js"></script>
1314
<script src="../src/deferred.js"></script>
1415
<script src="../src/support.js"></script>
1516
<script src="../src/data.js"></script>
@@ -37,6 +38,7 @@
3738
<script src="unit/core.js"></script>
3839
<script src="unit/support.js"></script>
3940
<script src="unit/callbacks.js"></script>
41+
<script src="unit/channel.js"></script>
4042
<script src="unit/deferred.js"></script>
4143
<script src="unit/data.js"></script>
4244
<script src="unit/queue.js"></script>

test/unit/channel.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module("channel", { teardown: moduleTeardown });
2+
3+
test( "jQuery.Channel - Anonymous Channel", function() {
4+
5+
expect( 4 );
6+
7+
var channel = jQuery.Channel(),
8+
count = 0;
9+
10+
function firstCallback( value ) {
11+
strictEqual( count, 1, "Callback called when needed" );
12+
strictEqual( value, "test", "Published value received" );
13+
}
14+
15+
count++;
16+
channel.subscribe( firstCallback );
17+
channel.publish( "test" );
18+
channel.unsubscribe( firstCallback );
19+
count++;
20+
channel.subscribe(function( value ) {
21+
strictEqual( count, 2, "Callback called when needed" );
22+
strictEqual( value, "test", "Published value received" );
23+
});
24+
channel.publish( "test" );
25+
26+
});
27+
28+
test( "jQuery.Channel - Named Channel", function() {
29+
30+
expect( 2 );
31+
32+
function callback( value ) {
33+
ok( true, "Callback called" );
34+
strictEqual( value, "test", "Proper value received" );
35+
}
36+
37+
jQuery.Channel( "test" ).subscribe( callback );
38+
jQuery.Channel( "test" ).publish( "test" );
39+
jQuery.Channel( "test" ).unsubscribe( callback );
40+
jQuery.Channel( "test" ).publish( "test" );
41+
});
42+
43+
test( "jQuery.Channel - Helpers", function() {
44+
45+
expect( 2 );
46+
47+
function callback( value ) {
48+
ok( true, "Callback called" );
49+
strictEqual( value, "test", "Proper value received" );
50+
}
51+
52+
jQuery.subscribe( "test", callback );
53+
jQuery.publish( "test" , "test" );
54+
jQuery.unsubscribe( "test", callback );
55+
jQuery.publish( "test" , "test" );
56+
});

0 commit comments

Comments
 (0)