Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.76 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.zevg.utilities
  2. {
  3.         import flash.utils.Dictionary;
  4.        
  5.         import mx.collections.ListCollectionView;
  6.         import mx.core.EventPriority;
  7.         import mx.events.CollectionEvent;
  8.         import mx.events.CollectionEventKind;
  9.  
  10.         public class CollectionEventBatcher
  11.         {
  12.                 private static var collection2batchEvent:Dictionary = new Dictionary(true);
  13.                
  14.                 /**
  15.                  * Batching add events. For performance purpose, problem is here https://bugs.adobe.com/jira/browse/SDK-30008
  16.                  *
  17.                  * IMPORTANT: Method batches only ADD events.
  18.                  */
  19.                 public static function start(collection:ListCollectionView):void
  20.                 {
  21.                         collection.addEventListener(
  22.                                 CollectionEvent.COLLECTION_CHANGE,
  23.                                 batching_collectionChangeHandler,
  24.                                 false, EventPriority.CURSOR_MANAGEMENT, true
  25.                         );
  26.                 }
  27.                
  28.                
  29.                 public static function stop(collection:ListCollectionView):void
  30.                 {
  31.                         collection.removeEventListener(
  32.                                 CollectionEvent.COLLECTION_CHANGE,
  33.                                 batching_collectionChangeHandler
  34.                         );
  35.                        
  36.                         if (collection2batchEvent[collection])
  37.                         {
  38.                                 collection.dispatchEvent(collection2batchEvent[collection]);
  39.                                 delete collection2batchEvent[collection];
  40.                         }
  41.                 }
  42.                
  43.                 private static function batching_collectionChangeHandler(event:CollectionEvent):void
  44.                 {
  45.                         // handle only ADD events
  46.                         if (event.kind != CollectionEventKind.ADD)
  47.                                 return;
  48.                        
  49.                         var collection:ListCollectionView = event.target as ListCollectionView;
  50.                        
  51.                         var batchAddEvent:CollectionEvent;
  52.                         if (!collection2batchEvent[collection])
  53.                                 collection2batchEvent[collection] = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE, false, false, CollectionEventKind.ADD);
  54.                        
  55.                         batchAddEvent = collection2batchEvent[collection];
  56.                        
  57.                         batchAddEvent.items = batchAddEvent.items.concat(event.items);
  58.                        
  59.                         // swallow this event
  60.                         event.stopImmediatePropagation();
  61.                 }
  62.         }
  63. }