Coder Social home page Coder Social logo

bytes7bytes7 / dart_mediatr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from georgopoulosgiannis/dart_mediatr

0.0 0.0 0.0 260 KB

Dart central mediator for sending queries, commands , events, etc.

Home Page: https://pub.dev/packages/mediatr

License: BSD 3-Clause "New" or "Revised" License

C++ 31.91% C 2.17% Objective-C 0.06% Kotlin 0.19% Dart 33.46% Swift 1.77% HTML 2.77% CMake 27.67%

dart_mediatr's Introduction

Dart mediator package

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 and send a request

 /// 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

Publishing events

/// create an event extending IDomainEvent
class MyEvent extends IDomainEvent {
  @override
  String get name => 'MyEvent';
}

mediator.publish<MyEvent>(MyEvent());

Subscribe on events with functions

/// subscribe on mediator instance
var unsubscribeFunc = mediator.subscribeWithFunc<MyEvent>((event){
 print(event.name);
});

/// call the func returned to unsubscribe
unsubscribeFunc();

Subscribe on events with a class instance

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);

Adding a middleware for all requests

class LoggingBehaviour extends IPipelineBehaviour {
  @override
  Future proccess(IRequest request, RequestHandlerDelegate next) {
    print(request);
    return next(request);
  }
}

final mediator = Mediator(
  Pipeline()
    ..addMiddleware(
      LoggingBehaviour(),
    ),
);

dart_mediatr's People

Contributors

georgopoulosgiannis avatar bytes7bytes7 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.