Created
June 4, 2015 10:31
-
-
Save dz1984/7d0deb681bdbac4f726a to your computer and use it in GitHub Desktop.
Practice to implement the Buffer pattern.
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
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