Created
July 4, 2020 09:37
-
-
Save 3assy2018/f750e327d00982d833fca443998f8f3f to your computer and use it in GitHub Desktop.
This code implements pipeline pattern using ES6. It is inspired by Laravel Framework pipeline pattern.
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
export default class Pipeline { | |
passable = null; | |
pipes = []; | |
method = 'handle'; | |
send = (passable) => { | |
this.passable = passable; | |
return this; | |
}; | |
through = (pipes) => { | |
this.pipes = pipes; | |
return this; | |
}; | |
via = (method) => { | |
this.method = method; | |
return this; | |
}; | |
then = (destination) => { | |
return this.pipes.reverse().reduce(this.carry, (passable) => destination(passable))(this.passable); | |
}; | |
carry = (stack, pipe) => { | |
return (passable) => { | |
let params = [passable, stack]; | |
if(this.method in pipe){ | |
return pipe[this.method](...params); | |
} | |
return pipe(passable, stack); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment