Permalink
Browse files

Merge pull request #1 from uxder/removeBugFix

Remove bug fix
  • Loading branch information...
uxder committed Dec 27, 2011
2 parents 20844fd + 1a97e0a commit acb6fa669159efcaa09e0eff578a14aca52bbe60
Showing with 65 additions and 45 deletions.
  1. +20 −38 radio.js
  2. +1 −1 radio.min.js
  3. +44 −6 tests/core.js
View
@@ -1,15 +1,6 @@
/**
* Author: Scott Murphy twitter: @hellocreation, github: uxder
* radio.js - The Chainable, Dependency Free Publish/Subscribe for Javascript
-
-
-ToDo:
- - create more tests
- - change out the add method so that you never add a fnction..I'm always adding an array
- - change out for loops for native array filter, search methods.
- - test benchmarks.
-
-
*/
(function(global) {
@@ -52,7 +43,7 @@ ToDo:
//if(typeof(listener) == "function") callback = context = listener;
//run the listener
- callback.apply(context, [arguments].splice(i,1));
+ callback.apply(context, arguments);
}
return this;
},
@@ -97,10 +88,10 @@ ToDo:
//run through each arguments and add it to the channel
for(i=0; i<l;i++) {
- var p;
+ var p, ai = a[i];
//add accepts either an array (fucntion, context) or just the function.
//if the user send just a function, wrap the fucntion in an array [function]
- p = (typeof(a[i]) == "function") ? [a[i]] : a[i];
+ p = (typeof(ai) === "function") ? [ai] : ai;
if( (typeof(p) === 'object') && (p.length) ) c[cn].push(p);
}
return this;
@@ -120,36 +111,27 @@ ToDo:
* radio('channel1').add([callback, context]).remove(callback); //just remove the callback
*/
remove: function() {
- var a = arguments,
- i,
- l= a.length;
- //run through the arguments
- for(i=0; i<l;i++) {
- this._removeOne(a[i]);
- }
- return this;
- },
-
- /**
- * Remove one listener from channel
- */
- _removeOne: function(func) {
- var i,
+ var a = arguments, i, j,
c = this.channels[this.channelName],
- l= c.length;
- //is this an expensive way to match and can? perhaps use another method like .search in array.
+ l= a.length,
+ cl = c.length,
+ offset = 0, jo;
+ //loop through each argument
for(i=0; i<l;i++) {
- if(c[i][0] === func) {
- c.splice(i,1);
- break;
+ offset = 0;
+ //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]) {
+ c.splice(jo,1);
+ offset++;
+ }
+
}
}
- },
- /**
- * return Array of all listeners in the current channel
- */
- all: function(){
- return this.channels[this.channeName];
+ return this;
}
};
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -3,10 +3,13 @@
*/
var test = {
selfTest : function() {
- return this;
+ 1+2;
},
add: function() {
1+1;
+ },
+ scopeTest: function() {
+ this.add();
}
}
@@ -69,15 +72,15 @@ describe("Radio Core Test", function() {
radio('channel1').add(f,f,f);
expect(radio._.channels.channel1.length).toBe(3);
});
- it("it should suppport adding listerners with setting the context of 'this'", function() {
+ it("should suppport adding listerners with setting the context of 'this'", function() {
radio('channel1').add([test.selfTest, test]);
expect(radio._.channels.channel1.length).toBe(1);
});
- it("it should a combination of adding functions and anonymous functions", function() {
+ it("should support a combination of adding functions in different ways", function() {
radio('channel1').add(f,[test.selfTest, test], f2);
expect(radio._.channels.channel1.length).toBe(3);
});
- it("it should not allow a non-function or array to be added", function() {
+ it("should not allow a non-function or array to be added", function() {
radio('channel1').add("string", f);
expect(radio._.channels.channel1.length).toBe(1);
radio('channel1').add(2, f);
@@ -102,6 +105,12 @@ describe("Radio Core Test", function() {
radio('channel1').remove(f);
expect(radio._.channels.channel1.length).toBe(2);
});
+ it("should remove duplicate listeners from a channel", function() {
+ radio('channel1').add(f,f,f);
+ expect(radio._.channels.channel1.length).toBe(3);
+ radio('channel1').remove(f);
+ expect(radio._.channels.channel1.length).toBe(0);
+ });
it("should remove listeners add with context from channel", function() {
radio('channel1').add([test.selfTest, test], f);
expect(radio._.channels.channel1.length).toBe(2);
@@ -122,11 +131,40 @@ describe("Radio Core Test", function() {
expect(radio._.channels.channel1.length).toBe(2);
radio('channel1').broadcast('test');
expect(test.selfTest).toHaveBeenCalled();
- expect(test.add).toHaveBeenCalled();
+ expect(test.add).toHaveBeenCalled();
+ expect(test.selfTest.callCount).toBe(1);
+ expect(test.add.callCount).toBe(1);
+ //reset methods
+ test.selfTest.reset();
+ test.add.reset();
+ });
+ it("should pass it's broadcast arguments to the listener", function() {
+ spyOn(test, 'add');
+ spyOn(test, 'selfTest');
+ radio('channel1').add([test.selfTest, test]);
+ radio('channel2').add([test.add, 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');
});
- it("should not fail even if the channel has never been created", function() {
+
+ 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);
+
});
+
+ 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');
+ expect(window.f.callCount).toBe(1);
+ });
+
});

0 comments on commit acb6fa6

Please sign in to comment.