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
|
||||
}
|
||||
+12
-11
@@ -1,6 +1,7 @@
|
||||
package ;
|
||||
|
||||
import bindx.Bind;
|
||||
import bindx.Bind.bind in bind;
|
||||
import haxe.unit.TestCase;
|
||||
|
||||
class BaseTest extends TestCase {
|
||||
@@ -13,13 +14,13 @@ class BaseTest extends TestCase {
|
||||
var b = new Bindable1();
|
||||
b.str = "a";
|
||||
var callNum = 0;
|
||||
Bind.bind(b.str, function (from, to) {
|
||||
bind(b.str, function (from, to) {
|
||||
assertEquals(from, "a");
|
||||
assertEquals(to, "b");
|
||||
callNum ++;
|
||||
});
|
||||
|
||||
bindx.Bind.bind(b.str, function (from, to) {
|
||||
bind(b.str, function (from, to) {
|
||||
assertEquals(from, "a");
|
||||
assertEquals(to, "b");
|
||||
callNum ++;
|
||||
@@ -38,12 +39,12 @@ class BaseTest extends TestCase {
|
||||
callNum ++;
|
||||
}
|
||||
|
||||
bindx.Bind.bind(b.str, listener);
|
||||
bindx.Bind.bind(b.str, listener);
|
||||
bind(b.str, listener);
|
||||
bind(b.str, listener);
|
||||
b.str = "";
|
||||
assertEquals(callNum, 1);
|
||||
|
||||
bindx.Bind.bind(b.str, listener);
|
||||
Bind.bind(b.str, listener);
|
||||
bindx.Bind.unbind(b.str, listener);
|
||||
b.str = "1";
|
||||
assertEquals(callNum, 1);
|
||||
@@ -54,12 +55,12 @@ class BaseTest extends TestCase {
|
||||
b.str = null;
|
||||
var callNum = 0;
|
||||
|
||||
bindx.Bind.bind(b.str, function (_, _) callNum++);
|
||||
bindx.Bind.bind(b.str, function (_, _) callNum++);
|
||||
bind(b.str, function (_, _) callNum++);
|
||||
bind(b.str, function (_, _) callNum++);
|
||||
b.str = "";
|
||||
assertEquals(callNum, 2);
|
||||
|
||||
bindx.Bind.unbind(b.str);
|
||||
Bind.unbind(b.str);
|
||||
b.str = "1";
|
||||
assertEquals(callNum, 2);
|
||||
|
||||
@@ -84,10 +85,10 @@ class BaseTest extends TestCase {
|
||||
}
|
||||
|
||||
Bind.bind(b.str, listener);
|
||||
bindx.Bind.notify(b.str, "1", "2");
|
||||
Bind.notify(b.str, "1", "2");
|
||||
assertEquals(callNum, 1);
|
||||
|
||||
bindx.Bind.notify(b.str, "1", "2");
|
||||
Bind.notify(b.str, "1", "2");
|
||||
assertEquals(callNum, 2);
|
||||
}
|
||||
|
||||
@@ -101,7 +102,7 @@ class BaseTest extends TestCase {
|
||||
assertEquals(callNum, 1);
|
||||
assertFalse(Reflect.hasField(b, "noBindChanged"));
|
||||
|
||||
bindx.Bind.notify(b.bind);
|
||||
Bind.notify(b.bind);
|
||||
assertEquals(callNum, 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user