trough 1.0.0
trough: ^1.0.0 copied to clipboard
A middleware pipeline library for Dart, inspired by similar libraries in the JavaScript ecosystem. Create composable function pipelines for processing data in sequence.
import 'package:trough/trough.dart';
void main() {
// Create a new pipeline
final pipeline = trough();
// Add middleware - multiply input by 2
pipeline.use((List<dynamic> input, [Function? next]) {
final value = input.isNotEmpty ? input[0] as int : 0;
next?.call(null, [value * 2]);
return null;
});
// Add another middleware - add 10
pipeline.use((List<dynamic> input, [Function? next]) {
final value = input.isNotEmpty ? input[0] as int : 0;
next?.call(null, [value + 10]);
return null;
});
// Run the pipeline
pipeline.run([5], (Object? error, [List<dynamic>? output]) {
if (error != null) {
print('Error: $error');
} else {
print(
'Result: ${output?.first}',
); // Should output: Result: 20 (5 * 2 + 10)
}
});
// Synchronous middleware example
final syncPipeline = trough();
syncPipeline.use((List<dynamic> input, [Function? next]) {
final text = input.isNotEmpty ? input[0] as String : '';
return '$text World!';
});
syncPipeline.run(['Hello'], (Object? error, [List<dynamic>? output]) {
if (error != null) {
print('Error: $error');
} else {
print(
'Sync result: ${output?.first}',
); // Should output: Sync result: Hello World!
}
});
}