Skip to content

Instantly share code, notes, and snippets.

@alexlawrence
Created March 13, 2015 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexlawrence/e41c4c5f15f5992785c5 to your computer and use it in GitHub Desktop.
Save alexlawrence/e41c4c5f15f5992785c5 to your computer and use it in GitHub Desktop.
middlewearable
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