Permalink
Browse files

Merge pull request #3 from uxder/renamingAddRemoveMethods

Renaming add remove methods
  • Loading branch information...
2 parents 9dec280 + 60d88d3 commit 294187d62d2e49ac38fc389888dedf8b9c711e44 @uxder committed Jan 17, 2012
Showing with 166 additions and 184 deletions.
  1. +1 −10 ender.js
  2. +1 −1 package.json
  3. +17 −17 radio.js
  4. +1 −1 radio.min.js
  5. +54 −54 tests/core.js
  6. +73 −82 tests/ender/ender.js
  7. +19 −19 tests/ender/integration.html
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,100 +63,100 @@ 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);
});
});
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,47 +165,47 @@ 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();
});
});
- 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();
Oops, something went wrong. Retry.

0 comments on commit 294187d

Please sign in to comment.