Re: async code and the callback-chain

2009-06-12 Thread Ravi

Daniel,
First thing, With Asynchronous call you should never get the Stack
over flow as Asynchronous calls are running in different thread and
your function retursn back before calls finish.
But i think you have genuine problem of asynchronous call
chains...where writing code become too problematic.
I had the same issue and long back i started this discussion that
having synchronous calls will be a good idea too., so that you don't
need to write async callback and leave it up to the developer if they
want to use synchronous calls.

But that time then i had to create a single class with one boolean
variable for each kind of call and correpsonding data.
So whenevr a call finishes i call the function of that class as

MySIngleTonCLass.setLoginCallFNished(true)
MySIngleTonCLass.setUser(User)
MySIngleTonCLass.refresh();

if some other class finishes then i call something like

MySIngleTonCLass.setDataRetrivedCall1(true)
MySIngleTonCLass.setDataRetrived1(someData)
MySIngleTonCLass.refresh();

MySIngleTonCLass.setDataRetrivedCall2(true)
MySIngleTonCLass.setDataRetrived2(someData2)
MySIngleTonCLass.refresh();

and then had to write my login in MySIngletonClass to update UI. ( my
problem area was updating the UI)

so i do something like

if(getLoginCallFNished() and user != null)
create Logout Link
else
create loginLink

if(getDataRetrivedCall1() and data1!= null)
Display the Data
else
Display No Data found

So yopu need to see if such kind of solution will fir to your problem.

But definitely asynchrnous call WILL not give you stack overflow. It
must be your code somewhere.

Ravi



On Jun 12, 2:14 pm, daniel d.brelov...@googlemail.com wrote:
 hi all,

 I hope someone can help to find a solution to the following problem:

 I have some complex code that may need data from the server. When I
 make a request I get the data asynchronous, so I have to provide a
 callback to handle the response. Now all the code that use this
 function need a callback too. But this leads to very long chains of
 nested callbacks. Especially when I need to call async methods in a
 loop and every call may depend on the previous, I can`t find a way to
 do this without calling the method recursive in the onSuccess of the
 callback. But this way I get a StackOverflow very fast.

 I can`t request all data first and run the code that needs it after
 that, cause which data is needed may depend on the data I already got.

 I have really no idea - hopefully you have

 thx in advance
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google Web Toolkit group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: async code and the callback-chain

2009-06-12 Thread daniel

Hi Ravi,

thanks for your reply.

You`re right. ts not the async call itself that leads to the
stackoverflow. its because of the callback chain that comes when a
large amount of code depends directly or indirectly on the callback.

supposed i have some simple code like this:

public Value doSomething(Value v){
 Value newV = getValue();
 //do something with the values
 //e.g. add them
 return newV.add(v);
}

public Value execute(){
  Value v1 = getValue();
  Value v2 = doSomething(v1);
  Value v3 = doSomesting(v2);
  return doSomething(v3);
}



everything is fine with this... but when i decide that getValue()
should request the Value from the Server i have to rewrite the code
to:

private void doSomething(final Value v, final AsyncCallbackValue
callback){
getValue(new AsyncCallbackValue(){
public void onSuccess(Value result) {
//do something with the values
//e.g. add them
callback.onSuccess(result.add(v));
}
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
});
}

public void execute(final AsyncCallbackValue callback){
getValue(new AsyncCallbackValue(){
public void onSuccess(Value result) {
doSomething(result, new AsyncCallbackValue(){
public void onSuccess(Value result) {
doSomething(result, new 
AsyncCallbackValue(){
public void onSuccess(Value 
result) {
doSomething(result, new 
AsyncCallbackValue(){
public void 
onSuccess(Value result) {

callback.onSuccess(result);
}
public void 
onFailure(Throwable caught) {

callback.onFailure(caught);
}
});
}
public void onFailure(Throwable 
caught) {

callback.onFailure(caught);
}
});
}
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
});
}
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
});
}

of course i would not write such code, but it illustrates the problem.
The chain of callbacks gets longer with every call to doSomething.
Imagine it should be called in a loop for 100 times or more ... than a
stackoverflow occures. And even if getValue doesn`t need to aks the
server every time (maybe most values are cached on clientside) the
code must be written in this way. I can reduce the amount of written
code by providing a recursive method for such calls, but i don't find
a way to get rid of the callback chain.

On 12 Jun., 18:06, Ravi ping2r...@gmail.com wrote:
 Daniel,
 First thing, With Asynchronous call you should never get the Stack
 over flow as Asynchronous calls are running in different thread and
 your function retursn back before calls finish.
 But i think you have genuine problem of asynchronous call
 chains...where writing code become too problematic.
 I had the same issue and long back i started this discussion that
 having synchronous calls will be a good idea too., so that you don't
 need to write async callback and leave it up to the developer if they
 want to use synchronous calls.

 But that time then i had to create a single class with one boolean
 variable for each kind of call and correpsonding data.
 So whenevr a call finishes i call the function of that class as

 MySIngleTonCLass.setLoginCallFNished(true)
 MySIngleTonCLass.setUser(User)
 MySIngleTonCLass.refresh();

 if some other class finishes then i call something like

 MySIngleTonCLass.setDataRetrivedCall1(true)
 MySIngleTonCLass.setDataRetrived1(someData)
 MySIngleTonCLass.refresh();

 MySIngleTonCLass.setDataRetrivedCall2(true)
 MySIngleTonCLass.setDataRetrived2(someData2)
 MySIngleTonCLass.refresh();

 and then had to write my login in MySIngletonClass to update UI. ( my
 problem area was updating the UI)

 so i do something like

 if(getLoginCallFNished() and user != null)