marcojakob/dart-event-bus

EventBus should accept any Dart object

Closed this issue · 1 comments

We should simplify the EventBus API so that it will accept any Dart object.

At the moment we need to create EventTypes for every type of event we want to fire. By allowing every Dart object to be an Event we would not need to define EventTypes and could get rid of the EventType class alltogether.

An Example

Define an arbitrary class to be used as event:

class UserLoggedInEvent {
  User user;
  UserLoggedInEvent(this.user);
}

Register listeners:

eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
  print(event.user);
});

Fire events:

User myUser = new User('Mickey');
eventBus.fire(new UserLoggedInEvent(myUser));

Advantages

  • No need for EventType, just use normal dart classes.
  • Custom defined event classes can carry as many data fields as needed.
  • No name collisions. Compiler ensures that every event class can only defined once.
  • With an inheritance structure of event classes we could create a hierarchical event bus. This allows us to define a common superclass of some events which we could listen to.

Available in v0.3.0 (6878d3d)