Giter Site home page Giter Site logo

dart-event-bus's Introduction

Guava Event Bus

This library provides a Google Guava style Event Bus.

EventBus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional in-process event distribution using explicit registration. It is not a general-purpose publish-subscribe system, nor is it intended for interprocess communication.

Synopsis

// Class is typically registered by the container.
class EventBusChangeRecorder {
  @subscribe void recordCustomerChange(ChangeEvent e) {
    recordChange(e.getChange());
  }
}

// somewhere during initialization
eventBus.register(new EventBusChangeRecorder());

// much later
public void changeCustomer() {
  ChangeEvent event = getChangeEvent();
  eventBus.post(event);
}

You can create the event bus with a callback to receive details about exceptions which are thrown by subscribers.

ExceptionHandler handler = (var exception, StackTrace stackTrace, SubscriberExceptionContext context) {
  print(context.event);
};

eventBus = new EventBus.withExceptionHandler(handler);

The exception handler is a typedef, and as such is expected to be a function. To use a class instead just implement the call method:

class CustomExceptionHandler {
  void call(var exception, StackTrace stackTrace, SubscriberExceptionContext context) {
    print(context.event);
  }
}

eventBus = new EventBus.withExceptionHandler(new CustomExceptionHandler());

For Listeners

To listen for a specific flavor of event (say, a CustomerChangeEvent) with EventBus: create a method that accepts CustomerChangeEvent as its sole argument, and mark it with the Subscribe annotation.

To register your listener methods with the event producers with EventBus: pass your object to the EventBus.register(Object) method on an EventBus. You'll need to make sure that your object shares an EventBus instance with the event producers.

To listen for a common event supertype (such as EventObject or Object) with EventBus: events are automatically dispatched to listeners of any supertype, allowing listeners for interface types or "wildcard listeners" for Object.

To listen for and detect events that were dispatched without listeners with EventBus: subscribe to DeadEvent. The EventBus will notify you of any events that were posted but not delivered. (Handy for debugging.)

For Producers

To keep track of listeners to your events with EventBus: EventBus does this for you.

To dispatch an event to listeners with EventBus: pass the event object to an EventBus's EventBus.post(Object) method.

dart-event-bus's People

Contributors

matthewfranglen avatar

Stargazers

Vitaliy Vostrikov avatar

Watchers

 avatar  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.