A Pipeline allows you to pass data through a series of pipes to perform a sequence of operations with the data. Each pipe is a callable piece of code: A class method, a function. Since each pipe operates on the data in isolation (the pipes don't know or care about each other), then that means you can easily compose complex workflows out of reusable actions that are also very easy to test because they aren't interdependent.
A simple usage example:
import 'package:pipeline_plus/pipeline_plus.dart';
var pipeline = Pipeline()
..send(data: User())
..onFailure(callback: (passable, exception) {
// do something
})
..through(
pipes: [
RegisterUserService(),
AddMemberToTeamService(),
(User user) {
// do something
return user;
},
SendWelcomeEmailService(),
],
);
By implementing PipelineMixin on a class, you can use the pipeThrough method for the class.
class User with PipelineMixin {}
import 'package:pipeline_plus/pipeline_plus.dart';
var userPipeline = User().pipeThrough(
pipes: [
RegisterUserService(),
AddMemberToTeamService(),
(User user) {
// do something
return user;
},
SendWelcomeEmailService(),
],
);
class SendWelcomeEmailService implements Pipe<User> {
@override
Future<User> handle(User user) async {
print('The welcome email is being sent.');
user.welcomeEmailIsSent = true;
return user;
}
}
Set the data being sent through the pipeline.
..send(data: User())
Set the list of pipes.
..through(
pipes: [
RegisterUserService(),
AddMemberToTeamService(),
(User user) {
user.teamId = 1000;
return user;
},
SendWelcomeEmailService(),
],
)
Push additional pipes onto the pipeline.
..pipe(
pipes: [
AdditionalService(),
],
)
Set callback to be executed on failure pipeline.
..onFailure(callback: (passable, exception) {
// do something
})
Run the pipeline with a final destination callback.
pipeline.then(callback: (data){
// do something
});
Run the pipeline and return the result.
var data = await pipeline.thenReturn();