event_bus 0.1.2

  • README.md
  • Installing
  • Versions
  • --

Event Bus #

A simple Event Bus using Dart Streams for decoupling applications.

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.


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. Add Dependency #

Add the folowing to your pubspec.yaml and run pub install

	  event_bus: any

2. Define Events #

import 'package:event_bus/event_bus.dart';

final EventType<User> userLoggedInEvent = new EventType<User>();
final EventType<Order> newOrderEvent = new EventType<Order>();

Note: The generic type of the event (User and Order in this case) is the type of data that will be provided when the event is fired.

3. Create 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.

EventBus eventBus = new EventBus();

This will instantiate the default implementation of EventBus which is SimpleEventBus. You may provide your own EventBus by either extending SimpleEventBus or implementing EventBus.

4. Register Listeners #

Register listeners that will be called whenever the event is fired.

eventBus.on(userLoggedInEvent).listen((User user) {

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<User> subscription = eventBus.on(userLoggedInEvent).listen((User user) {


5. Fire Events #

Finally, we need to fire an event.

eventBus.fire(userLoggedInEvent, new User('Mickey'));

Examples #

Examples are available in the example directory.

License #

The MIT License (MIT)

Changelog #

Version 0.1.0 (2013-04-29) #

  • Initial Version.

Version 0.1.1 (2013-04-29) #

  • Minor change in README to fix image links.

Version 0.1.2 (2013-05-17) #

  • Change in README: contained wrong license (Apache instead of MIT).
  • Remove import 'package:meta/meta.dart' in event_bus.dart as it is not needed and may cause an error if used as pub package.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  event_bus: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:event_bus/event_bus.dart';
Version Uploaded Documentation Archive
1.1.0 Mar 27, 2019 Go to the documentation of event_bus 1.1.0 Download event_bus 1.1.0 archive
1.0.3 Feb 13, 2019 Go to the documentation of event_bus 1.0.3 Download event_bus 1.0.3 archive
1.0.2 Feb 13, 2019 Go to the documentation of event_bus 1.0.2 Download event_bus 1.0.2 archive
1.0.1 Jul 7, 2018 Go to the documentation of event_bus 1.0.1 Download event_bus 1.0.1 archive
1.0.0 Jul 6, 2018 Go to the documentation of event_bus 1.0.0 Download event_bus 1.0.0 archive
0.4.1 May 13, 2015 Go to the documentation of event_bus 0.4.1 Download event_bus 0.4.1 archive
0.4.0 May 3, 2015 Go to the documentation of event_bus 0.4.0 Download event_bus 0.4.0 archive
0.3.0+3 Mar 9, 2015 Go to the documentation of event_bus 0.3.0+3 Download event_bus 0.3.0+3 archive
0.3.0+2 Sep 8, 2014 Go to the documentation of event_bus 0.3.0+2 Download event_bus 0.3.0+2 archive
0.3.0+1 Sep 8, 2014 Go to the documentation of event_bus 0.3.0+1 Download event_bus 0.3.0+1 archive

All 20 versions...

Awaiting analysis to complete.