added dispatcher package
This commit is contained in:
30
haxework/dispath/Dispatcher.hx
Executable file
30
haxework/dispath/Dispatcher.hx
Executable 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
13
haxework/dispath/IDispatcher.hx
Executable 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;
|
||||
}
|
||||
Reference in New Issue
Block a user