2

Using Closure Library, you can give any object the ability to dispatch events extending goog.events.EventTarget. Is this currently possible using Dart libraries?

I imagine it would look like this:

#import('dart:html');

class Foo implements EventTarget {
  Events get on() {
    // ???
  }
}

main() {
  Foo foo = new Foo();

  // Subscribe to the event.
  foo.on['bar'].add((Event event) => print('bar!'));

  // Dispatch the event.
  foo.on['bar'].dispatch(new Event('bar'));
}

Am I on the right track?

Edit Thanks to Lars Tackmann, a working draft is here: http://try.dartlang.org/s/f6wk

2 Answers 2

2

You can do this in multiple ways, one method could be to use typedef's to define a generic handler function:

class Event {
   final String type;
   Event(this.type);
}

typedef EventHandler(Event event);

interface EventTarget {
  Map<String,EventHandler> get on();
  void dispatch(Event event);
}

class Foo implements EventTarget {
  Map<String, EventHandler> _handlers;
  Foo() {
    _handlers = new Map();
  } 

  Map<String, EventHandler> get on() {
    return _handlers;
  }

  dispatch(Event event) {
     EventHandler handler = _handlers[event.type];
     handler(event);
  }
}

main() {
  Foo foo = new Foo();

  foo.on['bar'] = (Event event) => print('handling event ${event.type}');

  foo.dispatch(new Event('bar'));
}

I made a DartBoard snippet for you here to play around with.

You might want to experiment with making EventTarget into a base class, unless of cause it messes up your inheritance strategy (in which case a event bus you can inject with a factory might be a better fit).

2
  • Woah. It looks a lot simpler than I thought. Thanks for the complete example and dartboard snippet. :) The event bus pattern is new to me but I'll read more about it.
    – moraes
    Commented Dec 25, 2011 at 13:07
  • 1
    Your DartBoard snippet allows a single listener to be registered (see: try.dartlang.org/s/8rsk). Here is a version that supports multiple: try.dartlang.org/s/LBkk
    – moraes
    Commented Dec 25, 2011 at 21:02
2

I think you are, but I favor a more robust event model for internal objects than the one used to wrap DOM events in Dart. The one I use models more after the .net event model, and allows passing of both the source object, and an EventArgs class (or sub-class) carrying data related to the event.

https://github.com/LUCA-Studios-LLC/LUCA-UI-Framework-for-Dart/blob/master/core/FrameworkEvent.dart

looks like:

FrameworkEvent<EventArgs> myEvent = new FrameworkEvent<EventArgs>();

//here subscribing using custom operator override "+"    
var handler = myEvent + (Dynamic source, EventArgs args) {
 //do stuff when the event fires here
};

//fire the event
myEvent.invoke(this, new EventArgs());

//unsubscribe using operator override "-"
myEvent - handler; //unsubscribe from the event here
1
  • Right now I'm favoring an interface that stay as close as possible to dart:html's events interface. But your approach is interesting and I want to play around with this.
    – moraes
    Commented Dec 25, 2011 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.