Skip to content

Commit

Permalink
Migrate to Dart 2
Browse files Browse the repository at this point in the history
- BREAKING CHANGE: The `on` method now has a generic type. The type must be passed as a type argument instead of a method parameter. Change `myEventBus.on(MyEventType)` to `myEventBus.on<MyEventType>()`.
- BREAKING CHANGE: Every `EventBus` is now hierarchical so that listeners will also receive events of subtypes of the specified type. This is exactly the way that `HierarchicalEventBus` worked. So `HierarchicalEventBus` has been removed. Use the normal `EventBus` instead.
  • Loading branch information
marcojakob committed Jul 6, 2018
1 parent a425126 commit 0d7b88c
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 414 deletions.
13 changes: 9 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
.buildlog
.DS_Store
.idea
# Files and directories created by pub
.packages
.pub/
build/
packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock

# Files generated by dart tools
.dart_tool

# Directory created by dartdoc
doc/api/
78 changes: 36 additions & 42 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,85 @@
# Changelog

## Version 0.4.1 (2015-05-13)
## Version 1.0.0 (2018-07-06)

- Migrate to Dart 2.
- BREAKING CHANGE: The `on` method now has a generic type. The type must be passed as a type argument instead of a method parameter. Change `myEventBus.on(MyEventType)` to `myEventBus.on<MyEventType>()`.
- BREAKING CHANGE: Every `EventBus` is now hierarchical so that listeners will also receive events of subtypes of the specified type. This is exactly the way that `HierarchicalEventBus` worked. So `HierarchicalEventBus` has been removed. Use the normal `EventBus` instead.

* Fix Issue #13: Improve on() stream when no type is specified
## Version 0.4.1 (2015-05-13)

- Fix Issue #13: Improve on() stream when no type is specified

## Version 0.4.0 (2015-05-03)

* BREAKING CHANGE: Moved the `HierarchicalEventBus` to a separate library to
be able to remove `dart:mirrors` from the normal `EventBus`.
- BREAKING CHANGE: Moved the `HierarchicalEventBus` to a separate library to
be able to remove `dart:mirrors` from the normal `EventBus`.
Users of the hierarchical event bus must import `event_bus_hierarchical.dart`
and replace the use of the factory constructor `EventBus.hierarchical()` with
the `HierarchicalEventBus` constructor.


## Version 0.3.0 (2014-09-08)

* BREAKING CHANGE: Changed and simplified the EventBus API. We can now dispatch
- BREAKING CHANGE: Changed and simplified the EventBus API. We can now dispatch
any Dart object as an event. Before, we had to create an EventType for every
type of event we wanted to fire. Now we can use any class as an event.
type of event we wanted to fire. Now we can use any class as an event.
Listeners can (optionally) filter events by that class.
* Added a way to create a **hierarchical event bus** that filters events by
class and their subclasses. This currently only works with classes
**extending** other classes and not with **implementing** an interface.
We might have to wait for
- Added a way to create a **hierarchical event bus** that filters events by
class and their subclasses. This currently only works with classes
**extending** other classes and not with **implementing** an interface.
We might have to wait for
https://code.google.com/p/dart/issues/detail?id=20756 to enable interfaces.
* BREAKING CHANGE: The EventBus constructor defaults to **async instead of
sync**!!. This matches the constructor of the Dart Streams and an async event
- BREAKING CHANGE: The EventBus constructor defaults to **async instead of
sync**!!. This matches the constructor of the Dart Streams and an async event
bus might also be the more common use case.
* BREAKING CHANGE: Removed LoggingEventBus. Reason is that logging can easily
- BREAKING CHANGE: Removed LoggingEventBus. Reason is that logging can easily
be implemented with a event listener that listens for all events and logs
them.


## Version 0.2.5 (2014-09-03)

* Update example.
* Update readme with new links.
* Update dependencies.

- Update example.
- Update readme with new links.
- Update dependencies.

## Version 0.2.4 (2013-11-11)

* Update to dart libraries 0.9.0.

- Update to dart libraries 0.9.0.

## Version 0.2.3 (2013-09-16)

* Fix issue #8: Add logging of events that flow through event bus

- Fix issue #8: Add logging of events that flow through event bus

## Version 0.2.2 (2013-09-16)

* Change default of SimpleEventBus to sync (same as factory in EventBus)

- Change default of SimpleEventBus to sync (same as factory in EventBus)

## Version 0.2.1 (2013-07-01)

* Fix issue #6: Fire should accept null as data

- Fix issue #6: Fire should accept null as data

## Version 0.2.0 (2013-06-06)

* Update to new Dart SDK v0.5.13.1_r23552.
* Using Darts new Stream.broadcast() factory.
* Provide option for synchronous broadcasting of events.
* Update unit tests and example.
* Create demo page.


## Version 0.1.3 (2013-05-19)
- Update to new Dart SDK v0.5.13.1_r23552.
- Using Darts new Stream.broadcast() factory.
- Provide option for synchronous broadcasting of events.
- Update unit tests and example.
- Create demo page.

* Removed all occurrences of @override
## Version 0.1.3 (2013-05-19)

- Removed all occurrences of @override

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


## Version 0.1.1 (2013-04-29)

* Minor change in README to fix image links.

- Minor change in README to fix image links.

## Version 0.1.0 (2013-04-29)

* Initial Version.
- Initial Version.
62 changes: 23 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# Event Bus

A simple Event Bus using Dart [Streams](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.Stream)
A simple Event Bus using Dart [Streams](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.Stream)
for decoupling applications.

[![Star this Repo](https://img.shields.io/github/stars/marcojakob/dart-event-bus.svg?style=flat-square)](https://github.com/marcojakob/dart-event-bus)
[![Pub Package](https://img.shields.io/pub/v/event_bus.svg?style=flat-square)](https://pub.dartlang.org/packages/event_bus)
[![Build Status](https://drone.io/github.com/marcojakob/dart-event-bus/status.png)](https://drone.io/github.com/marcojakob/dart-event-bus/latest)

[GitHub](https://github.com/marcojakob/dart-event-bus) |
[Pub](https://pub.dartlang.org/packages/event_bus) |
[GitHub](https://github.com/marcojakob/dart-event-bus) |
[Pub](https://pub.dartlang.org/packages/event_bus) |
[Demos and Examples](http://code.makery.ch/library/dart-event-bus/)


## Event Bus Pattern

An Event Bus follows the publish/subscribe pattern. It allows listeners to
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](http://wikipedia.org/wiki/Model_View_Controller)
The Event Bus pattern is especially helpful for decoupling [MVC](http://wikipedia.org/wiki/Model_View_Controller)
(or [MVP](http://wikipedia.org/wiki/Model_View_Presenter)) applications.

**One group of MVC** is not a problem.
Expand All @@ -38,15 +35,13 @@ By communication through an **Event Bus**, the coupling is reduced.

![Event Bus](https://raw.githubusercontent.com/marcojakob/dart-event-bus/master/doc/event-bus.png)


## 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
Usually there is just one Event Bus per application, but more than one may be
set up to group a specific set of events.

```dart
Expand All @@ -55,22 +50,8 @@ 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.*

```dart
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.*


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

### 2. Define Events

Expand All @@ -79,24 +60,24 @@ Any Dart class can be used as an event.
```dart
class UserLoggedInEvent {
User user;
UserLoggedInEvent(this.user);
}
class NewOrderEvent {
Order order;
NewOrderEvent(this.order);
}
```


### 3. Register Listeners

Register listeners for a **specific events**:
Register listeners for **specific events**:

```dart
eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
eventBus.on<UserLoggedInEvent>().listen((event) {
// All events are of type UserLoggedInEvent (or subtypes of it).
print(event.user);
});
```
Expand All @@ -106,28 +87,26 @@ Register listeners for **all events**:
```dart
eventBus.on().listen((event) {
// Print the runtime type. Such a set up could be used for logging.
print(event.runtimeType);
print(event.runtimeType);
});
```


#### About Dart Streams

`EventBus` uses Dart [Streams](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.Stream)
as its underlying mechanism to keep track of listeners. You may use all
as its underlying mechanism to keep track of listeners. You may use all
functionality available by the [Stream](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.Stream)
API. One example is the use of [StreamSubscriptions](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.StreamSubscription)
to later unsubscribe from the events.

```dart
StreamSubscription loginSubscription = eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
print(event.user);
StreamSubscription loginSubscription = eventBus.on<UserLoggedInEvent>().listen((event) {
print(event.user);
});
loginSubscription.cancel();
```


### 4. Fire Events

Finally, we need to fire an event.
Expand All @@ -137,7 +116,12 @@ User myUser = new User('Mickey');
eventBus.fire(new UserLoggedInEvent(myUser));
```

## Running / Building / Testing

- Run from the terminal: `pub run build_runner serve`
- Build from the terminal: `pub run build_runner build --config release --output build`
- Testing: `pub run build_runner test --fail-on-severe -- -p chrome`

## License

The MIT License (MIT)
The MIT License (MIT)
10 changes: 10 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Lint rules and documentation, see http://dart-lang.github.io/linter/lints
linter:
rules:
- cancel_subscriptions
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- test_types_in_equals
- unrelated_type_equality_checks
- valid_regexps
11 changes: 11 additions & 0 deletions build.debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
targets:
$default:
builders:
build_web_compilers|entrypoint:
generate_for:
- example/example.dart
options:
compiler: dartdevc
dart2js_args:
- --enable-asserts
- --preview-dart-2
15 changes: 15 additions & 0 deletions build.release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
targets:
$default:
builders:
build_web_compilers|entrypoint:
generate_for:
- example/example.dart
options:
compiler: dart2js
dart2js_args:
- --fast-startup
- --minify
- --trust-type-annotations
# Consult https://webdev.dartlang.org/tools/dart2js#size-and-speed-options
# before enabling the following option:
# - --trust-primitives
7 changes: 2 additions & 5 deletions example/events.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* This is an example of how to set up the [EventBus] and its events.
*/
library events;

import 'package:event_bus/event_bus.dart';

/// The global [EventBus] object.
Expand All @@ -11,14 +9,13 @@ EventBus eventBus = new EventBus();
/// Event A.
class MyEventA {
String text;

MyEventA(this.text);
}

/// Event B.
class MyEventB {
String text;

MyEventB(this.text);
}

Loading

0 comments on commit 0d7b88c

Please sign in to comment.