Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Renaming add remove methods #3

Merged
merged 3 commits into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
34 radio.js
@@ -43,7 +43,7 @@
}
radio.$ = {
- version: '0.1.11',
+ version: '0.2',
channelName: "",
channels: [],
/**
@@ -95,24 +95,24 @@
* @example
* //basic usage
* var callback = function() {};
- * radio('channel1').add(callback);
+ * radio('channel1').subscribe(callback);
*
- * //add an endless amount of callbacks
- * radio('channel1').add(callback, callback2, callback3 ...);
+ * //subscribe an endless amount of callbacks
+ * radio('channel1').subscribe(callback, callback2, callback3 ...);
*
* //adding callbacks with context
- * radio('channel1').add([callback, context],[callback1, context], callback3);
+ * radio('channel1').subscribe([callback, context],[callback1, context], callback3);
*
- * //add by chaining
- * radio('channel1').add(callback).radio('channel2').add(callback).add(callback2);
+ * //subscribe by chaining
+ * radio('channel1').subscribe(callback).radio('channel2').subscribe(callback).subscribe(callback2);
*/
- add: function() {
+ subscribe: 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
+ //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]
@@ -124,17 +124,17 @@
/**
* Remove subscriber from channel
- * Take arguments with functions and remove it if there is a match against existing subscribers.
+ * 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').remove(callback);
- * //you can remove as many callbacks as you want
- * radio('channel1').remove(callback, callback2, callback3 ...);
+ * 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').add([callback, context]).remove(callback);
+ * radio('channel1').subscribe([callback, context]).unsubscribe(callback);
*/
- remove: function() {
+ unsubscribe: function() {
var a = arguments,
i, j, c = this.channels[this.channelName],
l = a.length,
@@ -149,9 +149,9 @@
//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 there is a match with the argument and the channel function, unsubscribe it from the channel array
if (c[jo][0] === a[i]) {
- //remove matched item from the channel array
+ //unsubscribe matched item from the channel array
c.splice(jo, 1);
offset++;
}
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
108 tests/core.js
@@ -5,11 +5,11 @@ var test = {
selfTest : function() {
1+2;
},
- add: function() {
+ subscribe: function() {
1+1;
},
scopeTest: function() {
- this.add();
+ this.subscribe();
}
}
@@ -49,7 +49,7 @@ describe("Radio Core Test", function() {
expect(radio.$.channelName).toBe('channel1');
});
it("should make multiple channels chainable", function() {
- radio('channel1').add(f,f).channel('channel2').add(f);
+ radio('channel1').subscribe(f,f).channel('channel2').subscribe(f);
expect(radio.$.channelName).toBe('channel2');
expect(radio.$.channels.channel1.length).toBe(2);
expect(radio.$.channels.channel2.length).toBe(1);
@@ -63,75 +63,75 @@ describe("Radio Core Test", function() {
});
- describe("radio.add method", function() {
+ describe("radio.subscribe method", function() {
it("should register a single listener to a channel", function() {
- var test = radio('channel1').add(f);
+ var test = radio('channel1').subscribe(f);
expect(radio.$.channels.channel1.length).toBe(1);
});
- it("should add multiple listeners to a channel", function() {
- radio('channel1').add(f,f,f);
+ it("should subscribe multiple listeners to a channel", function() {
+ radio('channel1').subscribe(f,f,f);
expect(radio.$.channels.channel1.length).toBe(3);
});
- it("should suppport adding listerners with setting the context of 'this'", function() {
- radio('channel1').add([test.selfTest, test]);
+ it("should suppport subscribeing listerners with setting the context of 'this'", function() {
+ radio('channel1').subscribe([test.selfTest, test]);
expect(radio.$.channels.channel1.length).toBe(1);
});
- it("should support a combination of adding functions in different ways", function() {
- radio('channel1').add(f,[test.selfTest, test], f2);
+ it("should support a combination of subscribeing functions in different ways", function() {
+ radio('channel1').subscribe(f,[test.selfTest, test], f2);
expect(radio.$.channels.channel1.length).toBe(3);
});
- it("should not allow a non-function or array to be added", function() {
- radio('channel1').add("string", f);
+ it("should not allow a non-function or array to be subscribeed", function() {
+ radio('channel1').subscribe("string", f);
expect(radio.$.channels.channel1.length).toBe(1);
- radio('channel1').add(2, f);
+ radio('channel1').subscribe(2, f);
expect(radio.$.channels.channel1.length).toBe(2);
});
it("should be chainable", function() {
- radio('channel1').add(f).add(f2);
+ radio('channel1').subscribe(f).subscribe(f2);
expect(radio.$.channels.channel1.length).toBe(2);
});
});
- describe("radio.remove method", function() {
- it("should remove a listener from a channel", function() {
- radio('channel1').add(f);
+ describe("radio.unsubscribe method", function() {
+ it("should unsubscribe a listener from a channel", function() {
+ radio('channel1').subscribe(f);
expect(radio.$.channels.channel1.length).toBe(1);
- radio('channel1').remove(f);
+ radio('channel1').unsubscribe(f);
expect(radio.$.channels.channel1.length).toBe(0);
});
- it("should remove multiple listeners from a channel", function() {
- radio('channel1').add(f,f2,f3);
+ it("should unsubscribe multiple listeners from a channel", function() {
+ radio('channel1').subscribe(f,f2,f3);
expect(radio.$.channels.channel1.length).toBe(3);
- radio('channel1').remove(f);
+ radio('channel1').unsubscribe(f);
expect(radio.$.channels.channel1.length).toBe(2);
});
- it("should remove duplicate listeners from a channel", function() {
+ it("should unsubscribe duplicate listeners from a channel", function() {
//check 1
- radio('channel1').add(f,f,f);
+ radio('channel1').subscribe(f,f,f);
expect(radio.$.channels.channel1.length).toBe(3);
- radio('channel1').remove(f);
+ radio('channel1').unsubscribe(f);
expect(radio.$.channels.channel1.length).toBe(0);
//check2
- radio('channel1').add(f,f,f, f2, f3, [test.selfTest, test], test.selfTest);
+ radio('channel1').subscribe(f,f,f, f2, f3, [test.selfTest, test], test.selfTest);
expect(radio.$.channels.channel1.length).toBe(7);
- radio('channel1').remove(f);
+ radio('channel1').unsubscribe(f);
expect(radio.$.channels.channel1.length).toBe(4);
- radio('channel1').remove(test.selfTest);
+ radio('channel1').unsubscribe(test.selfTest);
expect(radio.$.channels.channel1.length).toBe(2);
- radio('channel1').remove(f2, f3);
+ radio('channel1').unsubscribe(f2, f3);
expect(radio.$.channels.channel1.length).toBe(0);
});
- it("should remove listeners add with context from channel", function() {
- radio('channel1').add([test.selfTest, test], f);
+ it("should unsubscribe listeners subscribe with context from channel", function() {
+ radio('channel1').subscribe([test.selfTest, test], f);
expect(radio.$.channels.channel1.length).toBe(2);
- radio('channel1').remove(test.selfTest);
+ radio('channel1').unsubscribe(test.selfTest);
expect(radio.$.channels.channel1.length).toBe(1);
});
- it("should not throw an error if remove an item that doesn't exists", function() {
- radio('newChannel').remove(f2);
+ it("should not throw an error if unsubscribe an item that doesn't exists", function() {
+ radio('newChannel').unsubscribe(f2);
});
it("should be chainable", function() {
- radio('channel1').add(f, f2, f3).remove(f2);
+ radio('channel1').subscribe(f, f2, f3).unsubscribe(f2);
expect(radio.$.channels.channel1.length).toBe(2);
});
@@ -139,24 +139,24 @@ describe("Radio Core Test", function() {
describe("radio.broadcast method", function() {
it("should call each listener - test 1", function() {
- spyOn(test, 'add');
+ spyOn(test, 'subscribe');
spyOn(test, 'selfTest');
- radio('channel1').add([test.selfTest, test], [test.add, test]);
+ radio('channel1').subscribe([test.selfTest, test], [test.subscribe, test]);
expect(radio.$.channels.channel1.length).toBe(2);
radio('channel1').broadcast('test');
expect(test.selfTest).toHaveBeenCalled();
- expect(test.add).toHaveBeenCalled();
+ expect(test.subscribe).toHaveBeenCalled();
expect(test.selfTest.callCount).toBe(1);
- expect(test.add.callCount).toBe(1);
+ expect(test.subscribe.callCount).toBe(1);
//reset methods
test.selfTest.reset();
- test.add.reset();
+ test.subscribe.reset();
});
it("should call each listener - test 2", function() {
spyOn(window, 'f');
spyOn(window, 'f2');
spyOn(window, 'f3');
- radio('channel1').add(f, f, f, f, f2, f2, f3).broadcast('test');
+ radio('channel1').subscribe(f, f, f, f, f2, f2, f3).broadcast('test');
expect(window.f.callCount).toBe(4);
expect(window.f2.callCount).toBe(2);
expect(window.f3.callCount).toBe(1);
@@ -165,29 +165,29 @@ describe("Radio Core Test", function() {
window.f3.reset();
});
it("should pass it's broadcast arguments to the listener", function() {
- spyOn(test, 'add');
+ spyOn(test, 'subscribe');
spyOn(test, 'selfTest');
- radio('channel1').add([test.selfTest, test]);
- radio('channel2').add([test.add, test]);
+ radio('channel1').subscribe([test.selfTest, test]);
+ radio('channel2').subscribe([test.subscribe, test]);
radio('channel1').broadcast('data1', ['somearray','item2'], 'data3');
radio('channel2').broadcast('data1');
expect(test.selfTest).toHaveBeenCalledWith('data1', ['somearray','item2'], 'data3');
- expect(test.add).toHaveBeenCalledWith('data1');
+ expect(test.subscribe).toHaveBeenCalledWith('data1');
});
it("should call the listener and maintain it's set scope", function() {
- spyOn(test, 'add');
- //add the scopeTest method. Scope test sets the context 'test' as this and calls the add method
- radio('channel1').add([test.scopeTest, test]).broadcast('test');
- expect(test.add.callCount).toBe(1);
+ spyOn(test, 'subscribe');
+ //subscribe the scopeTest method. Scope test sets the context 'test' as this and calls the subscribe method
+ radio('channel1').subscribe([test.scopeTest, test]).broadcast('test');
+ expect(test.subscribe.callCount).toBe(1);
});
it("should call the listener and set the scope as window if it wasn't specified", function() {
spyOn(window, 'f');
- //add the scopeTest method. Scope test sets the context 'test' as this and calls the add method
- radio('channel1').add(f).broadcast('test');
+ //subscribe the scopeTest method. Scope test sets the context 'test' as this and calls the subscribe method
+ radio('channel1').subscribe(f).broadcast('test');
expect(window.f.callCount).toBe(1);
window.f.reset();
});
@@ -195,17 +195,17 @@ describe("Radio Core Test", function() {
});
- describe("radio add remove and broadcast methods ", function() {
+ describe("radio subscribe unsubscribe and broadcast methods ", function() {
it("should work all together in a chain", function() {
//test 1
- radio('channel1').add(f, f2, f3).remove(f);
+ radio('channel1').subscribe(f, f2, f3).unsubscribe(f);
expect(radio.$.channels.channel1.length).toBe(2);
//test2
spyOn(window, 'f');
spyOn(window, 'f2');
spyOn(window, 'f3');
- radio('channel2').add(f, f, f, f, f2).remove(f, f2).add(f3).broadcast('test');
+ radio('channel2').subscribe(f, f, f, f, f2).unsubscribe(f, f2).subscribe(f3).broadcast('test');
expect(window.f2.callCount).toBe(0);
expect(window.f3.callCount).toBe(1);
window.f.reset();
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>
Something went wrong with that request. Please try again.