]> O.S.I.I.S - jp/crow.git/commitdiff
debug bindings, clear only necessary bindings depending on source Instance changes
authorjpbruyere <jp.bruyere@hotmail.com>
Mon, 29 Feb 2016 09:43:47 +0000 (10:43 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Mon, 29 Feb 2016 09:43:47 +0000 (10:43 +0100)
src/CompilerServices/CompilerServices.cs
src/GraphicObjects/GraphicObject.cs

index c224f01df035c6dfb6857b32e62e1b8b83c78cbf..76a38c4965c790fee5660b95a5878c3238eb8320 100644 (file)
@@ -48,6 +48,8 @@ namespace Crow
        public class Binding{
                static int bindingCpt = 0;
                string dynMethodId = "";
+               bool resolved = false;
+
                public string NewDynMethodId {
                        get {
                                if (!string.IsNullOrEmpty (dynMethodId))
@@ -60,7 +62,24 @@ namespace Crow
                public string DynMethodId {
                        get { return dynMethodId; }
                }
-               public bool Resolved = false;
+
+
+               public bool Resolved {
+                       get {
+                               return resolved;
+                       }
+                       set {
+                               if (value == resolved)
+                                       return;
+                               #if DEBUG_BINDING
+                               if (value == true)
+                                       Debug.WriteLine ("\tOk => " + this.ToString());
+                               else
+                                       Debug.WriteLine ("\tresolved state reseted => " + this.ToString());
+                               #endif
+                               resolved = value;
+                       }
+               }
 
                public MemberReference Target;
                public MemberReference Source;
@@ -100,8 +119,10 @@ namespace Crow
                                        ptr++;
                                }
                                while (ptr < bindingExp.Length - 1) {
-                                       if (tmp == null)
+                                       if (tmp == null) {
+                                               Debug.WriteLine ("\tERROR: target not found => " + this.ToString());
                                                return false;
+                                       }
                                        if (bindingExp [ptr] == "..")
                                                tmp = tmp.LogicalParent;
                                        else if (bindingExp [ptr] == ".") {
@@ -113,8 +134,10 @@ namespace Crow
                                        ptr++;
                                }
 
-                               if (tmp == null)
+                               if (tmp == null) {
+                                       Debug.WriteLine ("\tERROR: target not found => " + this.ToString());
                                        return false;
+                               }
 
                                string[] bindTrg = bindingExp [ptr].Split ('.');
 
index cb99fc11d54f29d8cd920299bc275ab095b84d82..2ddceccac82dd903734445f9bf016d52d5d6d79f 100644 (file)
@@ -425,6 +425,12 @@ namespace Crow
                        set {
                                if (dataSource == value)
                                        return;
+                               #if DEBUG_BINDING
+                               Debug.WriteLine("******************************");
+                               Debug.WriteLine("New DataSource for => " + this.ToString());
+                               Debug.WriteLine("\t- " + DataSource);
+                               Debug.WriteLine("\t+ " + value);
+                               #endif
 
                                this.ClearBinding ();
 
@@ -436,7 +442,8 @@ namespace Crow
                        }
                        get {
                                return dataSource == null ? LogicalParent == null ? null :
-                                       (LogicalParent as GraphicObject).DataSource : dataSource;
+                                       LogicalParent is GraphicObject ?
+                                       (LogicalParent as GraphicObject).DataSource : null : dataSource;
                        }
                }
                [XmlAttributeAttribute]
@@ -1045,38 +1052,45 @@ namespace Crow
                        if (Bindings.Count == 0)
                                return;
                        #if DEBUG_BINDING
-                       Debug.WriteLine ("ResolveBinding => " + this.ToString ());
+                       Debug.WriteLine ("Resolve Bindings => " + this.ToString ());
                        #endif
+                       //grouped bindings by Instance of Source
                        Dictionary<object,List<Binding>> resolved = new Dictionary<object, List<Binding>>();
+
                        foreach (Binding b in Bindings) {
                                if (b.Resolved)
                                        continue;
+
                                if (b.Target.Member.MemberType == MemberTypes.Event) {
                                        if (b.Expression.StartsWith("{")){
                                                CompileEventSource(b);
                                                continue;
                                        }
-                               }
-                               if (!b.FindTarget ()) {
-                                       Debug.WriteLine ("BINDING ERROR: target not found => " + b.ToString());
-                                       continue;
-                               }
-                               if (b.Target.Member.MemberType == MemberTypes.Event) {
+                                       if (!b.FindTarget ())
+                                               continue;
                                        //register handler for event
                                        if (b.Source.Method == null) {
-                                               Debug.WriteLine ("Handler Method not found: " + b.ToString());
+                                               Debug.WriteLine ("\tError: Handler Method not found: " + b.ToString());
                                                continue;
                                        }
-
-                                       MethodInfo addHandler = b.Target.Event.GetAddMethod ();
-                                       Delegate del = Delegate.CreateDelegate (b.Target.Event.EventHandlerType, b.Source.Instance, b.Source.Method);
-                                       addHandler.Invoke (this, new object[] { del });
-                                       b.Resolved = true;
-                                       #if DEBUG_BINDING
-                                       Debug.WriteLine ("\tHandler binded => " + b.ToString());
-                                       #endif
+                                       try {
+                                               MethodInfo addHandler = b.Target.Event.GetAddMethod ();
+                                               Delegate del = Delegate.CreateDelegate (b.Target.Event.EventHandlerType, b.Source.Instance, b.Source.Method);
+                                               addHandler.Invoke (this, new object[] { del });
+
+                                               #if DEBUG_BINDING
+                                               Debug.WriteLine ("\tHandler binded => " + b.ToString());
+                                               #endif
+                                               b.Resolved = true;
+                                       } catch (Exception ex) {
+                                               Debug.WriteLine ("\tERROR: " + ex.ToString());
+                                       }
                                        continue;
                                }
+
+                               if (!b.FindTarget ())
+                                       continue;
+
                                List<Binding> bindings = null;
                                if (!resolved.TryGetValue (b.Source.Instance, out bindings)) {
                                        bindings = new List<Binding> ();
@@ -1084,9 +1098,7 @@ namespace Crow
                                }
                                bindings.Add (b);
                                b.Resolved = true;
-                               #if DEBUG_BINDING
-                               Debug.WriteLine ("\tmarked as resolved => " + b.ToString());
-                               #endif
+
                        }
 
                        MethodInfo stringEquals = typeof(string).GetMethod
@@ -1255,6 +1267,10 @@ namespace Crow
                /// <param name="es">Event binding details</param>
                public void CompileEventSource(Binding binding)
                {
+                       #if DEBUG_BINDING
+                       Debug.WriteLine ("\tCompile Event Source => " + binding.ToString());
+                       #endif
+
                        Type sourceType = this.GetType();
 
                        #region Retrieve EventHandler parameter type
@@ -1373,17 +1389,30 @@ namespace Crow
                        addHandler.Invoke(this, new object[] {del});
 
                        binding.Resolved = true;
-                       #if DEBUG_BINDING
-                       Debug.WriteLine ("\tCompiled Event Source => " + binding.ToString());
-                       #endif
                }
                /// <summary>
                /// Remove dynamic delegates by ids from dataSource
                ///  and delete ref of this in Shared interface refs
                /// </summary>
                public virtual void ClearBinding(){
+                       //dont clear binding if dataSource is not null,
                        foreach (Binding b in Bindings) {
                                try {
+                                       if (!b.Resolved)
+                                               continue;
+                                       //cancel compiled events
+                                       if (b.Source == null){
+                                               continue;
+                                               #if DEBUG_BINDING
+                                               Debug.WriteLine("Clear binding canceled for => " + b.ToString());
+                                               #endif
+                                       }
+                                       if (b.Source.Instance != DataSource){
+                                               #if DEBUG_BINDING
+                                               Debug.WriteLine("Clear binding canceled for => " + b.ToString());
+                                               #endif
+                                               continue;
+                                       }
                                        #if DEBUG_BINDING
                                        Debug.WriteLine("ClearBinding => " + b.ToString());
                                        #endif