TimedForEach Class

I ran into some trouble today with batch processing some files.
I am processing a set of files, but it is possible that some of the supplied files are invalid.
In that case i want to be able to pop up a messagebox and ask the user if he wants to retry, ignore or abort.
A lot of the involved calls have to be processed asynchronously and i have to give control back to the runtime in between calls, so that Flash can update the display (amongst other things).

The obvious solution is a queue that pauses in between calls to the processing routine and that can be paused while waiting for the user input.

This class does exactly that.

Actionscript:
  1. /**
  2. * ...
  3. * @author Oliver "gencha" Salzburg
  4. * @version 0.1
  5. */
  6.  
  7. package util {
  8.     import flash.events.EventDispatcher;
  9.     import flash.utils.Timer;
  10.     import flash.events.TimerEvent;
  11.    
  12.     /**
  13.      * The TimedForEach class enabled you to run a command on an array of items while having full control over the process.
  14.      * The class allows for processing of the items in an asychronous environment.
  15.      * You can pause the loop and resume it later on.
  16.      * A typical application would be pausing the loop while waiting for user input which could manipulate the way the loop behaves.
  17.      */
  18.     public class TimedForEach extends EventDispatcher {
  19.        
  20.         /**
  21.          * The delay between calls to the item processor
  22.          */
  23.         private static const DEFAULT_DELAY:uint = 100;
  24.        
  25.         private var targets_:Array;
  26.         private var command_:Function;
  27.        
  28.         private var targetIndex_:int;
  29.         private var lastIndex_:int;
  30.        
  31.         private var timer_:Timer;
  32.        
  33.         private var isStalled_:Boolean;
  34.        
  35.         /**
  36.          * Default constructor
  37.          * @param   targets The array that is to be processed.
  38.          * The array is not copied and not modified by the class
  39.          * @param   command The command that is applied to each item in the targes array.
  40.          * The function is called with a single target as it's parameter
  41.          */
  42.         public function TimedForEach( targets:Array, command:Function ) {
  43.             targets_ = targets;
  44.             command_ = command;
  45.         }
  46.        
  47.         /**
  48.          * Start the loop
  49.          */
  50.         public function execute():void {
  51.             isStalled_   = false;
  52.             targetIndex_    = 0;
  53.             lastIndex_    = 0;
  54.            
  55.             timer_ = new Timer( DEFAULT_DELAY );
  56.             timer_.addEventListener( TimerEvent.TIMER, checkState );
  57.             timer_.start();
  58.         }
  59.        
  60.         /**
  61.          * Stop processing
  62.          */
  63.         public function kill():void {
  64.             timer_.stop();
  65.         }
  66.        
  67.         /**
  68.          * Process the next item if possible
  69.          * @param   event
  70.          */
  71.         private function checkState( event:TimerEvent ):void {
  72.             if( isStalled_ ) return;
  73.            
  74.             if( targets_.length <= targetIndex_ ) {
  75.                 dispatchEvent( new TimerEvent( TimerEvent.TIMER_COMPLETE ) );
  76.                 kill();
  77.                 return;
  78.             }
  79.            
  80.             lastIndex_ = targetIndex_;
  81.             command_( targets_[ targetIndex_++ ] );
  82.         }
  83.        
  84.         /**
  85.          * Pause processing
  86.          * @see #resume()
  87.          */
  88.         public function stall():void {
  89.             isStalled_ = true;
  90.         }
  91.        
  92.         /**
  93.          * Resume processing
  94.          * @see #stall()
  95.          */
  96.         public function resume():void {
  97.             isStalled_ = false;
  98.         }
  99.        
  100.         /**
  101.          * Retry the last processed item
  102.          */
  103.         public function again():void {
  104.             targetIndex_ = lastIndex_;
  105.         }
  106.        
  107.         /**
  108.          * Resets the loop on the first item in the list
  109.          */
  110.         public function reset():void {
  111.             targetIndex_    = 0;
  112.             lastIndex_   = 0;
  113.         }
  114.        
  115.         /**
  116.          * Returns an exact copy of the object
  117.          * @return An exact copy of the object
  118.          */
  119.         public function clone():TimedForEach {
  120.             var clone:TimedForEach = new TimedForEach( targets_, command_ );
  121.             return clone;
  122.         }
  123.        
  124.         /**
  125.          * Returns a representation of the object as a string
  126.          * @return A representation of the object as a string
  127.          */
  128.         override public function toString():String {
  129.             return "[object TimedForEach]";
  130.         }
  131.        
  132.     }
  133.    
  134. }

Leave a Reply

You must be logged in to post a comment.