package util; import util.Signal.SignalListner; class EventBusListner { @:allow(util.EventBus) private final signalListner:SignalListner; @:allow(util.EventBus) private final eventName:String; @:allow(util.EventBus) private function new(signalListner: SignalListner,eventName: String) { this.signalListner = signalListner; this.eventName = eventName; } } /** Generic event handler. **/ class EventBus{ private var listner: Map> = new Map(); public function new() { } public function on(eventName: String, callback: T->Void):EventBusListner{ if (!listner.exists(eventName)){ listner[eventName] = new Signal(); } var signalListner = listner[eventName].on(callback); return new EventBusListner(signalListner,eventName); } public function once(eventName: String,callback: T->Void):EventBusListner { if (!listner.exists(eventName)){ listner[eventName] = new Signal(); } var signalListner = listner[eventName].once(callback); return new EventBusListner(signalListner,eventName); } public function emit(eventName: String, data: Any) { if (listner.exists(eventName)){ var signal = listner[eventName]; signal.emit(data); } } public function removeListner(id: EventBusListner) { if (!listner.exists(id.eventName)) { return; } listner[id.eventName].remove(id.signalListner); } }