My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.aldobucchi.utils
{

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.getTimer;



/**
* @eventType flash.events.Event.COMPLETE
*/
[Event(name="complete", type="flash.events.Event")]






/**
*
* Implements a simple Async Execution pattern to break resource-intensive tasks
* ( like large document parsing ) in the absence of threads.
*
* @author aldo.bucchi@gmail.com
*
*/
public class AsyncRunner extends EventDispatcher
{




// this is stored here instead of calculated on-demand for performance reasons
private var _isComplete:Boolean = true;
[Bindable("complete")]
public function get isComplete( ):Boolean
{
return _isComplete;
}



/**
*
* The stack is an array of queues.
*
*/
private var stack:Array = [ [] ];



/**
*
* The current queue is located at the end of the stack
*
* @return
*
*/
private function get currentQueue():Array
{
if ( stack.length == 0 )
return null;

return stack[ stack.length - 1 ] as Array;
}




/**
*
* You don't instantiate an AsyncRunner directly.
* Instead, you call AsyncRunner.create( ) to create a new runner thread.
* You can then call runner.step() to execute each step manually
*
*/
public function AsyncRunner( )
{


}





/**
*
* @param func
* @param args
*
*/
private function queue( func:Function, args:Array = null ):void
{
currentQueue.push( [ func, args ] );
_isComplete = false;
}




/**
*
* consumes one step from the queue
*
*/
public function step( ):void
{
if ( _isComplete )
{
return;
}



while ( currentQueue.length == 0 )
{
// the current queue has no more entries left, we can pop it and move down the stack
stack.pop( );
if ( currentQueue == null )
{
// the stack is exhausted, we're done
_isComplete = true;
dispatchEvent( new Event( Event.COMPLETE ) );
App.app.removeEventListener( Event.ENTER_FRAME, app_enterFrame );
return;
}
}


var entry:Array = currentQueue.shift() as Array;
// append to the stack so any calls made when executing this entry
// are stored in their own queue
stack.push( [] );



//////////////// execute //////////////////


var func:Function = entry[ 0 ] as Function;

var args:Array = [];

if ( entry[1] && entry[1] is Array )
args = ( entry[1] as Array );


runnerStack.push( this );
func.apply( null, args );
runnerStack.pop( );
}







private var endTime:int;
/**
*
* Will execute this thread for ( a bit longer than ) the amount
* of time specified.
*
* if no argument is passed it will run to completion
*
* @param elapsed the time, in milliseconds, that this thread is given to run. defaults to -1 ( no limit )
*
*/
public function run( elapsed:int = -1 ):void
{
endTime = getTimer() + elapsed;
while ( ( elapsed == -1 ) || ( getTimer( ) < endTime ) )
{

step( );

if ( _isComplete )
break;

}

}




/**
*
* A common use case.
* Will add an enter frame listener and try to run( elapsed ) on each frame.
*
* @param elapsed
*
*/
public function runEachFrame( elapsed:int = 100 ):void
{
elapsedPerFrame = elapsed;
App.app.addEventListener( Event.ENTER_FRAME, app_enterFrame );
}

private var elapsedPerFrame:int;

private function app_enterFrame( event:Event ):void
{
run( elapsedPerFrame );
}







////////////////////////////////////////////////////////////////////
///////////////////////////// static ///////////////////////////////
////////////////////////////////////////////////////////////////////



/**
*
* By using a stack here we allow for nested async runners
* ( a runner generated within another runner )
*
* We are still missing some logic to chain runners, but
* there are no use cases yet so we don't really care
*
*/
private static var runnerStack:Array = [];





/**
*
*
* Creates a runner instance, starting at the given function.
* You can then call runner.step() to execute the first step and any
* other nested steps that get queued as well.
*
*
* @param func
* @param args
* @return
*
*/
public static function create( func:Function, args:Array = null ):AsyncRunner
{
var runner:AsyncRunner = new AsyncRunner( );
runner.queue( func, args );
return runner;
}




/**
*
* Even though this function is static, it operates on the current active runner thread.
* It is only meant to be called from within functions that are designed to work with async
* threads.
*
* @param func
* @param args
*
*/
public static function queue( func:Function, args:Array = null ):void
{
if ( runnerStack.length > 0 )
{
// append call to the current ( topmost ) runner's queue
( runnerStack[ runnerStack.length - 1 ] as AsyncRunner ).queue( func, args );
}
else
{
throw new Error( "There is no active AsyncRunner. You cannot queue a method call." );
}
}

}

}

Change log

r42 by aldo.bucchi on Aug 1, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r14 by aldo.bucchi on Jun 21, 2008   Diff
checking-in a zillion classes... for
now they are all here but don't expect
this layout to last for too long
r6 by aldo.bucchi on Jun 12, 2008   Diff
removed unused imports
r5 by aldo.bucchi on Jun 12, 2008   Diff
added AsyncRunner
All revisions of this file

File info

Size: 5620 bytes, 286 lines
Powered by Google Project Hosting