diff --git a/src/Startup.hx b/src/Startup.hx index 90b0a40..4aab47c 100644 --- a/src/Startup.hx +++ b/src/Startup.hx @@ -1,33 +1,25 @@ +import kernel.Init; import lib.ui.Observable; import lib.ui.TextElement; import lib.ui.ReactiveUI; -import kernel.Log; -import util.Debug; import kernel.ui.WindowManager; -import kernel.net.Net; import kernel.KernelEvents; using util.Extender.LambdaExtender; class Startup { - static public function main() { - // OS.sleep(Math.random() * 3); // Native lua call to `sleep` - Net.instance.init(); - WindowManager.instance.init(); - - Debug.printBuildInfo(); + public static function main() { + Init.initKernel(); exampleUI(); - Log.moveToOutput("top"); - KernelEvents.instance.startEventLoop(); } static function exampleProgramm() { var context = WindowManager.instance.createNewContext(); - context.clickSignal.on(data -> { + context.clickSignal.handle(data -> { context.setCursorPos(data.pos.x,data.pos.y); context.write("x"); }); @@ -47,7 +39,7 @@ class Startup { ui.render(); - context.clickSignal.on(data -> { + context.clickSignal.handle(data -> { text.set("Holla mundo"); }); diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx new file mode 100644 index 0000000..27cd8dc --- /dev/null +++ b/src/kernel/Init.hx @@ -0,0 +1,22 @@ +package kernel; + +import util.Debug; +import kernel.ui.WindowManager; +import kernel.peripherals.Peripherals.Peripheral; +import kernel.net.Net; + +class Init { + public static function initKernel() { + // Init singeltons here because haxe is confused about the order to create them. + KernelEvents.instance = new KernelEvents(); + Peripheral.instance = new Peripheral(); + + WindowManager.instance = new WindowManager(); + MainTerm.instance = new MainTerm(); + Net.instance = new Net(); + + Log.init(); + + Debug.printBuildInfo(); + } +} diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index 7cf2b3e..0b6e278 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -1,7 +1,6 @@ package kernel; import cc.OS; -import lua.Coroutine; import util.EventBus; using lua.Table; @@ -10,7 +9,9 @@ using lua.Table; Class for interacting with the native pullEvent system. **/ class KernelEvents{ - public static final instance:KernelEvents = new KernelEvents(); + public static var instance:KernelEvents; + + @:allow(kernel.Init) private function new () {} private var eventBus: util.EventBus> = new EventBus(); @@ -33,15 +34,15 @@ class KernelEvents{ } } - public function on(eventName:String, callback:Array -> Void):EventBusListner> { + public function on(eventName:String, callback:Array -> Void):EventBusListner { return eventBus.on(eventName,callback); } - public function once(eventName:String, callback:Array -> Void):EventBusListner> { + public function once(eventName:String, callback:Array -> Void):EventBusListner { return eventBus.once(eventName,callback); } - public function removeListner(id:EventBusListner>) { + public function removeListner(id:EventBusListner) { return eventBus.removeListner(id); } } diff --git a/src/kernel/Log.hx b/src/kernel/Log.hx index d683d90..cccf2f6 100644 --- a/src/kernel/Log.hx +++ b/src/kernel/Log.hx @@ -1,6 +1,5 @@ package kernel; -import util.Debug; import kernel.ui.WindowContext; import kernel.ui.WindowManager; import lib.TermWriteable; @@ -10,8 +9,14 @@ import lib.TermIO; Log messages to specified output. **/ class Log { - private static final context:WindowContext = WindowManager.instance.createNewContext(); - private static var writer:TermIO = new TermIO(context); + private static var context:WindowContext; + private static var writer:TermIO; + + @:allow(kernel.Init) + private static function init() { + Log.context = WindowManager.instance.createNewContext(); + Log.writer = new TermIO(Log.context); + } private static function setMainoutout(newOutput: TermWriteable) { writer = new TermIO(newOutput); diff --git a/src/kernel/MainTerm.hx b/src/kernel/MainTerm.hx index 2224be2..4d8850f 100644 --- a/src/kernel/MainTerm.hx +++ b/src/kernel/MainTerm.hx @@ -1,6 +1,7 @@ package kernel; -import util.Signal; +using tink.CoreApi; + import lib.TermWriteable; import cc.Term; import util.Vec.Vec2; @@ -10,19 +11,21 @@ import util.Color; Represents the main computer screen. **/ class MainTerm implements TermWriteable{ - public static final instance:MainTerm = new MainTerm(); + public static var instance:MainTerm; + public var onResize(default, null):Signal>; + + private var onResizeTrigger:SignalTrigger>; + + @:allow(kernel.Init) private function new() { + this.onResizeTrigger = Signal.trigger(); + this.onResize = this.onResizeTrigger.asSignal(); + KernelEvents.instance.on("term_resize",params ->{ - _onResize.emit(null); + onResizeTrigger.trigger(getSize()); }); } - - public var onResize(get, null):SignalReadonly>; - private var _onResize:Signal> = new Signal(); - function get_onResize():SignalReadonly> { - return _onResize; - } public function write(text:String) { Term.write(text); diff --git a/src/kernel/Timer.hx b/src/kernel/Timer.hx index 9f657c8..39a26a3 100644 --- a/src/kernel/Timer.hx +++ b/src/kernel/Timer.hx @@ -1,5 +1,7 @@ package kernel; +using tink.CoreApi; + import util.EventBus.EventBusListner; import cc.OS; @@ -8,19 +10,19 @@ import cc.OS; **/ class Timer { private final timerID:Int; - private final callback:Void->Void; - private final timerListner:EventBusListner>; + private final callback:Callback; + private final timerListner:EventBusListner; /** Create new timer with timeout in seconds. **/ - public function new(timeout: Int, cb: Void->Void) { + public function new(timeout: Int, callback: Callback) { timerID = OS.startTimer(timeout); - callback = cb; + this.callback = callback; timerListner = KernelEvents.instance.on("timer",(params)->{ if (params[1] == timerID){ - cb(); + callback.invoke(null); KernelEvents.instance.removeListner(timerListner); } }); diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index efc424f..f9332e8 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -17,8 +17,18 @@ using util.Extender.LambdaExtender; Used to send and recceive packages. **/ class Net{ - public static final instance:Net = new Net(); - private function new () {} + public static var instance:Net; + + @:allow(kernel.Init) + private function new () { + KernelEvents.instance.on("modem_message",(params)->{ + var pack = Package.fromEvent(params); + handelIncomming(pack,params[1]); + }); + allModems = Peripheral.instance.getModems(); + open(); + discoverNeighbors(); + } public static inline final BRODCAST_PORT:Int = 65533; public static inline final MESSAGE_TIMEOUT:Int = 3; @@ -29,16 +39,6 @@ class Net{ private var allModems:Array; private var routingTable: Map = new Map(); - public function init() { - KernelEvents.instance.on("modem_message",(params)->{ - var pack = Package.fromEvent(params); - handelIncomming(pack,params[1]); - }); - allModems = Peripheral.instance.getModems(); - open(); - discoverNeighbors(); - } - private function handelIncomming(pack: Package, ?addr:String) { switch pack.type { case Data(_): diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index 1e4dd62..6be9dd9 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -10,7 +10,9 @@ using Lambda; Class responseable for retrieving peripherals. **/ class Peripheral { - public static final instance:Peripheral = new Peripheral(); + public static var instance:Peripheral; + + @:allow(kernel.Init) private function new() {} /** diff --git a/src/kernel/peripherals/Screen.hx b/src/kernel/peripherals/Screen.hx index c8e2dc6..09734ad 100644 --- a/src/kernel/peripherals/Screen.hx +++ b/src/kernel/peripherals/Screen.hx @@ -1,6 +1,7 @@ package kernel.peripherals; -import util.Signal; +using tink.CoreApi; + import cc.Term.TerminalSize; import lib.TermWriteable; import util.Vec.Vec2; @@ -11,14 +12,21 @@ class Screen implements TermWriteable{ private final addr:String; + private final onResizeTrigger: SignalTrigger>; + public var onResize(default,null):Signal>; + + @:allow(kernel.peripherals) public function new(nativePeripherals: cc.periphs.Monitor.Monitor,addr: String) { + this.onResizeTrigger = Signal.trigger(); + this.onResize = onResizeTrigger.asSignal(); + this.nativ = nativePeripherals; this.addr = addr; KernelEvents.instance.on("monitor_resize",params -> { if (params[1] == this.addr){ - _onResize.emit(null); + onResizeTrigger.trigger(getSize()); } }); @@ -29,13 +37,6 @@ class Screen implements TermWriteable{ return this.addr; } - public var onResize(get,null):SignalReadonly>; - private final _onResize:Signal> = new Signal(); - - function get_onResize():SignalReadonly> { - return _onResize; - } - public function getTextScale(): Float { return nativ.getTextScale(); } @@ -109,4 +110,3 @@ class Screen implements TermWriteable{ return nativ.isColor(); } } - diff --git a/src/kernel/ui/TermBuffer.hx b/src/kernel/ui/TermBuffer.hx index 21a59eb..1c08ab8 100644 --- a/src/kernel/ui/TermBuffer.hx +++ b/src/kernel/ui/TermBuffer.hx @@ -1,6 +1,7 @@ package kernel.ui; -import util.Signal; +using tink.CoreApi; + import util.Vec.Vec2; import util.Color; import lib.TermWriteable; @@ -21,14 +22,19 @@ class TermBuffer implements TermWriteable { private var currentTextColor: Color = White; private var currentBgColor: Color = Black; private var size: Vec2 = {x: 51,y:19}; // Default size set to default size of the main terminal + + public var onResize(default,null):Signal>; + private final onResizeTrigger:SignalTrigger>; public function new() { + this.onResizeTrigger = Signal.trigger(); + this.onResize = onResizeTrigger.asSignal(); initScreenBuffer(size); } private function setSize(size: Vec2) { if (this.size != size){ - this._onResize.emit(size); + this.onResizeTrigger.trigger(size); } this.size = size; @@ -96,13 +102,6 @@ class TermBuffer implements TermWriteable { // TermWriteable functions // - public var onResize(get,null):SignalReadonly>; - private final _onResize:Signal> = new Signal(); - - function get_onResize():Signal> { - return _onResize; - } - public function write(text:String) { for (i in 0...text.length){ safeWriteScreenBuffer({x: cursorPos.x,y: cursorPos.y},text.charAt(i)); @@ -171,4 +170,3 @@ class TermBuffer implements TermWriteable { throw new haxe.exceptions.NotImplementedException(); } } - diff --git a/src/kernel/ui/VirtualTermWriter.hx b/src/kernel/ui/VirtualTermWriter.hx index a2ae115..56807f7 100644 --- a/src/kernel/ui/VirtualTermWriter.hx +++ b/src/kernel/ui/VirtualTermWriter.hx @@ -1,6 +1,7 @@ package kernel.ui; -import util.Signal.SignalListner; +using tink.CoreApi; + import util.Vec.Vec2; import util.Color; import lib.TermWriteable; @@ -10,7 +11,7 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer { private var target: TermWriteable; private var enabled:Bool = false; - private var onResizeListner: SignalListner>; + private var onResizeLink: CallbackLink; public function new(?target: TermWriteable) { setTarget(target); @@ -42,12 +43,12 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer { super.setSize(newTarget.getSize()); // Remove old target event listner - if (onResizeListner != null && target != null){ - target.onResize.remove(onResizeListner); + if (onResizeLink != null){ + onResizeLink.cancel(); } // Add new target event listner - onResizeListner = newTarget.onResize.on(newSize -> { + onResizeLink = newTarget.onResize.handle(newSize -> { setSuperSize(newSize); }); diff --git a/src/kernel/ui/WindowContext.hx b/src/kernel/ui/WindowContext.hx index 767c734..8240929 100644 --- a/src/kernel/ui/WindowContext.hx +++ b/src/kernel/ui/WindowContext.hx @@ -1,7 +1,8 @@ package kernel.ui; +using tink.CoreApi; + import util.Color; -import util.Signal; import kernel.ui.WindowManager.ButtonType; import util.Vec.Vec2; import lib.TermWriteable; @@ -9,31 +10,46 @@ import lib.TermWriteable; class WindowContext implements TermWriteable { private final writer:VirtualTermWriter; - private final _clickSignal:Signal<{button: ButtonType, pos: Vec2}> = new Signal(); - private final _keySignal:Signal<{keyCode: Int, isHeld: Bool}> = new Signal(); - private final _keyUpSignal:Signal = new Signal(); - private final _mouseDragSignal:Signal<{button: ButtonType, pos: Vec2}> = new Signal(); - private final _mouseScrollSignal:Signal<{dir: Int,pos: Vec2}> = new Signal(); - private final _mouseUpSignal:Signal<{button: ButtonType,pos: Vec2}> = new Signal(); - private final _pasteSignal:Signal = new Signal(); + public var clickSignal(default,null):Signal<{button: ButtonType, pos: Vec2}>; + public var keySignal(default,null):Signal<{keyCode: Int, isHeld: Bool}>; + public var keyUpSignal(default,null):Signal; + public var mouseDragSignal(default,null):Signal<{button: ButtonType, pos: Vec2}>; + public var mouseScrollSignal(default,null):Signal<{dir: Int,pos: Vec2}>; + public var mouseUpSignal(default,null):Signal<{button: ButtonType,pos: Vec2}>; + public var pasteSignal(default,null):Signal ; + + @:allow(kernel.ui.WindowManager) private final clickTrigger:SignalTrigger<{button: ButtonType, pos: Vec2}>; + @:allow(kernel.ui.WindowManager) private final keyTrigger:SignalTrigger<{keyCode: Int, isHeld: Bool}>; + @:allow(kernel.ui.WindowManager) private final keyUpTrigger:SignalTrigger; + @:allow(kernel.ui.WindowManager) private final mouseDragTrigger:SignalTrigger<{button: ButtonType, pos: Vec2}>; + @:allow(kernel.ui.WindowManager) private final mouseScrollTrigger:SignalTrigger<{dir: Int,pos: Vec2}>; + @:allow(kernel.ui.WindowManager) private final mouseUpTrigger:SignalTrigger<{button: ButtonType,pos: Vec2}>; + @:allow(kernel.ui.WindowManager) private final pasteTrigger:SignalTrigger; - public var clickSignal(get,null):SignalReadonly<{button: ButtonType, pos: Vec2}>; - public var keySignal(get,null):SignalReadonly<{keyCode: Int, isHeld: Bool}>; - public var keyUpSignal(get,null):SignalReadonly; - public var mouseDragSignal(get,null):SignalReadonly<{button: ButtonType, pos: Vec2}>; - public var mouseScrollSignal(get,null):SignalReadonly<{dir: Int,pos: Vec2}>; - public var mouseUpSignal(get,null):SignalReadonly<{button: ButtonType,pos: Vec2}>; - public var pasteSignal(get,null):SignalReadonly ; public function new(writer: VirtualTermWriter) { this.writer = writer; + this.onResize = writer.onResize; + + this.clickTrigger = Signal.trigger(); + this.keyTrigger = Signal.trigger(); + this.keyUpTrigger = Signal.trigger(); + this.mouseDragTrigger = Signal.trigger(); + this.mouseScrollTrigger = Signal.trigger(); + this.mouseUpTrigger = Signal.trigger(); + this.pasteTrigger = Signal.trigger(); + + this.clickSignal = clickTrigger.asSignal(); + this.keySignal = keyTrigger.asSignal(); + this.keyUpSignal = keyUpTrigger.asSignal(); + this.mouseDragSignal = mouseDragTrigger.asSignal(); + this.mouseScrollSignal = mouseScrollTrigger.asSignal(); + this.mouseUpSignal = mouseUpTrigger.asSignal(); + this.pasteSignal = pasteTrigger.asSignal(); + } - public var onResize(get, null):SignalReadonly>; - - function get_onResize():SignalReadonly> { - return writer.onResize; - } + public var onResize(default, null):Signal>; @:allow(kernel.ui) private function setTarget(target: TermWriteable) { @@ -55,69 +71,6 @@ class WindowContext implements TermWriteable { return writer.isEnabled(); } - public function get_clickSignal(){ - return _clickSignal; - } - - public function get_keySignal(){ - return _keySignal; - } - - public function get_keyUpSignal(){ - return _keyUpSignal; - } - - public function get_mouseDragSignal(){ - return _mouseDragSignal; - } - - public function get_mouseScrollSignal(){ - return _mouseScrollSignal; - } - - public function get_mouseUpSignal(){ - return _mouseUpSignal; - } - - public function get_pasteSignal(){ - return _pasteSignal; - } - - @:allow(kernel.ui.WindowManager) // Package private - private function click(button:ButtonType ,pos: Vec2) { - _clickSignal.emit({button: button,pos: pos}); - } - - @:allow(kernel.ui.WindowManager) // Package private - private function key(keyCode: Int, isHeld: Bool) { - _keySignal.emit({keyCode: keyCode,isHeld: isHeld}); - } - - @:allow(kernel.ui.WindowManager) // Package private - private function keyUp(keyCode: Int) { - _keyUpSignal.emit(keyCode); - } - - @:allow(kernel.ui.WindowManager) // Package private - private function mouseDrag(button: ButtonType, pos: Vec2) { - _mouseDragSignal.emit({button: button,pos: pos}); - } - - @:allow(kernel.ui.WindowManager) // Package private - private function mouseScroll(dir: Int,pos: Vec2) { - _mouseScrollSignal.emit({dir: dir,pos: pos}); - } - - @:allow(kernel.ui.WindowManager) // Package private - private function mouseUp(button: ButtonType,pos: Vec2) { - _mouseUpSignal.emit({button: button,pos: pos}); - } - - @:allow(kernel.ui.WindowManager) // Package private - private function paste(text: String) { - _pasteSignal.emit(text); - } - public function write(text:String) { writer.write(text); } diff --git a/src/kernel/ui/WindowManager.hx b/src/kernel/ui/WindowManager.hx index f41ccc8..0908a89 100644 --- a/src/kernel/ui/WindowManager.hx +++ b/src/kernel/ui/WindowManager.hx @@ -1,7 +1,6 @@ package kernel.ui; import lib.TermWriteable; -import kernel.peripherals.Screen; import kernel.peripherals.Peripherals.Peripheral; import haxe.Exception; import util.Vec.Vec2; @@ -13,26 +12,22 @@ enum ButtonType { } class WindowManager { - public static final instance:WindowManager = new WindowManager(); - private function new() {} + public static var instance:WindowManager; - private var currentMainContext:WindowContext; - private final allContexts:Array = new Array(); - private final outputMap:Map = new Map(); - - public function init() { + @:allow(kernel.Init) + private function new() { KernelEvents.instance.on("key",params -> { var keyCode: Int = params[1]; var isHeld: Bool = params[2]; if (currentMainContext != null){ - currentMainContext.key(keyCode,isHeld); + currentMainContext.keyTrigger.trigger({keyCode: keyCode,isHeld: isHeld}); } }); KernelEvents.instance.on("key_up",params -> { var keyCode: Int = params[1]; if (currentMainContext != null){ - currentMainContext.keyUp(keyCode); + currentMainContext.keyUpTrigger.trigger(keyCode); } }); @@ -44,7 +39,7 @@ class WindowManager { }; if (currentMainContext != null){ - currentMainContext.click(button,clickPos); + currentMainContext.clickTrigger.trigger({button: button,pos: clickPos}); } }); @@ -55,7 +50,7 @@ class WindowManager { y: (params[3]:Int) - 1, } if (currentMainContext != null){ - currentMainContext.mouseDrag(button,pos); + currentMainContext.mouseDragTrigger.trigger({button: button,pos: pos}); } }); @@ -67,7 +62,7 @@ class WindowManager { } if (currentMainContext != null){ - currentMainContext.mouseScroll(dir,pos); + currentMainContext.mouseScrollTrigger.trigger({dir: dir,pos: pos}); } }); @@ -79,7 +74,7 @@ class WindowManager { } if (currentMainContext != null){ - currentMainContext.mouseUp(button,pos); + currentMainContext.mouseUpTrigger.trigger({button: button,pos: pos}); } }); @@ -87,7 +82,7 @@ class WindowManager { var text: String = params[1]; if (currentMainContext != null){ - currentMainContext.paste(text); + currentMainContext.pasteTrigger.trigger(text); } }); @@ -96,12 +91,16 @@ class WindowManager { }); } + private var currentMainContext:WindowContext; + private final allContexts:Array = new Array(); + private final outputMap:Map = new Map(); + public function createNewContext(): WindowContext { var newContext = new WindowContext(new VirtualTermWriter()); allContexts.push(newContext); - newContext.setTarget(MainTerm.instance); + // newContext.setTarget(MainTerm.instance); newContext.enable(); currentMainContext = newContext; diff --git a/src/lib/TermWriteable.hx b/src/lib/TermWriteable.hx index c9b0bca..b90da58 100644 --- a/src/lib/TermWriteable.hx +++ b/src/lib/TermWriteable.hx @@ -1,6 +1,7 @@ package lib; -import util.Signal; +using tink.CoreApi; + import util.Color; import util.Vec.Vec2; @@ -9,7 +10,7 @@ import util.Vec.Vec2; **/ interface TermWriteable { - public var onResize(get,null): SignalReadonly>; + public var onResize(default,null): Signal>; public function write(text: String): Void; public function scroll(y: Int): Void; diff --git a/src/util/EventBus.hx b/src/util/EventBus.hx index ba52981..ce9839a 100644 --- a/src/util/EventBus.hx +++ b/src/util/EventBus.hx @@ -1,17 +1,17 @@ package util; -import util.Signal.SignalListner; +using tink.CoreApi; -class EventBusListner { +class EventBusListner { @:allow(util.EventBus) - private final signalListner:SignalListner; + private final link:CallbackLink; @:allow(util.EventBus) private final eventName:String; @:allow(util.EventBus) - private function new(signalListner: SignalListner,eventName: String) { - this.signalListner = signalListner; + private function new(link: CallbackLink,eventName: String) { + this.link = link; this.eventName = eventName; } } @@ -20,43 +20,42 @@ class EventBusListner { Generic event handler. **/ class EventBus{ - private var listner: Map> = new Map(); + private var listner: Map> = new Map(); public function new() { } - public function on(eventName: String, callback: T->Void):EventBusListner{ + public function on(eventName: String, callback: Callback):EventBusListner{ if (!listner.exists(eventName)){ - listner[eventName] = new Signal(); + listner[eventName] = Signal.trigger(); } - var signalListner = listner[eventName].on(callback); - return new EventBusListner(signalListner,eventName); + var link = listner[eventName].asSignal().handle(callback); + return new EventBusListner(link,eventName); } - public function once(eventName: String,callback: T->Void):EventBusListner { + public function once(eventName: String,callback: Callback):EventBusListner { if (!listner.exists(eventName)){ - listner[eventName] = new Signal(); + listner[eventName] = Signal.trigger(); } - var signalListner = listner[eventName].once(callback); - return new EventBusListner(signalListner,eventName); + var link = listner[eventName].asSignal().handle(callback); + return new EventBusListner(link,eventName); } - public function emit(eventName: String, data: Any) { + public function emit(eventName: String, data: T) { if (listner.exists(eventName)){ - var signal = listner[eventName]; - signal.emit(data); + var trigger = listner[eventName]; + trigger.trigger(data); } } - public function removeListner(id: EventBusListner) { + public function removeListner(id: EventBusListner) { if (!listner.exists(id.eventName)) { return; } - - listner[id.eventName].remove(id.signalListner); + id.link.cancel(); } } \ No newline at end of file diff --git a/src/util/Signal.hx b/src/util/Signal.hx deleted file mode 100644 index ede89a6..0000000 --- a/src/util/Signal.hx +++ /dev/null @@ -1,61 +0,0 @@ -package util; - -interface SignalReadonly { - public function on(cb: T->Void):SignalListner; - public function once(cb: T->Void):SignalListner; - public function remove(id:SignalListner):Void; -} - -class SignalListner { - private final callback:T->Void; - - @:allow(util.Signal) - private final once:Bool; - - @:allow(util.Signal) - private function new(callback: T->Void,?once: Bool = false) { - this.callback = callback; - this.once = once; - } - - @:allow(util.Signal) - private function invoke(params: T) { - if (callback != null){ - callback(params); - } - } -} - -/** - Simple event system for one event type other than EventBus which has multiple events. -**/ -class Signal implements SignalReadonly{ - public final listner:Array> = new Array(); - - public function new() {} - - public function on(cb: T->Void):SignalListner { - var l = new SignalListner(cb,false); - listner.push(l); - return l; - } - - public function once(cb: T->Void):SignalListner { - var l = new SignalListner(cb,true); - listner.push(l); - return l; - } - - public function emit(data: T) { - for (cb in listner){ - cb.invoke(data); - if (cb.once){ - listner.remove(cb); - } - } - } - - public function remove(id:SignalListner ) { - listner.remove(id); - } -}