bus 0.0.11

Event Bus on Dart

A light-weight event bus library for Dart implementing the pub-sub pattern.

Functionality for JavaScript through dart2js is thanks to dart-patch-mirrors which provides some fixes for the buggy mirror functionality. All the examples provided work with dart2js.

diagram

Send a message to the bus and let the bus distribute the message to various handlers which are subscribed to that type of event. This library utilizes the Stream async library in Dart to handle subscriptions and publishing messages.

Install

See pub.dartlang.org for instructions on how to use bus in your project.

Usage

A simple usage example:

import 'package:bus/bus.dart';

main() async {
  var bus = new Bus<String>();

  var sub = bus.subscribe((String message) {
    print('Received a string: "$message"');
  });

  await bus.post('Hey there!');
  
  // cancel the subscription
  sub.cancel();
  
  // the subscription was cancelled, this is pointless
  bus.post('This message will not be heard...');
}

Also supported is subscribing an object full of handlers:

class GameListener implements Listener {
  @handler
  void _onGame(GameEvent event) {
    print('[An event occurred at ${event.timestamp}]');
  }

  @handler
  void _onChat(ChatEvent event) {
    print('${event.username} says "${event.message}"');
  }
}

...

var bus = new Bus<GameEvent>();
var subs = bus.subscribeAll(new GameListener()); // returns a list of subscriptions

// remember to cancel your event subscriptions
subs.forEach((s) => s.cancel());

See the game example for explicit details.

For your synchronous needs, you can use SyncBus to publish and handle messages synchronously.

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

0.0.10

  • Remove Listener.

0.0.8

  • Add tests (sync, async and listeners tests).

0.0.7

  • Getter for message type
  • Internal changes, util file for helpers

0.0.5

  • Synchronous bus, via new SyncBus<T>().

0.0.2-0.0.4

  • Documentation, README fixes.

0.0.1

  • Initial beta version.

1. Depend on it

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

dependencies:
  bus: "^0.0.11"

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:bus/bus.dart';

Platforms

Server
Web

About

A light-weight event bus implementing the pub-sub pattern.

Author

Keenan Thompson

Homepage

github.com/thekeenant/dart-bus

Documentation

www.dartdocs.org/documentation/bus/0.0.11/

Uploader

keenan@keenant.com

License

MIT (LICENSE)

Published

Oct 17, 2017