Skip to content

Instantly share code, notes, and snippets.

@hoodwink73
Created August 15, 2016 18:46
Show Gist options
  • Save hoodwink73/3115d1e0aeda38ea94555e2c483697ce to your computer and use it in GitHub Desktop.
Save hoodwink73/3115d1e0aeda38ea94555e2c483697ce to your computer and use it in GitHub Desktop.
Middlewares - a way to facilitate third party extension points around a framework's core operations
// original method
function canBeEnhancedLater (x) {
return x
}
// enhancement modules
function double (next) {
return function (x) {
return next(2 * x)
}
}
function square (next) {
return function (x) {
return next(x * x)
}
}
// enhancement stack
const operations = [double, square]
// internal method to apply enancement
function performEnhancements (operations) {
for (operation of operations) {
// monkey patching
canBeEnhancedLater = operation(canBeEnhancedLater)
}
}
performEnhancements(operations)
canBeEnhancedLater(5) // 50 -> 5 | square | double | identity (original)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment