Skip to content

Instantly share code, notes, and snippets.

@keeto
Created June 26, 2010 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeto/454220 to your computer and use it in GitHub Desktop.
Save keeto/454220 to your computer and use it in GitHub Desktop.
Mediator: Object grouping and brokering
/*
Script: Mediator
Object grouping and brokering
Copyright and License:
Copyright 2010, Mark Obcena. MIT-Style License
*/
(function(){
this.Mediator = new Type('Mediator', function(){
this.$collected = [];
this.$chain = [];
if (arguments.length){
this.$collected.concat(Array.prototype.slice(arguments));
}
});
var start = function(fn, args){
var obj = this.$collected.shift();
fn.apply(obj, args);
this.push(obj);
};
Mediator.implement({
push: function(obj){
this.$collected.push(obj);
if (this.$chain.length){
var fn = this.$chain.shift();
start.call(this, fn.fn, fn.args);
}
return this;
},
shift: function(){
return this.$collected.shift();
},
call: function(fn, args){
if (!this.$collected.length) this.$chain.push({fn: fn, args: args});
else start.call(this, fn, args);
return this;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment