decompose macro methods and realization
This commit is contained in:
+38
-16
@@ -7,8 +7,9 @@ import haxe.macro.Context;
|
||||
|
||||
using haxe.macro.Tools;
|
||||
using bindx.MetaUtils;
|
||||
#end
|
||||
using Lambda;
|
||||
#end
|
||||
|
||||
|
||||
@:access(bindx.BindMacros)
|
||||
class Bind {
|
||||
@@ -18,8 +19,7 @@ class Bind {
|
||||
}
|
||||
|
||||
@:noUsing macro static public function bindTo(field:Expr, target:Expr):Expr {
|
||||
var fieldData = checkField(field);
|
||||
return BindMacros.bindingSignalProvider.getClassFieldBindToExpr(fieldData.e, fieldData.field, target);
|
||||
return _bindTo(field, target);
|
||||
}
|
||||
|
||||
@:noUsing macro static public function unbind(field:Expr, ?listener:Expr):Expr {
|
||||
@@ -27,19 +27,15 @@ class Bind {
|
||||
}
|
||||
|
||||
@:noUsing macro static public function notify(field:Expr, ?oldValue:Expr, ?newValue:Expr):Expr {
|
||||
var fieldData = checkField(field);
|
||||
return BindMacros.bindingSignalProvider.getClassFieldChangedExpr(fieldData.e, fieldData.field, oldValue, newValue);
|
||||
return _notify(field, oldValue, newValue);
|
||||
}
|
||||
|
||||
@:noUsing macro static public function disposeBindings(object:ExprOf<IBindable>):Expr {
|
||||
var type = Context.typeof(object).follow();
|
||||
if (!isBindable(type.getClass())) {
|
||||
Context.error('\'${object.toString()}\' must be bindx.IBindable', object.pos);
|
||||
}
|
||||
return BindMacros.bindingSignalProvider.getDisposeBindingsExpr(object, type);
|
||||
return _disposeBindings(object);
|
||||
}
|
||||
|
||||
#if macro
|
||||
|
||||
static function _bind(field:Expr, listener:Expr, doBind:Bool):Expr {
|
||||
var fieldData = checkField(field);
|
||||
return if (fieldData != null) {
|
||||
@@ -48,6 +44,24 @@ class Bind {
|
||||
} else macro {};
|
||||
}
|
||||
|
||||
static function _bindTo(field:Expr, target:Expr):Expr {
|
||||
var fieldData = checkField(field);
|
||||
return BindMacros.bindingSignalProvider.getClassFieldBindToExpr(fieldData.e, fieldData.field, target);
|
||||
}
|
||||
|
||||
static function _notify(field:Expr, ?oldValue:Expr, ?newValue:Expr):Expr {
|
||||
var fieldData = checkField(field);
|
||||
return BindMacros.bindingSignalProvider.getClassFieldChangedExpr(fieldData.e, fieldData.field, oldValue, newValue);
|
||||
}
|
||||
|
||||
static function _disposeBindings(object:ExprOf<IBindable>):Expr {
|
||||
var type = Context.typeof(object).follow();
|
||||
if (!isBindable(type.getClass())) {
|
||||
Context.error('\'${object.toString()}\' must be bindx.IBindable', object.pos);
|
||||
}
|
||||
return BindMacros.bindingSignalProvider.getDisposeBindingsExpr(object, type);
|
||||
}
|
||||
|
||||
static function checkField(field:Expr):{e:Expr, field:ClassField} {
|
||||
switch (field.expr) {
|
||||
case EField(e, field):
|
||||
@@ -79,12 +93,20 @@ class Bind {
|
||||
return null;
|
||||
}
|
||||
|
||||
static inline function isBindable(classType:ClassType) {
|
||||
var res = classType.interfaces.exists(function (it) {
|
||||
var t = it.t.get();
|
||||
return t.module == "bindx.IBindable" && t.name == "IBindable";
|
||||
});
|
||||
return res || classType.superClass == null ? res : isBindable(classType.superClass.t.get());
|
||||
static var IBindableType = macro : bindx.IBindable;
|
||||
|
||||
static function isBindable(classType:ClassType) {
|
||||
|
||||
var t = classType;
|
||||
while (t != null) {
|
||||
for (it in t.interfaces) {
|
||||
var t = it.t.get();
|
||||
if (t.module == "bindx.IBindable" && t.name == "IBindable")
|
||||
return true;
|
||||
}
|
||||
t = t.superClass != null ? t.superClass.t.get() : null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#end
|
||||
}
|
||||
Reference in New Issue
Block a user