Skip to content

Instantly share code, notes, and snippets.

@icek
Forked from back2dos/Signal.hx
Last active August 29, 2015 14:19
Show Gist options
  • Save icek/0fed56bd88f9394e1f06 to your computer and use it in GitHub Desktop.
Save icek/0fed56bd88f9394e1f06 to your computer and use it in GitHub Desktop.
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
#end
abstract Signal<T>(Array<T>)
{
public inline function new()
{
this = [];
}
@:op(A+=B)
public inline function add(listener:T):Void
{
this.push(listener);
}
@:op(A-=B)
public inline function remove(listener:T):Void
{
var i = 0;
while (i < this.length)
{
if (Reflect.compareMethods(this[i], listener))
this.splice(i, 1);
else
i++;
}
}
public macro function dispatch(ethis:Expr, args:Array<Expr>):Expr
{
return macro
for (__handler in @:privateAccess $ethis.asArray())
__handler($a{args});
}
inline function asArray():Array<T> return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment