Pipeline<T extends Object?> class
A composable pipeline of middleware that processes values asynchronously.
Pipeline builds and executes a chain of middleware around a terminal handler. Middleware are registered via use and executed in the order they were added when the pipeline is called.
Execution order: When you call the pipeline, middleware execute in an "onion layer" pattern:
- First middleware's pre-processing runs
- Second middleware's pre-processing runs
- ... and so on until reaching the core handler
- The core handler executes
- Then post-processing runs in reverse order (last to first)
Example execution trace:
final pipeline = Pipeline<String>();
pipeline.use(mw1); // Added first
pipeline.use(mw2); // Added second
pipeline.use(mw3); // Added third
pipeline('hello');
// Execution order:
// → mw1 pre-processing
// → mw2 pre-processing
// → mw3 pre-processing
// → core handler
// ← mw3 post-processing
// ← mw2 post-processing
// ← mw1 post-processing
This pattern is useful for:
- Logging and monitoring: Track all values flowing through
- Error handling: Wrap operations in try-catch
- Validation: Check and validate inputs before core logic
- Performance timing: Measure execution duration
- Authentication/Authorization: Check permissions before processing
- Transformation: Modify values before or after processing
Complete example:
// Create a pipeline for processing strings
final pipeline = Pipeline<String>();
// Add logging middleware
pipeline.use((next) => (input) async {
print('Start: $input');
final result = await next(input);
print('End: $result');
return result;
});
// Add timing middleware
pipeline.use((next) => (input) async {
final sw = Stopwatch()..start();
final result = await next(input);
sw.stop();
print('Duration: ${sw.elapsedMilliseconds}ms');
return result;
});
// Add transformation middleware
pipeline.use((next) => (input) async {
final transformed = input.toUpperCase();
return await next(transformed);
});
// Call the pipeline
final result = await pipeline('hello');
// Logs:
// Start: HELLO
// Duration: 0ms
// End: HELLO
- Annotations
-
- @experimental
Constructors
- Pipeline()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
T input) → Future< T> - Executes the pipeline with the given input.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
use(
Middleware< T> middleware) → void - Registers a middleware in this pipeline.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited