Created
July 19, 2013 20:30
-
-
Save frostney/6042112 to your computer and use it in GitHub Desktop.
EventMap in HaXe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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