*Coding style*:
Several things. Why aren't your state variables part of an enum? Why
aren't you using a switch statement instead of 10 if/else ifs.
*Architecture*:
Why are you using a timer that will do nothing most of the time? You're
just putting load on the client unnecessarily. Why are you even using an
explicit state machine? With the example below you can easily chain RPC
calls together by putting the appropriate function call in the onSuccess.
This way, you're not needlessly executing code that does nothing every
100milliseconds. You are also getting 0 latency between states (thus better
performance). Stay away from timers unless you are doing animation or
actually need a periodic task. DO NOT USE IT TO SIMULATE AN EVENT LOOP (not
yelling - just an important point to keep in mind).
package com.test.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
public class GWTProcess implements EntryPoint
{
String result1 = "";
String result2 = "";
private static final class LazyWebService {
public static final WebServiceOneAsync webservice = GWT.create
(WebServiceOne.class);
}
public void onModuleLoad()
{
initialize();
buildGui();
}
private void initialize()
{
// nothing to do
}
private void buildGui()
{
Button b = new Button("Click me", new ClickListener()
{
public void onClick(Widget sender)
{
Window.alert("start data load");
fireWebServiceOne();
}
});
RootPanel.get().add(b);
}
private void displayData() {
Window.alert(result1+":"+result2);
}
private void fireWebServiceOne()
{
AsyncCallback<String> callback = new AsyncCallback<String>()
{
public void onFailure(Throwable caught)
{
Window.alert("ERROR READING WEBSERVICE");
}
public void onSuccess(String results)
{
results1 = results;
fireWebServiceTwo();
}
};
LazyWebService.webservice.getData(callback);
}
private void fireWebServiceTwo()
{
AsyncCallback<String> callback = new AsyncCallback<String>()
{
public void onFailure(Throwable caught)
{
Window.alert("ERROR READING WEBSERVICE");
}
public void onSuccess(String results)
{
results2 = results;
displayData();
}
};
LazyWebService.webservice.getData(callback);
}
}
On Thu, Apr 30, 2009 at 9:22 AM, devcybiko <[email protected]> wrote:
>
> I've started using timers combined with a state/transition matrix.
> I've offered a simple example below. The basic idea is to keep a
> 'state' variable with the current state in the program logic. When
> you initiate a asynchronous call, you move the state to "LOADING".
> When the call is complete, you move the state to "RUNNING". You can
> easily chain requests (as in the example) by pushing the state from
> "LOADING_WS1_DATA" to "DONE_LOADING_WS1_DATA" to "LOADING_WS2_DATA" to
> "DONE_LOADING_WS2_DATA" finally back to "RUNNING". Example follows:
>
> package com.test.client;
>
> import com.google.gwt.core.client.EntryPoint;
> import com.google.gwt.core.client.GWT;
> import com.google.gwt.user.client.Timer;
> import com.google.gwt.user.client.Window;
> import com.google.gwt.user.client.rpc.AsyncCallback;
> import com.google.gwt.user.client.ui.Button;
> import com.google.gwt.user.client.ui.ClickListener;
> import com.google.gwt.user.client.ui.RootPanel;
> import com.google.gwt.user.client.ui.Widget;
>
> public class GWTProcess implements EntryPoint
> {
> private static final int ERROR = -2;
> private static final int INIT = -1;
> private static final int RUNNING = 0;
> private static final int BUILD_GUI = 100;
> private static final int START_LOADING_DATA = 200;
> private static final int LOADING_WS1_DATA = 300;
> private static final int DONE_LOADING_WS1_DATA = 301;
> private static final int LOADING_WS2_DATA = 400;
> private static final int DONE_LOADING_WS2_DATA = 401;
> private static final int DISPLAY_DATA = 500;
>
> int state = INIT;
> String result1 = "";
> String result2 = "";
>
>
> public void onModuleLoad()
> {
> Timer t = new Timer()
> {
> public void run()
> {
> if (state == INIT)
> {
> initialize();
> state = BUILD_GUI;
> }
> else if (state == BUILD_GUI)
> {
> buildGui();
> state = RUNNING;
> }
> else if (state == RUNNING)
> {
> // do nothing special
> }
> else if (state == START_LOADING_DATA)
> {
> fireWebServiceOne();
> state = LOADING_WS1_DATA;
> }
> else if (state == LOADING_WS1_DATA)
> {
> // wait patiently
> }
> else if (state == DONE_LOADING_WS1_DATA)
> {
> state = LOADING_WS2_DATA;
> }
> else if (state == LOADING_WS2_DATA)
> {
> fireWebServiceTwo();
> state = LOADING_WS2_DATA;
> }
> else if (state == LOADING_WS2_DATA)
> {
> // wait patiently
> }
> else if (state == DONE_LOADING_WS2_DATA)
> {
> state = DISPLAY_DATA;
> }
> else if (state == DISPLAY_DATA)
> {
> displayData();
> state = RUNNING;
> } else if {state == ERROR) {
> Window.alert("ERROR READING WEBSERVICE");
> state = RUNNING;
> }
> }
> };
>
> // Schedule the timer to every 100 milliseconds.
> t.scheduleRepeating(100);
> }
>
>
> private void initialize()
> {
> // nothing to do
> }
>
>
> private void buildGui()
> {
> Button b = new Button("Click me", new ClickListener()
> {
> public void onClick(Widget sender)
> {
> Window.alert("start data load");
> state = START_LOADING_DATA;
> }
> });
>
> RootPanel.get().add(b);
> }
>
> private void displayData() {
> Window.alert(result1+":"+result2);
> }
> private void fireWebServiceOne()
> {
> WebServiceOneAsync webservice = GWT.create
> (WebServiceOne.class);
>
> AsyncCallback<String> callback = new AsyncCallback<String>()
> {
> public void onFailure(Throwable caught)
> {
> state = ERROR;
> }
>
> public void onSuccess(String results)
> {
> result1 = results;
> state = DONE_LOADING_WS1_DATA;
> }
> };
> webservice.getData(callback);
> }
> private void fireWebServiceTwo()
> {
> WebServiceOneAsync webservice = GWT.create
> (WebServiceOne.class);
>
> AsyncCallback<String> callback = new AsyncCallback<String>()
> {
> public void onFailure(Throwable caught)
> {
> state = ERROR;
> }
>
> public void onSuccess(String results)
> {
> result2 = results;
> state = DONE_LOADING_WS2_DATA;
> }
> };
> webservice.getData(callback);
> }
> }
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---