Primitives
Pipeline
@stone-js/pipeline is the chain of responsibility, distilled: send a value through an ordered list of pipes, each free to transform it, pass it on, or stop. It is the single control-flow idea behind the build phase and the middleware chain.
#Send, through, then
Create a pipeline, send a passable value through a list of pipes, and read the result with then. Each pipe receives the value and a next; call it to continue, or return early to short-circuit.
import { Pipeline } from '@stone-js/pipeline'
const result = await Pipeline
.create<number, number>()
.send(1)
.through([
(n, next) => next(n + 1), // 2
(n, next) => next(n * 10), // 20
(n, next) => n > 15 ? 15 : next(n) // clamps, may short-circuit
])
.then((n) => n)
// result === 15A value goes in, flows through ordered pipes, and comes out. Every control flow in the framework is this.
#The three forms of a pipe
A pipe can be a function, a class (with a handle method), or a factory, and can be referenced by alias. Guards like isFunctionPipe and isClassPipeclassify them when a pipeline processes a mixed list.
| Member | Type | Description |
|---|---|---|
| Pipeline.create() | <T, R>() => Pipeline | Start a pipeline. |
| .send(passable) | (value) => this | The value to push through. |
| .through(pipes) | (pipes) => this | The ordered pipes. |
| .via(method) | (name) => this | The method to call on class pipes (default handle). |
| .then(fn) | (fn) => R | Finalise and read the result. |
| .sync(...) | sync variant | Run synchronously when no pipe is async. |
#Why it matters
Middleware is a pipeline over events; the build phase is a pipeline over the manifest; adapters normalise causes with pipelines. Learn this one primitive and you have read the framework's control flow, from setup to response.