Permalink
Browse files

update Tests to reflect new method name. Update Ender Bridge

  • Loading branch information...
1 parent 7cae693 commit 60d88d36d4715089ba1564221a3d518cc84f5230 @uxder committed Jan 17, 2012
Showing with 96 additions and 114 deletions.
  1. +1 −10 ender.js
  2. +1 −1 package.json
  3. +1 −1 radio.js
  4. +1 −1 radio.min.js
  5. +73 −82 tests/ender/ender.js
  6. +19 −19 tests/ender/integration.html
View
11 ender.js
@@ -1,16 +1,7 @@
(function ($) {
var radio = require('radio');
- function integrate(meth) {
- return function() {
- var r = radio(arguments[0]);
- return r[meth].apply(r, Array.prototype.slice.call(arguments, 1));
- };
- }
-
$.ender({
- subscribe: integrate('add'),
- unsubscribe: integrate('remove'),
- broadcast: integrate('broadcast')
+ radio: radio
});
}(ender));
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "radio",
"description": "A small dependency-free publish/subscribe (pub/sub) javascript library",
- "version": "0.1.11",
+ "version": "0.2",
"homepage": "http://radio.uxder.com/",
"author": "Scott Murphy, @hellocreation",
"keywords": [ "pubsub", "pub/sub","publish","subscribe","events", "ender" ],
View
2 radio.js
@@ -43,7 +43,7 @@
}
radio.$ = {
- version: '0.1.11',
+ version: '0.2',
channelName: "",
channels: [],
/**
View
2 radio.min.js
@@ -1 +1 @@
-(function(a,c,b){if(typeof module!=="undefined"){module.exports=b(a,c)}else{if(typeof define==="function"&&typeof define.amd==="object"){define(b)}else{c[a]=b(a,c)}}})("radio",this,function(b,c){function a(d){a.$.channel(d);return a.$}a.$={version:"0.1.11",channelName:"",channels:[],broadcast:function(){var f,j=this.channels[this.channelName],d=j.length,g,h,e;for(f=0;f<d;f++){g=j[f];if((typeof(g)==="object")&&(g.length)){h=g[0];e=g[1]||c}h.apply(e,arguments)}return this},channel:function(d){var e=this.channels;if(!e[d]){e[d]=[]}this.channelName=d;return this},add:function(){var f=arguments,j=this.channels[this.channelName],g,e=f.length,h,d=[];for(g=0;g<e;g++){d=f[g];h=(typeof(d)==="function")?[d]:d;if((typeof(h)==="object")&&(h.length)){j.push(h)}}return this},remove:function(){var g=arguments,k,h,n=this.channels[this.channelName],f=g.length,e=n.length,m=0,d;for(k=0;k<f;k++){m=0;e=n.length;for(h=0;h<e;h++){d=h-m;if(n[d][0]===g[k]){n.splice(d,1);m++}}}return this}};return a});
+(function(a,c,b){if(typeof module!=="undefined"){module.exports=b(a,c)}else{if(typeof define==="function"&&typeof define.amd==="object"){define(b)}else{c[a]=b(a,c)}}})("radio",this,function(b,c){function a(d){a.$.channel(d);return a.$}a.$={version:"0.2",channelName:"",channels:[],broadcast:function(){var f,j=this.channels[this.channelName],d=j.length,g,h,e;for(f=0;f<d;f++){g=j[f];if((typeof(g)==="object")&&(g.length)){h=g[0];e=g[1]||c}h.apply(e,arguments)}return this},channel:function(d){var e=this.channels;if(!e[d]){e[d]=[]}this.channelName=d;return this},subscribe:function(){var f=arguments,j=this.channels[this.channelName],g,e=f.length,h,d=[];for(g=0;g<e;g++){d=f[g];h=(typeof(d)==="function")?[d]:d;if((typeof(h)==="object")&&(h.length)){j.push(h)}}return this},unsubscribe:function(){var g=arguments,k,h,n=this.channels[this.channelName],f=g.length,e=n.length,m=0,d;for(k=0;k<f;k++){m=0;e=n.length;for(h=0;h<e;h++){d=h-m;if(n[d][0]===g[k]){n.splice(d,1);m++}}}return this}};return a});
View
155 tests/ender/ender.js
@@ -185,77 +185,77 @@
},
/**
- * Add Subscriber to channel
- * Take the arguments and add it to the this.channels array.
- * @param {Function|Array} arguments list of callbacks or arrays[callback, context] separated by commas
- * @example
- * //basic usage
- * var callback = function() {};
- * radio('channel1').add(callback);
- *
- * //add an endless amount of callbacks
- * radio('channel1').add(callback, callback2, callback3 ...);
- *
- * //adding callbacks with context
- * radio('channel1').add([callback, context],[callback1, context], callback3);
- *
- * //add by chaining
- * radio('channel1').add(callback).radio('channel2').add(callback).add(callback2);
- */
- add: function() {
- var a = arguments,
- c = this.channels[this.channelName],
- i, l = a.length,
- p, ai = [];
-
- //run through each arguments and add it to the channel
- for (i = 0; i < l; i++) {
- ai = a[i];
- //if the user sent just a function, wrap the fucntion in an array [function]
- p = (typeof(ai) === "function") ? [ai] : ai;
- if ((typeof(p) === 'object') && (p.length)) c.push(p);
- }
- return this;
- },
-
- /**
- * Remove subscriber from channel
- * Take arguments with functions and remove it if there is a match against existing subscribers.
- * @param {Function} arguments callbacks separated by commas
- * @example
- * //basic usage
- * radio('channel1').remove(callback);
- * //you can remove as many callbacks as you want
- * radio('channel1').remove(callback, callback2, callback3 ...);
- * //removing callbacks with context is the same
- * radio('channel1').add([callback, context]).remove(callback);
- */
- remove: function() {
- var a = arguments,
- i, j, c = this.channels[this.channelName],
- l = a.length,
- cl = c.length,
- offset = 0,
- jo;
- //loop through each argument
- for (i = 0; i < l; i++) {
- //need to reset vars that change as the channel array items are removed
- offset = 0;
- cl = c.length;
- //loop through the channel
- for (j = 0; j < cl; j++) {
- jo = j - offset;
- //if there is a match with the argument and the channel function, remove it from the channel array
- if (c[jo][0] === a[i]) {
- //remove matched item from the channel array
- c.splice(jo, 1);
- offset++;
- }
- }
- }
- return this;
- }
- };
+ * Add Subscriber to channel
+ * Take the arguments and add it to the this.channels array.
+ * @param {Function|Array} arguments list of callbacks or arrays[callback, context] separated by commas
+ * @example
+ * //basic usage
+ * var callback = function() {};
+ * radio('channel1').subscribe(callback);
+ *
+ * //subscribe an endless amount of callbacks
+ * radio('channel1').subscribe(callback, callback2, callback3 ...);
+ *
+ * //adding callbacks with context
+ * radio('channel1').subscribe([callback, context],[callback1, context], callback3);
+ *
+ * //subscribe by chaining
+ * radio('channel1').subscribe(callback).radio('channel2').subscribe(callback).subscribe(callback2);
+ */
+ subscribe: function() {
+ var a = arguments,
+ c = this.channels[this.channelName],
+ i, l = a.length,
+ p, ai = [];
+
+ //run through each arguments and subscribe it to the channel
+ for (i = 0; i < l; i++) {
+ ai = a[i];
+ //if the user sent just a function, wrap the fucntion in an array [function]
+ p = (typeof(ai) === "function") ? [ai] : ai;
+ if ((typeof(p) === 'object') && (p.length)) c.push(p);
+ }
+ return this;
+ },
+
+ /**
+ * Remove subscriber from channel
+ * Take arguments with functions and unsubscribe it if there is a match against existing subscribers.
+ * @param {Function} arguments callbacks separated by commas
+ * @example
+ * //basic usage
+ * radio('channel1').unsubscribe(callback);
+ * //you can unsubscribe as many callbacks as you want
+ * radio('channel1').unsubscribe(callback, callback2, callback3 ...);
+ * //removing callbacks with context is the same
+ * radio('channel1').subscribe([callback, context]).unsubscribe(callback);
+ */
+ unsubscribe: function() {
+ var a = arguments,
+ i, j, c = this.channels[this.channelName],
+ l = a.length,
+ cl = c.length,
+ offset = 0,
+ jo;
+ //loop through each argument
+ for (i = 0; i < l; i++) {
+ //need to reset vars that change as the channel array items are removed
+ offset = 0;
+ cl = c.length;
+ //loop through the channel
+ for (j = 0; j < cl; j++) {
+ jo = j - offset;
+ //if there is a match with the argument and the channel function, unsubscribe it from the channel array
+ if (c[jo][0] === a[i]) {
+ //unsubscribe matched item from the channel array
+ c.splice(jo, 1);
+ offset++;
+ }
+ }
+ }
+ return this;
+ }
+ };
return radio;
});
@@ -265,18 +265,9 @@
(function ($) {
var radio = require('radio');
-
- function integrate(meth) {
- return function() {
- var r = radio(arguments[0]);
- return r[meth].apply(r, Array.prototype.slice.call(arguments, 1));
- }
- }
-
+
$.ender({
- subscribe: integrate('add'),
- unsubscribe: integrate('remove'),
- broadcast: integrate('broadcast')
+ radio: radio
});
}(ender))
View
38 tests/ender/integration.html
@@ -24,49 +24,49 @@
}
res = false;
- $.subscribe('test1', function() { res = true; });
- $.broadcast('test1');
+ $.radio('test1').subscribe(function() { res = true; });
+ $.radio('test1').broadcast();
result(res, 'simple subscribe &amp; broadcast');
res = false;
- $.subscribe('test2', function(data) { res = data === 101; });
- $.broadcast('test2', 101);
+ $.radio('test2').subscribe(function(data) { res = data === 101; });
+ $.radio('test2').broadcast(101);
result(res, 'single argument subscribe &amp; broadcast');
res = false;
- $.subscribe('test3', function() { res = arguments[0] === 101 && arguments[1] === 202 && arguments[2] === 303; });
- $.broadcast('test3', 101, 202, 303);
+ $.radio('test3').subscribe(function() { res = arguments[0] === 101 && arguments[1] === 202 && arguments[2] === 303; });
+ $.radio('test3').broadcast(101, 202, 303);
result(res, 'multi argument subscribe &amp; broadcast');
res = 0;
fn = function() { res++; };
- $.subscribe('test4', fn)
- $.broadcast('test4');
- $.broadcast('test4');
- $.unsubscribe('test4', fn)
- $.broadcast('test4');
+ $.radio('test4').subscribe(fn)
+ $.radio('test4').broadcast();
+ $.radio('test4').broadcast();
+ $.radio('test4').unsubscribe(fn)
+ $.radio('test4').broadcast();
result(res === 2, 'subscribe, multi-broadcast &amp; unsubscribe');
res = false;
ctx = {};
- $.subscribe('test5', [ function() { res = this === ctx; }, ctx ]);
- $.broadcast('test5');
+ $.radio('test5').subscribe([ function() { res = this === ctx; }, ctx ]);
+ $.radio('test5').broadcast();
result(res, 'subscribe &amp; broadcast with context');
res = 0;
- $.subscribe('test6', function() { res++ });
- $.subscribe('test6', function() { res++ });
- $.broadcast('test6');
+ $.radio('test6').subscribe(function() { res++ });
+ $.radio('test6').subscribe(function() { res++ });
+ $.radio('test6').broadcast();
result(res === 2, 'multiple subscribers');
res = 0;
- $.subscribe('test7', function() { res++ }, function() { res++ });
- $.broadcast('test7');
+ $.radio('test7').subscribe(function() { res++ }, function() { res++ });
+ $.radio('test7').broadcast();
result(res === 2, 'multi-argument subscribe');
res = 0;
fn = function() { res++; };
- $.subscribe('test8', fn).broadcast('test8').broadcast('test8').remove('test8', fn).broadcast('test8');
+ $.radio('test8').subscribe(fn).broadcast().broadcast().unsubscribe(fn).broadcast();
result(res === 2, 'chained subscribe, multi-broadcast &amp; unsubscribe');
</script>

0 comments on commit 60d88d3

Please sign in to comment.