event_bus 0.1.0

  • 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:

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

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

	dependencies:
	  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 this 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) {
  print(user.name);	
});

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) {
  print(user.name);	
});

subscription.cancel();

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 #

Apache 2.0

Use this package as a library

1. Depend on it

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


dependencies:
  event_bus: ^0.1.0

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.