Created
August 15, 2016 18:46
-
-
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
This file contains hidden or 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
// 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