flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mih...@apache.org
Subject [09/12] git commit: [flex-sdk] [refs/heads/develop] - FLEX-34854 Items removed from the collection are not watched for changes anymore.
Date Mon, 08 Jun 2015 18:02:36 GMT
FLEX-34854
Items removed from the collection are not watched for changes anymore.

NOTES:
-to be able to unwatch individual items the storage for the watchers had to change into a
(weakly typed) Dictionary.


Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/b9fba79a
Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/b9fba79a
Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/b9fba79a

Branch: refs/heads/develop
Commit: b9fba79a81d34f0a5c7aaf5174d74ef390226c9c
Parents: f68c6c9
Author: Mihai Chira <mihaic@apache.org>
Authored: Mon Jun 8 18:38:29 2015 +0200
Committer: Mihai Chira <mihaic@apache.org>
Committed: Mon Jun 8 18:38:29 2015 +0200

----------------------------------------------------------------------
 .../mx/collections/ComplexFieldChangeWatcher.as | 65 +++++++++++++++-----
 1 file changed, 49 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/b9fba79a/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as
b/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as
index 7190bd5..a128cf3 100644
--- a/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as
+++ b/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as
@@ -19,6 +19,7 @@
 
 package mx.collections {
     import flash.events.EventDispatcher;
+    import flash.utils.Dictionary;
 
     import mx.binding.utils.ChangeWatcher;
     import mx.core.mx_internal;
@@ -26,28 +27,49 @@ package mx.collections {
     import mx.events.CollectionEventKind;
     import mx.events.PropertyChangeEvent;
 
-    public class ComplexFieldChangeWatcher extends EventDispatcher {
-
-        private var _complexFieldWatchers:Vector.<ChangeWatcher> = new Vector.<ChangeWatcher>();
+    public class ComplexFieldChangeWatcher extends EventDispatcher
+    {
+        private var _complexFieldWatchers:Dictionary = new Dictionary(true);
         private var _list:IList;
         private var _listCollection:ICollectionView;
 
         public function stopWatchingForComplexFieldChanges():void
         {
             unwatchListForChanges();
+            unwatchAllItems();
+        }
 
-            for each(var watcher:ChangeWatcher in _complexFieldWatchers)
+        private function unwatchAllItems():void
+        {
+            for each(var item:Object in _complexFieldWatchers)
+            {
+                unwatchItem(item);
+                delete _complexFieldWatchers[item];
+            }
+        }
+
+        private function unwatchArrayOfItems(items:Array):void
+        {
+            for(var i:int = 0; i < items.length; i++)
             {
-                watcher.unwatch();
+                unwatchItem(items[i]);
             }
+        }
 
-            _complexFieldWatchers.length = 0;
+        private function unwatchItem(item:Object):void
+        {
+            var watchersForItem:Array = _complexFieldWatchers[item] as Array;
+            while(watchersForItem && watchersForItem.length)
+            {
+                var watcher:ChangeWatcher = watchersForItem.pop() as ChangeWatcher;
+                if(watcher)
+                    watcher.unwatch();
+            }
         }
 
         public function startWatchingForComplexFieldChanges():void
         {
             watchListForChanges();
-
             watchItems(list);
         }
 
@@ -93,19 +115,15 @@ package mx.collections {
             var watcher:ChangeWatcher = ChangeWatcher.watch(item, chain, new Closure(item,
onComplexValueChanged).callFunctionOnObject, false, true);
             if(watcher)
             {
-                _complexFieldWatchers.push(watcher);
+                addWatcher(watcher, item);
             }
         }
 
-        private function onCollectionChanged(event:CollectionEvent):void
+        private function addWatcher(watcher:ChangeWatcher, forItem:Object):void
         {
-            switch(event.kind)
-            {
-                case CollectionEventKind.ADD: {
-                    watchArrayOfItems(event.items);
-                    break;
-                }
-            }
+            if(!_complexFieldWatchers[forItem])
+                _complexFieldWatchers[forItem] = [];
+            (_complexFieldWatchers[forItem] as Array).push(watcher);
         }
 
         private function onComplexValueChanged(item:Object):void
@@ -145,6 +163,21 @@ package mx.collections {
             if(list)
                 list.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChanged);
         }
+
+        private function onCollectionChanged(event:CollectionEvent):void
+        {
+            switch(event.kind)
+            {
+                case CollectionEventKind.ADD: {
+                    watchArrayOfItems(event.items);
+                    break;
+                }
+                case CollectionEventKind.REMOVE: {
+                    unwatchArrayOfItems(event.items);
+                    break;
+                }
+            }
+        }
     }
 }
 


Mime
View raw message