Git Product home page Git Product logo

dart-event-bus's Introduction

Event Bus

A simple Event Bus using Dart Streams for decoupling applications.

Star this Repo Pub Package Build Status

GitHub | Pub | Demos and Examples

Event Bus Pattern

An Event Bus follows the publish/subscribe pattern. It allows listeners to subscribe for events and publishers to fire events. This enables objects to interact without requiring to explicitly define listeners and keeping track of them.

Event Bus and MVC

The Event Bus pattern is especially helpful for decoupling MVC (or MVP) applications.

One group of MVC is not a problem.

Model-View-Controller

But as soon as there are multiple groups of MVCs, those groups will have to talk to each other. This creates a tight coupling between the controllers.

Multi Model-View-Controllers

By communication through an Event Bus, the coupling is reduced.

Event Bus

Usage

1. Create an Event Bus

Create an instance of EventBus and make it available to other classes.

Usually there is just one Event Bus per application, but more than one may be set up to group a specific set of events.

import 'package:event_bus/event_bus.dart';

EventBus eventBus = new EventBus();

You can alternatively use the HierarchicalEventBus that filters events by event class including its subclasses.

Note that the hierarchical event bus uses dart:mirrors which support in dart2js is experimental.

import 'package:event_bus/event_bus_hierarchical.dart';

EventBus eventBus = new HierarchicalEventBus();

Note: The default constructor will create an asynchronous event bus. To create a synchronous you must provide the optional sync: true attribute.

2. Define Events

Any Dart class can be used as an event.

class UserLoggedInEvent {
  User user;
  
  UserLoggedInEvent(this.user);
}

class NewOrderEvent {
  Order order;
  
  NewOrderEvent(this.order);
}

3. Register Listeners

Register listeners for a specific events:

eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
  print(event.user);
});

Register listeners for all events:

eventBus.on().listen((event) {
  // Print the runtime type. Such a set up could be used for logging.
  print(event.runtimeType); 
});

About Dart Streams

EventBus uses Dart Streams as its underlying mechanism to keep track of listeners. You may use all functionality available by the Stream API. One example is the use of StreamSubscriptions to later unsubscribe from the events.

StreamSubscription loginSubscription = eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
  print(event.user);	
});

loginSubscription.cancel();

4. Fire Events

Finally, we need to fire an event.

User myUser = new User('Mickey');
eventBus.fire(new UserLoggedInEvent(myUser));

License

The MIT License (MIT)

dart-event-bus's People

Contributors

marcojakob avatar

Watchers

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