Skip to content

Commit

Permalink
Simplify Group and also pass the array with arguments where the event…
Browse files Browse the repository at this point in the history
…s were called with
  • Loading branch information
Arian committed Jul 31, 2011
1 parent 9f36b46 commit 1053a9a
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions Source/Utilities/Group.js
Expand Up @@ -27,34 +27,28 @@ this.Group = new Class({

initialize: function(){
this.instances = Array.flatten(arguments);
this.events = {};
this.checker = {};
},

addEvent: function(type, fn){
this.checker[type] = this.checker[type] || {};
this.events[type] = this.events[type] || [];
if (this.events[type].contains(fn)) return false;
else this.events[type].push(fn);
this.instances.each(function(instance, i){
instance.addEvent(type, this.check.pass([type, instance, i], this));
}, this);
return this;
},

check: function(type, instance, i){
this.checker[type][i] = true;
var every = this.instances.every(function(current, j){
return this.checker[type][j] || false;
}, this);
if (!every) return;
this.checker[type] = {};
this.events[type].each(function(event){
event.call(this, this.instances, instance);
}, this);
var instances = this.instances,
len = instances.length,
togo = len,
args = new Array(len),
self = this;

instances.each(function(instance, i){
instance.addEvent(type, function(){
if (!args[i]) togo--;
args[i] = arguments;
if (!togo){
fn.call(self, instances, instance, args);
togo = len;
args = new Array(len);
}
});
});
}

});

})();

0 comments on commit 1053a9a

Please sign in to comment.