This is a feature request for Flash Player 8.5 in support of a new Class for handling synchronous behavior. Adding synchronous behavior into all Flash Player classes would be a massive undertaking but allowing a single function to execute in a structured manner would not. Let me explain.
First let’s define Synchronous within Flash Player to make sure we are all on the same page. I view Synchronous behavior in Flash as:
1. A Synchronous process is started
2. The Flash Player executes the process over and over until the process returns
3. The Player does not error or timeout or complain
4. When the process returns, AS execution starts from the exact point where it left off.
Here is some example Synchronous code examples:
// retrieve data from a URL and set it as a DataProvider property.
dataGrid.dataProvider = getData( url )
or
if( uploadFile( url ) ){
\\file was browsed, selected, and uploaded to the url successfully
}else{
\\an error occured
}
The key to making this work is providing a class within which a function can be executed over and over without causing the player to:
1. Reach the Script Timeout Limit
2. Reach the Recursion Limit
If Flash Player 8.5 provided a Class under which the above 2 rules were valid, we could execute a function over and over to provide Synchronous behavior. Here is an example that counts to 10,000,000 within function run Synchronously:
function timeTo10Million( ){
import flash.util.Synchronous
// define a function to be executed over and over
// a single object argument is passed
// with every execution to save data between calls
var synchFunction = function( sdo:Object ){
//if i is undefined, define it.
if( sdo.i == undefined ) sdo.i=0
//if startTime is undefined, define it.
if( sdo.startTime == undefined ) sdo.startTime = flash.util.getTimer()
//add 1 to i
sdo.i++
// if we have reached 10 Million return
if( sdo.i >= 10000000 ){
//return ms to count to 10 million
//this will end execution of Synchronous.execute!!!
return flash.util.getTimer() – sdo.startTime
}
//every million iterations, force an update to the DisplayList
if( sdo.i % 1000000 == 0 ) Synchronous.updateAfterEvent()
}
//call Synchronous.execute with synchFunction and a new Object
return Synchronous.execute( synchFunction , {} )
}
trace( timeTo10Million() ) // 55
When a function is executed under Synchronous.execute, the rules for AS execution change within the Flash Player. The player will execute the function over and over and if data is returned at any point, execution returns to the exact spot where Synchronous.execute was started. Synchronous.execute calls the passed function over and over, and passes a common object as a argument for storing data between executions. This allows you to avoid creating lots of temporary variables and allows other AS to execute during Synchronous execution. Within this function, you could fetch data remotely, listen for Socket AMF data to return, or just sit and wait for 100 ms without using events. Obviously use of Synchronous.execute is an advanced feature but it provides an opportunity to create lots of simplified tools very easily. I can think of lots of uses to simplify software development with Flash/Flex and I am sure you can think of allot more too.
The advantages here is that we can use AS3 language to extend the player synchronously without allot of muss or fuss. Thread management, specialized events, or advanced concepts of parallel execution are not needed. Also as a bonus, because Synchronous is a class, it is already ECMA compliant.
If you would like to see Synchronous behavior in Flash Player 8.5, say something!!! There is very little development time left as Flash Player 8.5 moves toward feature completion. I think this addition would revolutionize the use of Flash/Flex and simplify lots of concepts that make no sense to developers coming from other languages. It is about time we had an alternative to “waiting yet another frame”.
Comments encouraged!
Cheers,
Ted