Root instance & dispatcher run once
This commit is contained in:
@@ -10,8 +10,8 @@ class Dispatcher<L:{}> implements IDispatcher<L> {
|
|||||||
listeners = new ObjectMap<L, Bool>();
|
listeners = new ObjectMap<L, Bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addListener(listener:L):Void {
|
public function addListener(listener:L, once:Bool = false):Void {
|
||||||
listeners.set(listener, true);
|
listeners.set(listener, once);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeListener(listener:L):Bool {
|
public function removeListener(listener:L):Bool {
|
||||||
@@ -25,6 +25,12 @@ class Dispatcher<L:{}> implements IDispatcher<L> {
|
|||||||
|
|
||||||
public function dispatch(caller:L->Void):Void {
|
public function dispatch(caller:L->Void):Void {
|
||||||
var i:Iterator<L> = listeners.keys();
|
var i:Iterator<L> = listeners.keys();
|
||||||
while (i.hasNext()) caller(i.next());
|
var r:Array<L> = [];
|
||||||
|
while (i.hasNext()) {
|
||||||
|
var l = i.next();
|
||||||
|
caller(l);
|
||||||
|
if (listeners.get(l)) r.push(l);
|
||||||
|
};
|
||||||
|
for (l in r) listeners.remove(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ interface IDispatcher<L:{}> {
|
|||||||
|
|
||||||
private var listeners(null, null):ObjectMap<L, Bool>;
|
private var listeners(null, null):ObjectMap<L, Bool>;
|
||||||
|
|
||||||
public function addListener(listener:L):Void;
|
public function addListener(listener:L, once:Bool = false):Void;
|
||||||
public function removeListener(listener:L):Bool;
|
public function removeListener(listener:L):Bool;
|
||||||
public function removeAllListeners():Void;
|
public function removeAllListeners():Void;
|
||||||
public function dispatch(caller:L->Void):Void;
|
public function dispatch(caller:L->Void):Void;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package haxework.gui;
|
package haxework.gui;
|
||||||
|
|
||||||
|
import flash.errors.Error;
|
||||||
import flash.Lib;
|
import flash.Lib;
|
||||||
import flash.display.StageAlign;
|
import flash.display.StageAlign;
|
||||||
import flash.display.StageScaleMode;
|
import flash.display.StageScaleMode;
|
||||||
@@ -9,9 +10,13 @@ import flash.display.Sprite;
|
|||||||
|
|
||||||
class Root {
|
class Root {
|
||||||
|
|
||||||
private var view:IView<Sprite>;
|
public static var instance(default, null):Root;
|
||||||
|
|
||||||
|
public var view(default, null):IView<Sprite>;
|
||||||
|
|
||||||
public function new(view:IView<Sprite>) {
|
public function new(view:IView<Sprite>) {
|
||||||
|
if (instance != null) throw new Error("Only one instance");
|
||||||
|
instance = this;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
Lib.current.addChild(view.content);
|
Lib.current.addChild(view.content);
|
||||||
var content:DisplayObject = view.content;
|
var content:DisplayObject = view.content;
|
||||||
|
|||||||
Reference in New Issue
Block a user