When using flex/flash and leveraging remote objects, there will be a time where you might need to dispatch two or more events simultaneous. The issue with this is when using BlazeDS you will run into a bundling effect of the events. The events will be fired in the same frame and as such they will be considered as a bundled request. The down side is that one request will affect the time of return for the other request.


Example:

Request 1 takes 135ms

Request 2 takes 784ms

These request will be processed as a bundle and will not be returned until both are performed so you will not benefit from performances tuned request and your result will be both returning in 919ms and your UI will have to process both pieces of information together instead of staggering layout.

The alternative is to make sure each event is dispatched in a separate frame so there is no bundling.

package com.justinimhoff.library.utilities
{
    import flash.events.EventDispatcher;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
 
    /**
     *
     * Creates a queue of events that only one is dispatched every other key frame in order to
     * get away from bundling of events from the player an browser. When bundling of events occurs,
     * the slowest running will impac the result time of all others and items live on a single thread
     * server side.
     *
     */
    public class StaggeredRemoteDispatcher extends EventDispatcher
    {
        /**
         * set to low value so it will only dispatch on next keyframe
         * @private
         */
        private var dispatchTimer:Timer = new Timer(100);
 
        /**
         * a queue of events to dispatch
         * @private
         */
        private var events:Array = [];
 
        /**
         * Adds an event to the queue that will be called on the next frame event.
         * Starts the time and adds event listner for the next interval
         * @param event
         *
         */
        public function addFunction(item:Function):void
        {
            if (events.length == 0)
            {
                dispatchTimer.addEventListener(TimerEvent.TIMER, dispatchSequenceEvent);
                dispatchTimer.start();
            }
            events.push(item);
        }
 
        /**
         * Dispatches the next event in the queue and stops the timer and removes the event
         * listner if there are no more items.
         * @param event
         *
         */
        protected function dispatchSequenceEvent(event:TimerEvent):void
        {
            if (events.length > 0)
            {
                var callFunction:Function = events[0] as Function;
                callFunction.call();
		//remove just called function
		events.shift();
            }
			if(events.length == 0){
				dispatchTimer.stop();
				dispatchTimer.removeEventListener(TimerEvent.TIMER, dispatchSequenceEvent);
			}
        }
    }
}

Leave a reply