如果类具有用户信息,则函数canReveal()
返回 true 。ClientState
如果没有,它首先尝试使用异步调用来获取该用户信息GetUser
。我需要在里面做的IF
是等待这个异步调用返回(onSuccess
),这样我就可以检查ClientState
现在是否有用户信息。我怎样才能做到这一点?谢谢
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();
}