您可以使用 RestyGWT 自定义 Dispatcher 来跟踪请求生命周期。Dispatcher 可以手动配置或使用注释(https://resty-gwt.github.io/documentation/restygwt-user-guide.html)。手动设置示例:
RootRestService rest = GWT.create(RootRestService.class);
((RestServiceProxy) rest).setDispatcher(new DefaultDispatcher() {
@Override public Request send(Method m, RequestBuilder rb) throws RequestException {
RequestCallback callback = rb.getCallback();
rb.setCallback(new RequestCallback() {
@Override public void onResponseReceived(Request req, Response res) {
log.info("request success (stop event)");
callback.onResponseReceived(req, res);
}
@Override public void onError(Request req, Throwable ex) {
log.info("request error (stop event)");
callback.onError(req, ex);
}
});
try {
log.info("request initialized (start event)");
return request = super.send(m, rb);
} finally {
log.info("request fail to initialize error (stop event)");
}
}
});
您可以使用 eventBus 发送事件来代替记录,并使用此事件来跟踪活动请求的数量,如果活动请求的数量大于 0,则最终显示加载指示器。