Skip to content

Instantly share code, notes, and snippets.

@frostney
Created July 19, 2013 20:30
Show Gist options
  • Save frostney/6042112 to your computer and use it in GitHub Desktop.
Save frostney/6042112 to your computer and use it in GitHub Desktop.
EventMap in HaXe
package ;
/**
* ...
* @author Johannes Stein
*/
class EventMap
{
private var map: Map<String, Array<Dynamic -> Void>>;
public function new() {
map = new Map<String, Array<Dynamic -> Void>>();
}
public function on(eventName: String, fn: Dynamic -> Void) {
if (map.exists(eventName)) {
var value = map.get(eventName);
value.push(fn);
map.set(eventName, value);
} else {
var arr = new Array<Dynamic -> Void>();
arr.push(fn);
map.set(eventName, arr);
}
}
public function off(eventName: String) {
if (map.exists(eventName)) {
map.remove(eventName);
}
}
public function trigger(eventName: String, ?args: Dynamic) {
if (map.exists(eventName)) {
var value = map.get(eventName);
for (i in value) {
i.bind(args)();
}
}
}
public function exists(eventName: String): Bool {
return map.exists(eventName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment