Skip to content

Commit

Permalink
Merge pull request #3 from uxder/renamingAddRemoveMethods
Browse files Browse the repository at this point in the history
Renaming add remove methods
  • Loading branch information
uxder committed Jan 17, 2012
2 parents 9dec280 + 60d88d3 commit 294187d
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 184 deletions.
11 changes: 1 addition & 10 deletions 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));
2 changes: 1 addition & 1 deletion 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" ],
Expand Down
34 changes: 17 additions & 17 deletions radio.js
Expand Up @@ -43,7 +43,7 @@
}

radio.$ = {
version: '0.1.11',
version: '0.2',
channelName: "",
channels: [],
/**
Expand Down Expand Up @@ -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]
Expand All @@ -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,
Expand All @@ -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++;
}
Expand Down
2 changes: 1 addition & 1 deletion radio.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 54 additions & 54 deletions tests/core.js
Expand Up @@ -5,11 +5,11 @@ var test = {
selfTest : function() {
1+2;
},
add: function() {
subscribe: function() {
1+1;
},
scopeTest: function() {
this.add();
this.subscribe();
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 294187d

Please sign in to comment.