Created
April 8, 2018 15:51
-
-
Save kenanhancer/a123d38b915ae3046ee71d886b964a11 to your computer and use it in GitHub Desktop.
PipeLineDemo1 created by kenanhancer - https://repl.it/@kenanhancer/PipeLineDemo1
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
let pipelineBuilder = (functions, index = 0) => { | |
if (functions.length == index) return env => {}; //Default handler | |
let pipelineFunc = environment => | |
functions[index](environment, pipelineBuilder(functions, index + 1)); | |
return pipelineFunc; | |
}; | |
const middleware1 = (environment, next) => { | |
console.log('middleware1', '\n', JSON.stringify(environment.Person), '\n'); | |
environment.Person.city = 'Istanbul'; | |
next(environment); | |
}; | |
const middleware2 = (environment, next) => { | |
console.log( | |
'middleware2', | |
'\n', | |
JSON.stringify(environment.Person), | |
'\n', | |
'Person has a new field named city coming from middleware1\n' | |
); | |
environment.Person.email = '[email protected]'; | |
next(environment); | |
}; | |
const middleware3 = (environment, next) => { | |
console.log( | |
'middleware3', | |
'\n', | |
JSON.stringify(environment.Person), | |
'\n', | |
'Person has a new field named email coming from middleware2\n' | |
); | |
next(environment); | |
}; | |
const pipelineInvoker = pipelineBuilder([ | |
middleware1, | |
middleware2, | |
middleware3, | |
]); | |
const person = { | |
personId: 1, | |
firstName: 'kenan', | |
lastName: 'HANCER', | |
}; | |
pipelineInvoker({ | |
Person: person, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment