Maybe this will help you.
You can use Command pattern to send request after server responde to
previous request. The idea is. You have one callback, that callback is
given to another callback who in onSuccess() method call the next
command and then give that callback to the finall callback. So to be
more clear, create one interface Command who have only one method execute.
public interface Command {
abstract void execute();
}
Create interface Loader who have two methods add(Command command) and
execute().
public interface Loader {
void add(Command command);
void execute();
}
Create implementation for that interface for example ChainLoader who
have one collection List<Command> command and method add(Command
command) add elements in that collection. The method execute check if
the collection contain any command and if size is 0 return. If size is
not 0 Get first command from the commands, call execute method and
remove that command from the list.
public class ChainLoader implements Loader{
List<Command> commands = new ArrayList<Command>();
@Override
public void add(Command command){
commands.add(command);
}
@Override
public void execute(){
if(commands.size() == 0){
return;
}
Command command = commands.get(0);
command.execute();
if(commands.size() > 0){
commands.remove(0);
}
}
}
Create one CommandChain who implements your AsyncCallback interface, and
in constructor receiveAsyncCallback and Loader.
public class CommandChain<T> implements AsyncCallback<T>{
private AsyncCallback<T> callback;
private Loader loader;
public CommandChain(AsyncCallback<T> callback, Loader loader) {
this.callback = callback;
this.loader = loader;
}
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(T result) {
callback.onSuccess(result);
loader.execute();
}
}
Finaly create CommandChain what you will use to make the chain.
public abstract class EntityCommand<T> implements Command {
private AsyncCallback<T> callback;
public EntityCommand(AsyncCallback<T> callback) {
this.callback = callback;
}
@Override
public void execute(){
proceed(callback);
}
public abstract void proceed(AsyncCallback<T> callback);
}
How you probably say WTF? :) The use is really simple for example i
write one test you can modify in your code:
@Test
public void test(){
AsyncCallback<String> callback0 = new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(String result) {
doSomeStuff(result);
}
};
AsyncCallback<String> callback1 = new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(String result) {
doSomeStuff(result);
}
};
Loader loader = new ChainLoader();
CommandChain<String> chain1 = new CommandChain<String>(callback0,
loader);
Command<String> command1 = new Command<String>(chain1) {
@Override
public void proceed(AsyncCallback<String> callback) {
asyncService.sendRequest(callback);
}
};
loader.add(command1);
CommandChain<String> chain2 = new CommandChain<String>(callback1,
loader);
Command<String> command2 = new Command<String>(chain2) {
@Override
public void proceed(AsyncCallback<String> callback) {
asyncService.sendRequest(callback);
}
};
loader.add(command2);
loader.execute();
}
Good luck ;)
Shawn Pearce wrote:
> On Thu, Oct 15, 2009 at 15:48, AndiMullaraj <[email protected]> wrote:
>
>> There are cases when blocking calls to server are indispensable
>> (agreed, assync calls are the way to go for 99% of the cases).
>>
>> I scanned the client API and cannot find a way on how to do this. If
>> not through a direct API call, is there a way on how to achieve this?
>> (Like having a wait/process_events loop going until the response comes
>> from the server).
>>
>
> No. Most browsers are single threaded. They can't send an HTTP
> request or parse an HTTP response while they are also executing
> JavaScript. So GWT assumes that is the case and requires you to
> return control to the browser. Which means blocking calls are not
> supported.
>
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---