Function canReveal() returns true if class ClientState has user information. If not, it first tries to get that user information using asyncronous call to GetUser. What I need to do inside the IF is to wait somehow until this asyncronous call returns (onSuccess), so that I can check whether ClientState has now the user information or not. How can I do that? Thanks
public class MyGatekeeper implements Gatekeeper{
private DispatchAsync dispatcher;
@Inject
public MyGatekeeper(DispatchAsync dispatcher) {
this.dispatcher = dispatcher;
}
@Override
public boolean canReveal() {
if(ClientState.isUserLoggedin()==false) {
dispatcher.execute(new GetUser(Window.Location.getHref()),
new DispatchCallback<GetUserResult>() {
@Override
public void onSuccess(GetUserResult result) {
if (!result.getErrorText().isEmpty()) {
Window.alert(result.getErrorText());
return;
}
ClientState.setUserInfo(result.getUserInfo());
}
});
return ClientState.isUserLoggedin(); // WAIT till onSuccess returns!
}
}
return ClientState.isUserLoggedin();
}
The way to do this is to have
canRevealtake aCallback<Boolean>:Unfortunately there’s no way to tell GWT to “wait” for the async callback, since that would basically freeze JS execution, since JS is single-threaded.