Skip to content

Commit 0d7b88c

Browse files
committed
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.
1 parent a425126 commit 0d7b88c

17 files changed

+294
-414
lines changed

.gitignore

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
.buildlog
2-
.DS_Store
3-
.idea
1+
# Files and directories created by pub
2+
.packages
43
.pub/
54
build/
6-
packages
5+
# Remove the following pattern if you wish to check in your lock file
76
pubspec.lock
7+
8+
# Files generated by dart tools
9+
.dart_tool
10+
11+
# Directory created by dartdoc
12+
doc/api/

CHANGELOG.md

+36-42
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,85 @@
11
# Changelog
22

3-
## Version 0.4.1 (2015-05-13)
3+
## Version 1.0.0 (2018-07-06)
4+
5+
- Migrate to Dart 2.
6+
- 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>()`.
7+
- 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.
48

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

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

813
## Version 0.4.0 (2015-05-03)
914

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

16-
1721
## Version 0.3.0 (2014-09-08)
1822

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

35-
3639
## Version 0.2.5 (2014-09-03)
3740

38-
* Update example.
39-
* Update readme with new links.
40-
* Update dependencies.
41-
41+
- Update example.
42+
- Update readme with new links.
43+
- Update dependencies.
4244

4345
## Version 0.2.4 (2013-11-11)
4446

45-
* Update to dart libraries 0.9.0.
46-
47+
- Update to dart libraries 0.9.0.
4748

4849
## Version 0.2.3 (2013-09-16)
4950

50-
* Fix issue #8: Add logging of events that flow through event bus
51-
51+
- Fix issue #8: Add logging of events that flow through event bus
5252

5353
## Version 0.2.2 (2013-09-16)
5454

55-
* Change default of SimpleEventBus to sync (same as factory in EventBus)
56-
55+
- Change default of SimpleEventBus to sync (same as factory in EventBus)
5756

5857
## Version 0.2.1 (2013-07-01)
5958

60-
* Fix issue #6: Fire should accept null as data
61-
59+
- Fix issue #6: Fire should accept null as data
6260

6361
## Version 0.2.0 (2013-06-06)
6462

65-
* Update to new Dart SDK v0.5.13.1_r23552.
66-
* Using Darts new Stream.broadcast() factory.
67-
* Provide option for synchronous broadcasting of events.
68-
* Update unit tests and example.
69-
* Create demo page.
70-
71-
72-
## Version 0.1.3 (2013-05-19)
63+
- Update to new Dart SDK v0.5.13.1_r23552.
64+
- Using Darts new Stream.broadcast() factory.
65+
- Provide option for synchronous broadcasting of events.
66+
- Update unit tests and example.
67+
- Create demo page.
7368

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

71+
- Removed all occurrences of @override
7672

7773
## Version 0.1.2 (2013-05-17)
7874

79-
* Change in README: contained wrong license (Apache instead of MIT).
80-
* Remove import 'package:meta/meta.dart' in event_bus.dart as it is not needed
75+
- Change in README: contained wrong license (Apache instead of MIT).
76+
- Remove import 'package:meta/meta.dart' in event_bus.dart as it is not needed
8177
and may cause an error if used as pub package.
8278

83-
8479
## Version 0.1.1 (2013-04-29)
8580

86-
* Minor change in README to fix image links.
87-
81+
- Minor change in README to fix image links.
8882

8983
## Version 0.1.0 (2013-04-29)
9084

91-
* Initial Version.
85+
- Initial Version.

README.md

+23-39
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# Event Bus
22

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

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

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

14-
1513
## Event Bus Pattern
1614

17-
An Event Bus follows the publish/subscribe pattern. It allows listeners to
15+
An Event Bus follows the publish/subscribe pattern. It allows listeners to
1816
subscribe for events and publishers to fire events. This enables objects to
1917
interact without requiring to explicitly define listeners and keeping track of
2018
them.
2119

22-
2320
### Event Bus and MVC
2421

25-
The Event Bus pattern is especially helpful for decoupling [MVC](http://wikipedia.org/wiki/Model_View_Controller)
22+
The Event Bus pattern is especially helpful for decoupling [MVC](http://wikipedia.org/wiki/Model_View_Controller)
2623
(or [MVP](http://wikipedia.org/wiki/Model_View_Presenter)) applications.
2724

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

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

41-
4238
## Usage
4339

44-
4540
### 1. Create an Event Bus
4641

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

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

5247
```dart
@@ -55,22 +50,8 @@ import 'package:event_bus/event_bus.dart';
5550
EventBus eventBus = new EventBus();
5651
```
5752

58-
You can alternatively use the `HierarchicalEventBus` that filters events by
59-
event class **including** its subclasses.
60-
61-
*Note that the hierarchical event bus uses `dart:mirrors` which support in
62-
dart2js is experimental.*
63-
64-
```dart
65-
import 'package:event_bus/event_bus_hierarchical.dart';
66-
67-
EventBus eventBus = new HierarchicalEventBus();
68-
```
69-
70-
**Note:** *The default constructor will create an asynchronous event bus. To
71-
create a synchronous you must provide the optional `sync: true` attribute.*
72-
73-
53+
**Note:** _The default constructor will create an asynchronous event bus. To
54+
create a synchronous bus you must provide the optional `sync: true` attribute._
7455

7556
### 2. Define Events
7657

@@ -79,24 +60,24 @@ Any Dart class can be used as an event.
7960
```dart
8061
class UserLoggedInEvent {
8162
User user;
82-
63+
8364
UserLoggedInEvent(this.user);
8465
}
8566
8667
class NewOrderEvent {
8768
Order order;
88-
69+
8970
NewOrderEvent(this.order);
9071
}
9172
```
9273

93-
9474
### 3. Register Listeners
9575

96-
Register listeners for a **specific events**:
76+
Register listeners for **specific events**:
9777

9878
```dart
99-
eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
79+
eventBus.on<UserLoggedInEvent>().listen((event) {
80+
// All events are of type UserLoggedInEvent (or subtypes of it).
10081
print(event.user);
10182
});
10283
```
@@ -106,28 +87,26 @@ Register listeners for **all events**:
10687
```dart
10788
eventBus.on().listen((event) {
10889
// Print the runtime type. Such a set up could be used for logging.
109-
print(event.runtimeType);
90+
print(event.runtimeType);
11091
});
11192
```
11293

113-
11494
#### About Dart Streams
11595

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

122102
```dart
123-
StreamSubscription loginSubscription = eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
124-
print(event.user);
103+
StreamSubscription loginSubscription = eventBus.on<UserLoggedInEvent>().listen((event) {
104+
print(event.user);
125105
});
126106
127107
loginSubscription.cancel();
128108
```
129109

130-
131110
### 4. Fire Events
132111

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

119+
## Running / Building / Testing
120+
121+
- Run from the terminal: `pub run build_runner serve`
122+
- Build from the terminal: `pub run build_runner build --config release --output build`
123+
- Testing: `pub run build_runner test --fail-on-severe -- -p chrome`
140124

141125
## License
142126

143-
The MIT License (MIT)
127+
The MIT License (MIT)

analysis_options.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Lint rules and documentation, see http://dart-lang.github.io/linter/lints
2+
linter:
3+
rules:
4+
- cancel_subscriptions
5+
- hash_and_equals
6+
- iterable_contains_unrelated_type
7+
- list_remove_unrelated_type
8+
- test_types_in_equals
9+
- unrelated_type_equality_checks
10+
- valid_regexps

build.debug.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
targets:
2+
$default:
3+
builders:
4+
build_web_compilers|entrypoint:
5+
generate_for:
6+
- example/example.dart
7+
options:
8+
compiler: dartdevc
9+
dart2js_args:
10+
- --enable-asserts
11+
- --preview-dart-2

build.release.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
targets:
2+
$default:
3+
builders:
4+
build_web_compilers|entrypoint:
5+
generate_for:
6+
- example/example.dart
7+
options:
8+
compiler: dart2js
9+
dart2js_args:
10+
- --fast-startup
11+
- --minify
12+
- --trust-type-annotations
13+
# Consult https://webdev.dartlang.org/tools/dart2js#size-and-speed-options
14+
# before enabling the following option:
15+
# - --trust-primitives

example/events.dart

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* This is an example of how to set up the [EventBus] and its events.
33
*/
4-
library events;
5-
64
import 'package:event_bus/event_bus.dart';
75

86
/// The global [EventBus] object.
@@ -11,14 +9,13 @@ EventBus eventBus = new EventBus();
119
/// Event A.
1210
class MyEventA {
1311
String text;
14-
12+
1513
MyEventA(this.text);
1614
}
1715

1816
/// Event B.
1917
class MyEventB {
2018
String text;
21-
19+
2220
MyEventB(this.text);
2321
}
24-

0 commit comments

Comments
 (0)