Pure dart package. No dependencies in flutter can be run both in flutter and dart API's.
Inspired by https://github.com/jbogard/MediatR
/// Create the request
class AddRequest extends IRequest<int> {
final int i;
AddRequest(this.i);
}
/// Create a request handler
class AddRequestHandler extends IRequestHandler<int, AddRequest> {
@override
Future<int> call(AddRequest request) async {
return request.i + 1;
}
}
/// Register the handler to the mediator instance ( commonly stored as a singleton )
final mediator = Mediator(Pipeline());
mediator.registerHandler<int, AddRequest, AddRequestHandler>(
() => AddRequestHandler(),
);
/// Send the request througt the mediator instance
final added = await mediator.send<int, AddRequest>(
AddRequest(2),
);
print(added); // prints 3
/// create an event extending IDomainEvent
class MyEvent extends IDomainEvent {
@override
String get name => 'MyEvent';
}
mediator.publish<MyEvent>(MyEvent());
/// subscribe on mediator instance
var unsubscribeFunc = mediator.subscribeWithFunc<MyEvent>((event){
print(event.name);
});
/// call the func returned to unsubscribe
unsubscribeFunc();
class MyEventHandler extends IEventHandler<MyEvent> {
@override
Future<void> call(MyEvent event) {
print(event);
}
}
var eventHandler = MyEventHandler();
mediator.subscribe<MyEvent>(eventHandler);
/// call unsubscribe with the same instance to remove the handler.
mediator.unsubscribe<MyEvent>(eventHandler);
class LoggingBehaviour extends IPipelineBehaviour {
@override
Future proccess(IRequest request, RequestHandlerDelegate next) {
print(request);
return next(request);
}
}
final mediator = Mediator(
Pipeline()
..addMiddleware(
LoggingBehaviour(),
),
);