A - The type of the action.R - The type of the result.public interface RpcInterceptor<A,R> extends Interceptor<A,R>
RpcInterceptor that has been registered with the bound
RpcInterceptorRegistry is called and
DispatchAsync does not automatically
send the command over HTTP to the server.
Interceptors provide a number of flexible options:
RpcInterceptor can take over and communicate directly with the server, possibly using a
different mechanism.
// Interface of cache singleton
public interface Cache {
<A extends Action<R>, R> R get(A action);
<A extends Action<R>, R> void put(A action, R result);
}
// Interceptor that injects the cache
public class RetrieveFooInterceptor
extends AbstractCachingInterceptor<RetrieveFooAction, RetrieveFooResult> {
@Inject
RetrieveFooInterceptor(Cache cache) {
super(RetrieveFooAction.class, cache);
}
}
// abstract interceptor that:
// - first checks cache and returns result immediately if found in cache
// - executes command on server
// - saves result to cache before returning it
public abstract class AbstractCachingInterceptor<A extends Action<R>, R>
extends AbstractInterceptor<A, R> {
private final Cache cache;
public AbstractCachingInterceptor(Class<A> actionType, Cache cache) {
super(actionType);
this.cache = cache;
}
@Override
public DispatchRequest execute(final A action, final AsyncCallback<R> resultCallback,
ExecuteCommand<A, R> executeCommand) {
R cacheResult = cache.get(action);
if (cacheResult != null) {
resultCallback.onSuccess(cacheResult);
return new CompletedDispatchRequest();
} else {
return executeCommand.execute(action, new AsyncCallback<R>() {
@Override
public void onSuccess(R result) {
if(!request.isCancelled()) {
cache.put(action, result);
resultCallback.onSuccess(result);
}
}
@Override
public void onFailure(Throwable caught) {
resultCallback.onFailure(caught);
}
});
}
}
}
| Modifier and Type | Method and Description |
|---|---|
com.gwtplatform.dispatch.shared.DispatchRequest |
undo(A action,
R result,
com.google.gwt.user.client.rpc.AsyncCallback<Void> callback,
UndoCommand<A,R> undoCommand)
Undoes the specified action if supported.
|
canExecute, execute, getActionTypecom.gwtplatform.dispatch.shared.DispatchRequest undo(A action, R result, com.google.gwt.user.client.rpc.AsyncCallback<Void> callback, UndoCommand<A,R> undoCommand)
DelegatingDispatchRequest.isPending() against the request parameter.action - The action to undo.result - The result to undo.callback - The callback to use to indicate when the action has been undone. Unless the request is
cancelled, you must invoke AsyncCallback.onSuccess(T) on this callback when you have
successfully undone the action. If any failure occurs call AsyncCallback.onFailure(java.lang.Throwable).undoCommand - Call UndoCommand.undo(Object, Object,
com.google.gwt.user.client.rpc.AsyncCallback) on this object to send the action over to
the server via gwt-rpc. As a parameter you can pass callback or your custom
AsyncCallback if you want to perform any processing following the undo.DispatchRequest object. Never return null, instead return a new
CompletedDispatchRequest if you executed,
cancelled or ignored the action.Copyright © 2010–2017 Arcbees. All rights reserved.