Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active December 23, 2015 09:19
Show Gist options
  • Save back2dos/6613772 to your computer and use it in GitHub Desktop.
Save back2dos/6613772 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 = [];
}
public inline function add(listener:T):Void
{
this.push(listener);
}
private inline function asArray():Array<T> return this;
public function remove(listener:T):Void
{
var i = 0;
while (i < this.length)
{
if (Reflect.compareMethods(this[i], listener))
this.splice(i, 1);
else
i++;
}
}
static public macro function dispatch(ethis:Expr, args:Array<Expr>):Expr
{
return macro
for (__handler in @:privateAccess $ethis.asArray())
__handler($a{args});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment