My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
AsyncRunner  

Featured
Updated Aug 8, 2008 by aldo.buc...@gmail.com

AS3 has no threads... so every process runs to completion before returning control to the caller. For big processes ( like parsing a multi MB file ) this is a huge blocker.

The utils.AsyncRunner class provides a simple pattern that you can implement in classes that perform huge tasks to split processing into small chunks.

To use it:

  • Make all the involved functions "queueable"
    • They must not return values ( you won't be able to catch them )
    • They must queue ( AsyncRunner.queue() ) every nested method call, passing ALL necessary context as arguments
  • Start an AsyncRunner thread ( instance ) by calling AsyncRunner.create( ) and passing a queuable function as argument ( this will be the starting point )
  • Call yourRunnerInstance.step( ) to execute a step, repeat manually until there are no more steps left
  • Optionally, you can call yourRunnerInstance.run( 1000 ) to run the thread for a limited time ( 1000ms in this case )

Usage example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="vertical"
	creationComplete="onCreationComplete( )"
	>
	
	<mx:Script>
		<![CDATA[
		
			import com.aldobucchi.utils.AsyncRunner;
			
			[Bindable]
			public var runner:AsyncRunner;
			
			
			private function onCreationComplete( ):void
			{
				// create the thread by specifying an entry point
				runner = AsyncRunner.create( parseNode, [ items ] );		
			}
			
			
			
			private function parseNode( node:XML, level:int=0 ):void
			{
				ta.text += indent( level ) + node.@name + "\n";
				
				for each ( var childNode:XML in node.item )
				{
					// queue methods to be called later
					AsyncRunner.queue( parseNode, [ childNode, level + 1 ] );
				}
			}			

			
			private function indent( level:int ):String
			{
				for ( var str:String="" ; level > 0 ; level-- )
					str += "- -";
				return str;
			}
			
			
			
		]]>
	</mx:Script>
	
	<mx:Button 
		label="{runner.isComplete ? 'complete' : 'step'}" 
		enabled="{!runner.isComplete}"
		click="runner.step( )"
		/>

	<mx:XML id="items" xmlns="">
		<items name="items">
			<item name="A">
					<item name="A1"></item>
					<item name="A2"></item>
					<item name="A3"></item>
					<item name="A4">
							<item name="A4A"></item>
							<item name="A4B"></item>
							<item name="A4C"></item>
							<item name="A4D">
								<item name="A4D1"></item>
								<item name="A4D2"></item>
								<item name="A4D3"></item>
								<item name="A4D4"></item>
							</item>
					</item>
			</item>
			<item name="B">
					<item name="B1"></item>
					<item name="B2"></item>
					<item name="B3"></item>
					<item name="B4">
							<item name="B4A"></item>
							<item name="B4B"></item>
							<item name="B4C"></item>
							<item name="B4D">
								<item name="B4D1"></item>
								<item name="B4D2"></item>
								<item name="B4D3"></item>
								<item name="B4D4"></item>
							</item>
					</item>
			</item>
		</items>
	</mx:XML>
	
	<mx:TextArea id="ta" width="250" height="400" />
	
</mx:Application>

Sign in to add a comment
Powered by Google Project Hosting