Skip to content

Commit

Permalink
There was a bug that didn't allow multiple callabcks to be registered…
Browse files Browse the repository at this point in the history
… to the same event. This fixes that
  • Loading branch information
screeley committed Apr 10, 2014
1 parent 5212b6c commit 6ff9b95
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Install
Player.js is hosted on Embedly's CDN.
::

<script type="text/javascript" src="//cdn.embed.ly/player-0.0.7.min.js"></script>
<script type="text/javascript" src="//cdn.embed.ly/player-0.0.8.min.js"></script>


Ready
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Player.js",
"name": "Player.js",
"version": "0.0.7",
"version": "0.0.8",
"homepage": "http://github.com/embedly/player.js",
"author": {
"name": "Embedly"
Expand Down
26 changes: 15 additions & 11 deletions src/keeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@ playerjs.Keeper.prototype.execute = function(event, id, data, ctx){
for (var i=0; i< this.data[event].length; i++){
var d = this.data[event][i];

if (!playerjs.isNone(id) && d.id !== id){
continue;
}

execute.push({
cb: d.cb,
ctx: d.ctx? d.ctx: ctx,
data: data
});

if (d.one === false){
// There are omni events, in that they do not have an id. i.e "ready".
// Or there is an ID and we only want to execute the right id'd method.
if (playerjs.isNone(id) || (!playerjs.isNone(id) && d.id === id )){

execute.push({
cb: d.cb,
ctx: d.ctx? d.ctx: ctx,
data: data
});

// If we only wanted to execute this once.
if (d.one === false){
keep.push(d);
}
} else {
keep.push(d);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ playerjs.Receiver.prototype.receive = function(e){
if (data.method === 'addEventListener') {
if (this.eventListeners.hasOwnProperty(data.value)) {
//If the listener is the same, i.e. null only add it once.
if (this.eventListeners[data.value].indexOf(listener) > -1){
if (this.eventListeners[data.value].indexOf(listener) === -1){
this.eventListeners[data.value].push(listener);
}
} else {
Expand Down
67 changes: 66 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*globals asyncTest:true, ok:true, start:true, playerjs:true*/
/*globals asyncTest:true, ok:true, fail:true, start:true, playerjs:true*/
var FRAMES = [
'http://localhost.com:8003/test/mock.html',
'http://localhost.com:8003/test/html.html',
Expand Down Expand Up @@ -58,6 +58,71 @@ function testCases(){
player.play();
});

// Test to make sure we can attach multiple listeners to the same event.
asyncTest("multi-listeners", 4, function() {

var indexes = [];
var done = function(index){
indexes.push(index);
if (indexes.length === 3){
// Wait for pause.
player.on('pause', function(){
player.off('pause');
player.off('play', one);
player.play();
});
player.pause();
} else if (indexes.length === 5){
// Give it some time to call the 6th play event, otherwise
// suceed.
setTimeout(function(){
var occr = {}, n;
// count occurances in array;
for (var i = 0; i<indexes.length; i++){
n = indexes[i];
if (occr.hasOwnProperty(n)){
occr[n]++;
} else {
occr[n] = 1;
}
}
// Make sure the correct number of play events were done.
ok(occr[1] === 1, 'The proper number of events were registered');
ok(occr[0] === 2, 'The proper number of events were registered');
ok(occr[2] === 2, 'The proper number of events were registered');

ok(true, 'All play events were registered and executed');
player.off('play');
player.pause();
start();
}, 200);
} else if (indexes.length === 6){
// If we get too many events, we should fail.
fail('play event was not removed');
}
};

// Callbacks.
var zero = function(){
done(0);
};

var one = function(){
done(1);
};

var two = function(){
done(2);
};

player.on('play', zero);
player.on('play', one);
player.on('play', two);


player.play();
});

if (player.supports('method', playerjs.METHODS.GETPAUSED)){
asyncTest("getPaused", 1, function() {
player.on('pause', function(){
Expand Down

0 comments on commit 6ff9b95

Please sign in to comment.