Skip to content

Instantly share code, notes, and snippets.

@3assy2018
Created July 4, 2020 09:37
Show Gist options
  • Save 3assy2018/f750e327d00982d833fca443998f8f3f to your computer and use it in GitHub Desktop.
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.
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