diff --git a/haxework/dispath/Dispatcher.hx b/haxework/dispath/Dispatcher.hx index 67530ac..c9ca6c3 100755 --- a/haxework/dispath/Dispatcher.hx +++ b/haxework/dispath/Dispatcher.hx @@ -10,8 +10,8 @@ class Dispatcher implements IDispatcher { listeners = new ObjectMap(); } - public function addListener(listener:L):Void { - listeners.set(listener, true); + public function addListener(listener:L, once:Bool = false):Void { + listeners.set(listener, once); } public function removeListener(listener:L):Bool { @@ -25,6 +25,12 @@ class Dispatcher implements IDispatcher { public function dispatch(caller:L->Void):Void { var i:Iterator = listeners.keys(); - while (i.hasNext()) caller(i.next()); + var r:Array = []; + while (i.hasNext()) { + var l = i.next(); + caller(l); + if (listeners.get(l)) r.push(l); + }; + for (l in r) listeners.remove(l); } } \ No newline at end of file diff --git a/haxework/dispath/IDispatcher.hx b/haxework/dispath/IDispatcher.hx index a9ef139..3bd09d9 100755 --- a/haxework/dispath/IDispatcher.hx +++ b/haxework/dispath/IDispatcher.hx @@ -6,7 +6,7 @@ interface IDispatcher { private var listeners(null, null):ObjectMap; - public function addListener(listener:L):Void; + public function addListener(listener:L, once:Bool = false):Void; public function removeListener(listener:L):Bool; public function removeAllListeners():Void; public function dispatch(caller:L->Void):Void; diff --git a/haxework/gui/Root.hx b/haxework/gui/Root.hx index 3dcee6f..ade3d96 100755 --- a/haxework/gui/Root.hx +++ b/haxework/gui/Root.hx @@ -1,5 +1,6 @@ package haxework.gui; +import flash.errors.Error; import flash.Lib; import flash.display.StageAlign; import flash.display.StageScaleMode; @@ -9,9 +10,13 @@ import flash.display.Sprite; class Root { - private var view:IView; + public static var instance(default, null):Root; + + public var view(default, null):IView; public function new(view:IView) { + if (instance != null) throw new Error("Only one instance"); + instance = this; this.view = view; Lib.current.addChild(view.content); var content:DisplayObject = view.content;