Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created August 15, 2016 14:12
Show Gist options
  • Save francescoagati/62649442a15640a0e6475638e32a0a51 to your computer and use it in GitHub Desktop.
Save francescoagati/62649442a15640a0e6475638e32a0a51 to your computer and use it in GitHub Desktop.
haxe 3.3 handling events with abstracts resolve macro and inlining objects
import haxe.macro.Expr;
#if macro
import haxe.macro.Context;
using tink.MacroApi;
using haxe.macro.ExprTools;
using haxe.macro.Context;
#end
#if !macro
class Wrapper {
public var property:String;
public var obj:js.html.Element;
public inline function new(property:String,obj:js.html.Element) {
this.property = property;
this.obj =obj;
}
public inline function handle(fn:Dynamic->Void) obj.addEventListener(property,fn);
}
#end
abstract EventHandler(Dynamic) from Dynamic to Dynamic {
@:resolve public static macro function resolve(self:Dynamic,name:ExprOf<String>) {
return macro new EventHandler.Wrapper($name,$self);
}
}
class Main {
static function main() {
var x:EventHandler = js.Browser.document.createElement('div');
x.click.handle(function(e) {});
}
}
// Generated by Haxe 3.3.0
(function () { "use strict";
var Main = function() { };
Main.main = function() {
var x = window.document.createElement("div");
var _this_property = "click";
var _this_obj = x;
_this_obj.addEventListener(_this_property,function(e) {
});
};
Main.main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment