-
-
Save icek/0fed56bd88f9394e1f06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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