Created
June 26, 2010 17:40
-
-
Save keeto/454220 to your computer and use it in GitHub Desktop.
Mediator: Object grouping and brokering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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