minimize scope for chain binding
This commit is contained in:
+10
-14
@@ -1,13 +1,12 @@
|
||||
package bindx;
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Printer;
|
||||
|
||||
#if macro
|
||||
|
||||
|
||||
import bindx.Error;
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Type;
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Printer;
|
||||
|
||||
using Lambda;
|
||||
using haxe.macro.Tools;
|
||||
@@ -70,25 +69,22 @@ class BindExt {
|
||||
var fields = checkFields(expr);
|
||||
if (fields.length == 0)
|
||||
Context.fatalError("can't bind empty expression", expr.pos);
|
||||
var flag = false;
|
||||
var i = fields.length;
|
||||
var first = null;
|
||||
while (i-- > 0) {
|
||||
var f = fields[i];
|
||||
if (flag) f.bindable = false;
|
||||
else if (!f.bindable) {
|
||||
flag = true;
|
||||
if (first == null) first = f;
|
||||
}
|
||||
if (first != null) f.bindable = false;
|
||||
else if (!f.bindable && first == null) first = f;
|
||||
}
|
||||
if (flag) {
|
||||
if (first != null)
|
||||
Context.warning('expr in not full bindable. Can bind only "${first.e.toString()}"', expr.pos);
|
||||
}
|
||||
|
||||
var chain = prepareBindChain(fields, listener, expr.pos);
|
||||
var listenerExpr = macro listener;
|
||||
var chain = prepareBindChain(fields, listenerExpr, expr.pos);
|
||||
|
||||
var res = macro
|
||||
$b { chain.bind.concat(chain.init).concat([macro function __unbind__():Void $b { chain.unbind } ]) };
|
||||
var res = macro (function (listener):Void->Void
|
||||
$b { chain.bind.concat(chain.init).concat([macro return function ():Void $b { chain.unbind }]) }
|
||||
)($listener);
|
||||
//trace(new Printer().printExpr(res));
|
||||
return res;
|
||||
}
|
||||
|
||||
+14
-10
@@ -27,10 +27,11 @@ class ChainBindTest extends BuddySuite {
|
||||
it("BindExt.chain should bind chain changes (unset links)", {
|
||||
b.c.c.d = val;
|
||||
|
||||
var unbind = BindExt.chain(b.c.c.d, function (_, t:String) {
|
||||
var listener = function (_, t:String) {
|
||||
callNum++;
|
||||
t.should.be(val);
|
||||
});
|
||||
};
|
||||
var unbind = BindExt.chain(b.c.c.d, listener);
|
||||
|
||||
callNum.should.be(1);
|
||||
|
||||
@@ -88,9 +89,12 @@ class ChainBindTest extends BuddySuite {
|
||||
b.c.c = b2.c.c;
|
||||
callNum.should.be(3);
|
||||
|
||||
Bind.notify(b.c.c.f);
|
||||
callNum.should.be(4);
|
||||
|
||||
unbind();
|
||||
b.c.c.f("tada").d = "c";
|
||||
callNum.should.be(3);
|
||||
callNum.should.be(4);
|
||||
});
|
||||
|
||||
it("BindExt.chain should bind chain changes (1 gap)", {
|
||||
@@ -108,22 +112,22 @@ class ChainBindTest extends BuddySuite {
|
||||
var b2 = new BindableChain(4);
|
||||
b2.c.nc.c.f("tada").d = val = "c";
|
||||
var t = b.c;
|
||||
b.c = b2.c;
|
||||
b.c = b2.c; // bind works
|
||||
|
||||
callNum.should.be(2);
|
||||
|
||||
val = t.nc.c.f("tada").d;
|
||||
b.c = t;
|
||||
b.c = t; // bind works
|
||||
|
||||
callNum.should.be(3);
|
||||
|
||||
b2.c.nc.c.f("tada").d = val = "d";
|
||||
|
||||
b.c.nc.c = b2.c.nc.c;
|
||||
callNum.should.be(3); // nc gap
|
||||
b.c.nc.c = b2.c.nc.c; // nc gap
|
||||
callNum.should.be(3);
|
||||
|
||||
unbind();
|
||||
b.c.nc.c.f("tada").d = "c";
|
||||
b.c.nc.c.f("tada").d = "c"; // nc gap
|
||||
callNum.should.be(3);
|
||||
});
|
||||
|
||||
@@ -195,11 +199,11 @@ class BindableChain implements bindx.IBindable {
|
||||
|
||||
@:bindable
|
||||
public function f(s:String):BindableChain {
|
||||
return c;
|
||||
return s == "tada" ? c : null;
|
||||
}
|
||||
|
||||
public function nf(s:String):BindableChain {
|
||||
return nc;
|
||||
return s == "tada" ? nc : null;
|
||||
}
|
||||
|
||||
public function new(depth:Int) {
|
||||
|
||||
Reference in New Issue
Block a user