Skip to content

Instantly share code, notes, and snippets.

@onedayitwillmake
Created April 20, 2016 18:24
Show Gist options
  • Save onedayitwillmake/8bb2bcb6a1a6685dc6b0cc120e6383ee to your computer and use it in GitHub Desktop.
Save onedayitwillmake/8bb2bcb6a1a6685dc6b0cc120e6383ee to your computer and use it in GitHub Desktop.
/**
* A simple "event" emitter for
* Dart inspired by the classical
* stuff used in JavaScript/ActionScript.
*/
library dart_events;
class EventEmitter {
Map<String, List<Function>> _listeners = new Map<String, List<Function>>();
EventEmitter();
on(String type, Function handler) {
// Create the channel if it doesn't exist
_listeners.putIfAbsent(type, () {
return new List<Function>();
});
List<Function> listeners = _listeners[type];
bool exists = listeners.contains(handler);
if (!exists) {
listeners.add(handler);
}
}
off(String type, Function handler) {
if (_listeners.containsKey(type)) {
List<Function> listeners = _listeners[type];
int index = listeners.indexOf(handler);
if (index != -1) {
listeners.removeAt(index);
}
}
}
emit(String type, dynamic data) {
if (_listeners.containsKey(type)) {
List<Function> listeners = _listeners[type];
listeners.forEach((Function item) {
// Getting an exception with this (2013/01/07)
//
// Exception: UnimplementedError: Function.apply not implemented
// Stack Trace: #0 Function.apply (dart:core-patch:729:5)
// Function.apply(item, ...);
// but I'd like to use Function.apply ...
item(data);
});
}
}
Map<String, List<Function>> listeners () => _listeners;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment