Created
March 13, 2015 09:02
-
-
Save alexlawrence/e41c4c5f15f5992785c5 to your computer and use it in GitHub Desktop.
middlewearable
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
makeFunctionWithMiddleware = (originalFunction) -> | |
_allFunctions = [originalFunction] | |
decoratedFunction = -> | |
_allFunctions[0].apply null, arguments | |
decoratedFunction.use = (middleware) -> | |
nextFunction = _allFunctions[_allFunctions.length - 1] | |
_allFunctions.unshift (args...) -> middleware.apply null, args.concat nextFunction | |
decoratedFunction | |
someFunction = (a, b) -> | |
console.log a, b | |
someFunction = makeFunctionWithMiddleware someFunction | |
someFunction.use (a, b, next) -> | |
if a is 'Hello' | |
next a, b | |
someFunction.use (a, b, next) -> | |
if a is 'World' | |
next a, b | |
someFunction 'Hello', 'foo' | |
someFunction 'bar', 'World' | |
someFunction 'Hello', 'World' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment