I am looking for a way to automate showing and hiding a \'loading\' message when calling an async service, so instead of doing this:
showLoadingWidget();
se
You could wrap the call itself in an object that handles displaying the loading message, maybe retrying a few times on errors or whatever. Something like this:
public abstract class AsyncCall implements AsyncCallback {
/** Call the service method using cb as the callback. */
protected abstract void callService(AsyncCallback cb);
public void go(int retryCount) {
showLoadingMessage();
execute(retryCount);
}
private void execute(final int retriesLeft) {
callService(new AsyncCallback() {
public void onFailure(Throwable t) {
GWT.log(t.toString(), t);
if (retriesLeft <= 0) {
hideLoadingMessage();
AsyncCall.this.onFailure(t);
} else {
execute(retriesLeft - 1);
}
}
public void onSuccess(T result) {
hideLoadingMessage();
AsyncCall.this.onSuccess(result);
}
});
}
public void onFailure(Throwable t) {
// standard error handling
}
...
}
To make the call do something like this:
new AsyncCall() {
protected void callService(AsyncCallback cb) {
DemoService.App.get().someService("bla", cb);
}
public void onSuccess(DTO result) {
// do something with result
}
}.go(3); // 3 retries
You could extend this with code to detect calls that are taking a long time and display a busy indicator of some kind etc.