Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created June 4, 2015 10:31
Show Gist options
  • Save dz1984/7d0deb681bdbac4f726a to your computer and use it in GitHub Desktop.
Save dz1984/7d0deb681bdbac4f726a to your computer and use it in GitHub Desktop.
Practice to implement the Buffer pattern.
Buffer = {
commands: [],
add: function(fn) {
var commands = this.commands;
var next = function() {
commands.shift();
if (commands.length) commands[0](next);
};
commands.push(fn);
if (this.commands.length == 1) fn(next);
return this;
}
};
var log_1 = function(next) {
console.log('entry log one');
next();
};
var log_2 = function(next) {
console.log('entry log two');
next();
};
var log_3 = function(next) {
console.log('entry log three');
next();
};
Buffer.add(log_1).add(log_2).add(log_3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment