added dispatcher package

This commit is contained in:
2013-09-11 15:51:22 +02:00
parent 7f3ba591f3
commit 25e479bb01
8 changed files with 135 additions and 16 deletions

30
haxework/dispath/Dispatcher.hx Executable file
View File

@@ -0,0 +1,30 @@
package haxework.dispath;
import haxe.ds.ObjectMap;
class Dispatcher<L:{}> implements IDispatcher<L> {
private var listeners(null, null):ObjectMap<L, Bool>;
public function new() {
listeners = new ObjectMap<L, Bool>();
}
public function addListener(listener:L):Void {
listeners.set(listener, true);
}
public function removeListener(listener:L):Bool {
return listeners.remove(listener);
}
public function removeAllListeners():Void {
var i:Iterator<L> = listeners.keys();
while (i.hasNext()) listeners.remove(i.next());
}
public function dispatch(caller:L->Void):Void {
var i:Iterator<L> = listeners.keys();
while (i.hasNext()) caller(i.next());
}
}

13
haxework/dispath/IDispatcher.hx Executable file
View File

@@ -0,0 +1,13 @@
package haxework.dispath;
import haxe.ds.ObjectMap;
interface IDispatcher<L:{}> {
private var listeners(null, null):ObjectMap<L, Bool>;
public function addListener(listener:L):Void;
public function removeListener(listener:L):Bool;
public function removeAllListeners():Void;
public function dispatch(caller:L->Void):Void;
}