Skip to content

Instantly share code, notes, and snippets.

@AxGord
Created September 17, 2015 16:20
Show Gist options
  • Save AxGord/db4148a35dbe6df12fb4 to your computer and use it in GitHub Desktop.
Save AxGord/db4148a35dbe6df12fb4 to your computer and use it in GitHub Desktop.
package;
import pony.Priority;
import haxe.Constraints.Function;
#if (cs||java)
typedef Listener1<T1> = {args:Int, f:T1->Void};
#else
typedef Listener1<T1> = T1->Void;
#end
/**
* Signal1
* @author AxGord <[email protected]>
*/
#if !(cs||java)
@:forward(add, remove, empty, clear)
#else
@:forward(empty, clear)
#end
abstract Signal1<T1>(Priority<Listener1<T1>>) from Priority<Listener1<T1>> to Priority<Listener1<T1>> {
@:extern inline public function new(double:Bool = false) {
this = new Priority(double);
#if (cs||java)
this.compare = SignalTools.functionHashCompare;
#end
}
#if (cs||java)
@:extern inline public function add(e:T1->Void, priority:Int = 0):Signal1<T1> {
return this.add({args:1, f:e}, priority);
}
@:extern inline public function add0(f:Void->Void, priority:Int = 0):Signal1<T1> return this.add( { args:0, f:cast f }, priority );
@:op(A << B) @:extern inline private function op_add(listener:T1->Void):Signal1<T1> {
return add(cast listener);
}
@:extern inline public function dispatch(a1:T1):Void for (e in this) {
if (switch e.args {
case 0: (e.f:Function)();
case 1: (e.f:Function)(a1);
case _: throw 'Argument count error';
}) break;
}
#else
@:extern inline public function add0(f:Void->Void, priority:Int = 0):Signal1<T1> return this.add(cast f, priority);
@:op(A << B) @:extern inline private function op_add(listener:T1->Void):Signal1<T1> {
return this.add(listener);
}
public function dispatch(a1:T1):Void for (e in this) {
var f:Function = e;
if (switch SignalTools.functionLength(e) {
case 0: f();
case 1: f(a1);
case _: throw 'Argument count error';
}) break;
}
#end
@:op(A << B) @:extern inline private function op_add0(listener:Void->Void):Signal1<T1> {
return add0(listener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment