ActionScript 3 isn't supposed to be a simple synchronous architecture language? - actionscript-3

A simple piece of code that should trace :
rien
test
done!
and I get something completely far away from that,
scenario A :
var __functions_to_execute:Array;
function start():void {
__functions_to_execute =[];
__functions_to_execute.push(futile_trace());
__functions_to_execute.push(futile_trace('test'));
execute_functions();
}
function execute_functions():void {
if(__functions_to_execute.length){
//where shift on this Array remove the first element and returns it
var exec:Function =__functions_to_execute.shift();
exec;
//I tried this too, just in case
//__functions_to_execute[0];
//__functions_to_execute.shift();
} else trace("done!");
}
function futile_trace(_value:String ='rien'):void {
trace(_value);
execute_functions();
}
start();
pretty simple. but the result is :
rien
done!
test
lets add a deprecated function to this and lets change the futile_trace function to :
function futile_trace(_value:String ='rien'):void {
trace(_value);
setTimeout(execute_functions, 0);
}
and then the result is :
rien
test
done!
Ok then, I said to myself, why not, lets change the scope when I call execute_functions, so I tried :
function futile_trace(_value:String ='rien'):void {
trace(_value);
extra_step();
}
function extra_step():void {
execute_functions();
}
guess what was the result?! yeah :
rien
done!
test
so?! Is the trace function that bad? that slow? is it the fact that passing an argument to the function take so much time compare to the other one? I mean... wow!
is there something I can do to avoid this type of weirdness ?
(For the record, my project is not to trace {rien, done and test}... I have 15k lines of codes that react completely differently if I compile them with "Omit trace statements" or not.
Thanks for your input guys.

You are executing the functions and adding their return values to the __functions_to_execute array, not the functions themselves.
Your function execute_functions doesn't actually do anything. I've tried to explain the sequence in-line:
function start():void {
__functions_to_execute =[];
// 1. traces 'rien' first because futile_trace() is called with no args
// 2. 'done!' will be traced inside execute_functions because the array is still empty
// 3.undefined will be pushed into the array next
__functions_to_execute.push(futile_trace());
// 4. traces 'test'
// execute_functions does not trace anything because __functions_to_execute is non-empty
// but it also doesn't do anything because it is just removing the `undefined` value from the start of the array.
__functions_to_execute.push(futile_trace('test'));
execute_functions();
}
Something more like this should behave how you expect. It's storing in the array function references, along with the arguments that should be passed when the function is called.
var __functions_to_execute:Array;
function start():void {
__functions_to_execute = [];
__functions_to_execute.push({func:futile_trace, args:[]});
__functions_to_execute.push({func:futile_trace, args:['test']});
execute_functions();
}
function execute_functions():void {
if(__functions_to_execute.length){
var obj:Object = __functions_to_execute.shift();
obj.func.apply(null, obj.args);
} else trace("done!");
}
function futile_trace(_value:String ='rien'):void {
trace(_value);
execute_functions();
}
start();

For scenario A, you're not actually ever pushing futile_trace to the array - you're calling it (notice the () after the function name), and then pushing the result of that call to the array.
In other words:
You call futile_trace()
futile_trace traces 'rien', because you passed no value.
futile_trace calls _execute_functions
At this point, nothing has been pushed yet, so _execute_functions traces 'done!'
_execute_functions returns.
_futile_trace returns.
The result of futile_trace() (void) is pushed.
You call futile_trace('test')
futile_trace() outputs 'test'.
futile_trace calls _execute_functions
_execute_functions shifts void from the array.
_execute_functions executes void; (which does nothing)
etc. etc.
If you need to pass a function to another function or store a reference to it in a variable, make sure you're not calling it.
__functions_to_execute.push(futile_trace);
// Use an anonymous function to pass with arguments without executing:
__functions_to_execute.push(function() { futile_trace('test'); });
... and in _execute_functions do remember the parantheses:
exec();

Related

Evaluate where a function call originated from

Okay so I have a function called changeHandler - it is called by several eventListeners in other functions. I want to write several if statements that evaluate the source of function call and change the dataProvider of my ComboBox depending on the originating function. Example: one of the many functions is called displayCarbs() and has an eventListener like so:
function displayCarbs(event:MouseEvent):void {
myComboBox.addEventListener(Event.CHANGE, changeHandler);
}
(I've removed all of the unnecessary code from the function above)
The if statement inside the changeHandler will look something like this:
if (****referring function = displayCarbs****) {
myComboBox2.dataProvider = new DataProvider(carbItems);
}
I've searched high and low for something that can achieve this, but I just don't have a good enough grasp of AS3 or vocabulary to describe what describe what I mean to get the answer from Google.
The simplest way I can think of... Couldn't you simply create a text string that updates to the name of function before going to changeHandler then in turn changeHandler can check string content and act accordingly..
public var referring_function:String;
function displayCarbs(event:MouseEvent):void
{
referring_function = "displayCarbs";
myComboBox.addEventListener(Event.CHANGE, changeHandler);
}
function displayCarbs(event:Event):void
{
if (referring_function == "displayCarbs")
{ myComboBox2.dataProvider = new DataProvider(carbItems); }
if (referring_function == "displayOthers")
{ myComboBox2.dataProvider = new DataProvider(otherItems); }
// etc etc
}
I cant remember right now if you need == or just = when checking the If statement against strings.
I know there is an accepted answer already, but based on what I gleaned about the problem, here is a solution that wouldn't require adding another variable to check :
function displayCarbs(event:MouseEvent):void
{
myComboBox.addEventListener(Event.CHANGE, changeHandler);
}
function changeHandler(event:Event):void
{
var comboBox:ComboBox = event.target as ComboBox;
if (comboBox.dataProvider == uniqueProvider)
{
myComboBox2.dataProvider = new DataProvider(appropriateItems);
}
}
This should work if the second dataProvider is determined based on the first dataProvider. This of course requires that your uniqueProvider is a class member variable so it has scope within the handler.

weird behavior of variables in anonymous functions?

Could anyone explain why the trace result of the code below are "5,5,5,5,5" rather than "1,2,3,4,5" and how do you make the anonymous function refer the collect element in the array?( in this example, "var item" should be referring list[0],[1],[2],[3],[4]).
var list:Array=[1,2,3,4,5];
var funcs:Array=[];
for each(var item:int in list){
funcs.push( function(){
trace(item);
});
}
for each(var func:Function in funcs){
func();
}
trace result: 5,5,5,5,5
The problem with your code is that you create a closure which you access later.
For what you want to do you need to create multiple closures that can be later accessed.
var list:Array=[1,2,3,4,5];
var funcs:Array=[];
var closure_factory = function(index) {
return function() { trace(index); };
};
for each(var item:int in list){
funcs.push(closure_factory(item));
}
for each(var func:Function in funcs){
func();
}
This is the result of two things:
Function-level scope in AS3: the variable declaration inside for each(var item:int in list) is equivalent to declaring a var item:int at the beginning of your function (in your example, at the start of your code).
Anonymous functions are closures, which contain not only the code that you specify trace(item), but also the environment for that code to run in. Specifically, each of the anonymous functions created by your code knows that it should use the item variable (declared at function scope) for printing (via trace()).
So, what happens is that item gets assigned all the elements of list, and then retains the last value (which is 5). It exists (does not go out of scope), and when those anonymous functions fire, each of them looks at the same item and prints the same value.

Recursive function with event listener in flex 4

I have written a function which is recursively called. I have maintained an array in which values are pushed with a listener of an event. But the problem is function is returned first without array increment and the listener is executed later.
public function getAllChilds(seltem:XML, allChilds:Array): Array
{
if(//the childs of selected item if need to retrive from server)
var viewChildrenJobsService : HTTPService = new HTTPService();
viewChildrenJobsService.url = // here is my url ;
viewChildrenJobsService .addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void {
// now on this result event i got all childs of selected item.
for each(var childJob :XML in seltem.children())
{
allChilds.push(childJob);
if (//the childs of childJob need to retrive from server)
allChilds = getAllHierarchicalChilds(childJob, allChilds);
}
});
return allChilds;
}
Is there any way to overcome this problem so that the function will return after the completion of the event?
ok , you are returning allchilds before the Result Event. You erase the original question ;) , so i dont remember if your problem was this.
Try to split the function : First the request, then get the value of allChild.
and yes ... is better to write (and paste) good formatted code : its easier to understand.

Passing e:MouseEvent as an argument via setInterval

So i have this function
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);
function captureImage(e:MouseEvent):void {
//lalalala
}
I want to call this function every 2 seconds (after mouse click event happens).
I tried using setInterval
setInterval(captureImage,2000,e:MouseEvent);
but it leads to following error
1084: Syntax error: expecting rightparen before colon.
What's wrong ?
And ya, i am new to AS.
First, since this is AS3 you should be using Timer and TimerEvent. I'll show you how in the example.
Now you'll need to separate your functions:
edit: I've updated this to be safer based on #(Juan Pablo Califano) suggestions. I would keep the same timer for ever if the amount of time isn't going to change.
// first param is milliseconds, second is repeat count (with 0 for infinite)
private var captureTimer:Timer = new Timer(2000, 0);
captureTimer.addEventListener(TimerEvent.TIMER, handleInterval);
function handleClick(event:MouseEvent):void
{
// call here if you want the first capture to happen immediately
captureImage();
// start it
captureTimer.start();
}
function handleInterval(event:TimerEvent):void
{
captureImage();
}
function captureImage():void
{
// lalalala
}
You can also stop the timer with captureTimer.stop() whenever you want.
The problem is that you should use the parameterName:ParameterType syntax only when declaring formal parameters (or when declaring vars and consts). Meaning, this is valid only when you are defining a function:
function func(paramName:Type){
}
When you call the function, you don't have to put the type of the arguments.
So, your function call should look like this:
setInterval(captureImage,2000,e);

Best practice - When to evaluate conditionals of function execution

If I have a function called from a few places, and it requires some condition to be met for anything it does to execute, where should that condition be checked? In my case, it's for drawing - if the mouse button is held down, then execute the drawing logic (this is being done in the mouse movement handler for when you drag.)
Option one says put it in the function so that it's guaranteed to be checked. Abstracted, if you will.
public function Foo() {
DoThing();
}
private function DoThing() {
if (!condition) return;
// do stuff
}
The problem I have with this is that when reading the code of Foo, which may be far away from DoThing, it looks like a bug. The first thought is that the condition isn't being checked.
Option two, then, is to check before calling.
public function Foo() {
if (condition) DoThing();
}
This reads better, but now you have to worry about checking from everywhere you call it.
Option three is to rename the function to be more descriptive.
public function Foo() {
DoThingOnlyIfCondition();
}
private function DoThingOnlyIfCondition() {
if (!condition) return;
// do stuff
}
Is this the "correct" solution? Or is this going a bit too far? I feel like if everything were like this function names would start to duplicate their code.
About this being subjective: of course it is, and there may not be a right answer, but I think it's still perfectly at home here. Getting advice from better programmers than I is the second best way to learn. Subjective questions are exactly the kind of thing Google can't answer.
According to DRY, I'd go with the first one.
public function Foo() {
DoThing();
}
private function DoThing() {
if (!condition) return;
// do stuff
}
Once you get used to the pattern, it's not so unnerving seeing a lone DoThing() in your code. You'll start to read it like a EnsureThingDone().
Option four, wrap the predicate and the actual call in a 3rd function.
function DoThing() {
// do stuff
}
function DoThingOnlyIfCondition() {
if (!condition) return;
DoThing();
}
function Foo() {
DoThingOnlyIfCondition();
}
// Foo version 2
function FooBar() {
DoThing();
}
Now Foo, or whatever function, can use the most appropriate DoXXX() version.
I like to check preconditions inside the function,
public function DoThing()
{
ValidatePreconditions();
DoWork();
}
private function DoWork()
{
//Do the actual work;
}
This way i'm certain all the proper preconditions are met before the execution of my function and there's no need for a consumer to add unnecesary code every time my function is called.
You could use the type system. Make the parameter to DoThing an object that you can only instantiate if the preconditions are passed.
A neat-ish way to do this would be to make DoThing an instance method on that object.