Root instance & dispatcher run once

This commit is contained in:
2015-04-28 17:02:20 +03:00
parent d0fc36c097
commit 5403b13931
3 changed files with 16 additions and 5 deletions

View File

@@ -10,8 +10,8 @@ class Dispatcher<L:{}> implements IDispatcher<L> {
listeners = new ObjectMap<L, Bool>();
}
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<L:{}> implements IDispatcher<L> {
public function dispatch(caller:L->Void):Void {
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);
}
}

View File

@@ -6,7 +6,7 @@ interface IDispatcher<L:{}> {
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 removeAllListeners():Void;
public function dispatch(caller:L->Void):Void;

View File

@@ -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<Sprite>;
public static var instance(default, null):Root;
public var view(default, null):IView<Sprite>;
public function new(view:IView<Sprite>) {
if (instance != null) throw new Error("Only one instance");
instance = this;
this.view = view;
Lib.current.addChild(view.content);
var content:DisplayObject = view.content;