This is a simple functional pipeline implementation that allows the user to execute middleware through a series of sequential steps. It also exposes a compose function for chaining together higher order components.
import pipeline, { Middleware } from "./pipeline";
const firstStep: Middlware<any, any> = async (payload, next) => {
console.log(payload);
await setTimeout(() => { // this is just an example to show that the steps can be asynchronous
next({ firstStep: 'completed' });
}, 3000);
}
const secondStep: Middleware<any, any> = (payload, next) => {
console.log(payload);
next({ secondStep: 'completed' });
}
const steps = [
firstStep,
secondStep
];
const initialPayload = {
firstStep: null,
secondStep: null
};
pipeline(initialPayload, ...steps);
// Output:
// { firstStep: null, secondStep: null }
// { firstStep: 'completed', secondStep: null }