SignalTools
This commit is contained in:
+26
-27
@@ -7,7 +7,6 @@ import bindx.BindSignal.Signal;
|
|||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
import haxe.macro.Type;
|
import haxe.macro.Type;
|
||||||
import haxe.macro.Context;
|
import haxe.macro.Context;
|
||||||
import haxe.rtti.Meta;
|
|
||||||
|
|
||||||
using bindx.MetaUtils;
|
using bindx.MetaUtils;
|
||||||
using haxe.macro.Tools;
|
using haxe.macro.Tools;
|
||||||
@@ -26,8 +25,6 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
*/
|
*/
|
||||||
static inline var INLINE_SIGNAL_GETTER = "inlineSignalGetter";
|
static inline var INLINE_SIGNAL_GETTER = "inlineSignalGetter";
|
||||||
|
|
||||||
static inline var BIND_SIGNAL_META = "BindSignal";
|
|
||||||
|
|
||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
@:extern static inline function signalName(fieldName:String):String return fieldName + SIGNAL_POSTFIX;
|
@:extern static inline function signalName(fieldName:String):String return fieldName + SIGNAL_POSTFIX;
|
||||||
@@ -75,7 +72,7 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
|
|
||||||
public function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr {
|
public function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr {
|
||||||
var signalName = signalName(field.name);
|
var signalName = signalName(field.name);
|
||||||
return if (!isNull(listener))
|
return if (!listener.isNullOrEmpty())
|
||||||
macro @:privateAccess $expr.$signalName.remove($listener);
|
macro @:privateAccess $expr.$signalName.remove($listener);
|
||||||
else
|
else
|
||||||
macro @:privateAccess $expr.$signalName.removeAll();
|
macro @:privateAccess $expr.$signalName.removeAll();
|
||||||
@@ -84,9 +81,9 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
public function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr {
|
public function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr {
|
||||||
var args = switch (field.kind) {
|
var args = switch (field.kind) {
|
||||||
case FMethod(_):
|
case FMethod(_):
|
||||||
if (!isNull(oldValue))
|
if (!oldValue.isNullOrEmpty())
|
||||||
Context.error("method notify doesn't require oldValue", oldValue.pos);
|
Context.error("method notify doesn't require oldValue", oldValue.pos);
|
||||||
if (!isNull(newValue))
|
if (!newValue.isNullOrEmpty())
|
||||||
Context.error("method notify doesn't require newValue", newValue.pos);
|
Context.error("method notify doesn't require newValue", newValue.pos);
|
||||||
[];
|
[];
|
||||||
case FVar(_, _):
|
case FVar(_, _):
|
||||||
@@ -96,21 +93,7 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getUnbindAllExpr(expr:ExprOf<IBindable>, type:Type):Expr {
|
public function getUnbindAllExpr(expr:ExprOf<IBindable>, type:Type):Expr {
|
||||||
return macro {
|
return macro bindx.BindSignal.SignalTools.unbindAll($expr);
|
||||||
var meta = haxe.rtti.Meta.getFields(std.Type.getClass($expr));
|
|
||||||
if (meta != null) for (m in std.Reflect.fields(meta)) {
|
|
||||||
var data = std.Reflect.field(meta, m);
|
|
||||||
if (std.Reflect.hasField(data, $v{BIND_SIGNAL_META})) {
|
|
||||||
var signal:bindx.BindSignal.Signal<Dynamic> = cast Reflect.field($expr, m);
|
|
||||||
if (signal != null) {
|
|
||||||
signal.removeAll();
|
|
||||||
var args:Array<Dynamic> = Reflect.field(data, $v { BIND_SIGNAL_META } );
|
|
||||||
var lazy:Bool = args[0];
|
|
||||||
if (lazy) Reflect.setField($expr, m, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateSignal(field:Field, type:ComplexType, builder:Expr, res:Array<Field>):Void {
|
function generateSignal(field:Field, type:ComplexType, builder:Expr, res:Array<Field>):Void {
|
||||||
@@ -124,7 +107,7 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
name: signalPrivateName,
|
name: signalPrivateName,
|
||||||
kind: FVar(type, null),
|
kind: FVar(type, null),
|
||||||
pos: field.pos,
|
pos: field.pos,
|
||||||
meta: [ { name:BIND_SIGNAL_META, pos:field.pos, params: [macro true] } ],
|
meta: [ { name:SignalTools.BIND_SIGNAL_META, pos:field.pos, params: [macro true] } ],
|
||||||
access: [APrivate]
|
access: [APrivate]
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -158,7 +141,7 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
kind: FProp("default", "null", type, builder),
|
kind: FProp("default", "null", type, builder),
|
||||||
pos: field.pos,
|
pos: field.pos,
|
||||||
access: [APrivate],
|
access: [APrivate],
|
||||||
meta: [ { name:BIND_SIGNAL_META, pos:field.pos, params: [macro false] } ]
|
meta: [ { name:SignalTools.BIND_SIGNAL_META, pos:field.pos, params: [macro false] } ]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,10 +163,6 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
@:extern inline function hasLazy(meta:MetadataEntry):Bool {
|
@:extern inline function hasLazy(meta:MetadataEntry):Bool {
|
||||||
return meta.findParam(LAZY_SIGNAL).isNullOrTrue();
|
return meta.findParam(LAZY_SIGNAL).isNullOrTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@:extern inline function isNull(expr:Expr):Bool {
|
|
||||||
return expr == null || expr.expr.match(EConst(CIdent("null")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#end
|
#end
|
||||||
@@ -260,4 +239,24 @@ class Signal<T> {
|
|||||||
lock = 0;
|
lock = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SignalTools {
|
||||||
|
static public inline var BIND_SIGNAL_META = "BindSignal";
|
||||||
|
|
||||||
|
static public function unbindAll(bindable:bindx.IBindable):Void {
|
||||||
|
var meta = haxe.rtti.Meta.getFields(std.Type.getClass(bindable));
|
||||||
|
if (meta != null) for (m in std.Reflect.fields(meta)) {
|
||||||
|
var data = std.Reflect.field(meta, m);
|
||||||
|
if (std.Reflect.hasField(data, BIND_SIGNAL_META)) {
|
||||||
|
var signal:bindx.BindSignal.Signal<Dynamic> = cast Reflect.field(bindable, m);
|
||||||
|
if (signal != null) {
|
||||||
|
signal.removeAll();
|
||||||
|
var args:Array<Dynamic> = std.Reflect.field(data, BIND_SIGNAL_META);
|
||||||
|
var lazy:Bool = args[0];
|
||||||
|
if (lazy) std.Reflect.setField(bindable, m, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,8 +22,7 @@ class MetaUtils {
|
|||||||
case {expr:EConst(CIdent(s))}:
|
case {expr:EConst(CIdent(s))}:
|
||||||
if (s == name) res = { expr:(macro true).expr , pos: p.pos };
|
if (s == name) res = { expr:(macro true).expr , pos: p.pos };
|
||||||
case _:
|
case _:
|
||||||
trace(p.expr);
|
Context.warning('Bindable arguments syntax error. Supported syntax: (flag1=true, flag2=false, flag3)', p.pos);
|
||||||
Context.warning('Bindable arguments syntax error. Supported syntax: (flag1=true, flag2=false)', p.pos);
|
|
||||||
}
|
}
|
||||||
if (res != null) break;
|
if (res != null) break;
|
||||||
}
|
}
|
||||||
@@ -71,6 +70,10 @@ class ExprMetaUtils {
|
|||||||
static public inline function isFalse(expr:Expr):Bool
|
static public inline function isFalse(expr:Expr):Bool
|
||||||
return expr.expr.match(EConst(CIdent("false")));
|
return expr.expr.match(EConst(CIdent("false")));
|
||||||
|
|
||||||
|
static public inline function isNullOrEmpty(expr:Expr):Bool {
|
||||||
|
return expr == null || expr.expr.match(EConst(CIdent("null")));
|
||||||
|
}
|
||||||
|
|
||||||
static public inline function isNotNullAndTrue(expr:Expr):Bool
|
static public inline function isNotNullAndTrue(expr:Expr):Bool
|
||||||
return expr != null && isTrue(expr);
|
return expr != null && isTrue(expr);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user