- package com.zevg.utilities
- {
- import flash.utils.Dictionary;
- import mx.collections.ListCollectionView;
- import mx.core.EventPriority;
- import mx.events.CollectionEvent;
- import mx.events.CollectionEventKind;
- public class CollectionEventBatcher
- {
- private static var collection2batchEvent:Dictionary = new Dictionary(true);
- /**
- * Batching add events. For performance purpose, problem is here https://bugs.adobe.com/jira/browse/SDK-30008
- *
- * IMPORTANT: Method batches only ADD events.
- */
- public static function start(collection:ListCollectionView):void
- {
- collection.addEventListener(
- CollectionEvent.COLLECTION_CHANGE,
- batching_collectionChangeHandler,
- false, EventPriority.CURSOR_MANAGEMENT, true
- );
- }
- public static function stop(collection:ListCollectionView):void
- {
- collection.removeEventListener(
- CollectionEvent.COLLECTION_CHANGE,
- batching_collectionChangeHandler
- );
- if (collection2batchEvent[collection])
- {
- collection.dispatchEvent(collection2batchEvent[collection]);
- delete collection2batchEvent[collection];
- }
- }
- private static function batching_collectionChangeHandler(event:CollectionEvent):void
- {
- // handle only ADD events
- if (event.kind != CollectionEventKind.ADD)
- return;
- var collection:ListCollectionView = event.target as ListCollectionView;
- var batchAddEvent:CollectionEvent;
- if (!collection2batchEvent[collection])
- collection2batchEvent[collection] = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE, false, false, CollectionEventKind.ADD);
- batchAddEvent = collection2batchEvent[collection];
- batchAddEvent.items = batchAddEvent.items.concat(event.items);
- // swallow this event
- event.stopImmediatePropagation();
- }
- }
- }