]> source.dussan.org Git - gwtquery.git/commitdiff
Support for multiple request in RF Promise helper
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Tue, 10 Sep 2013 07:10:25 +0000 (09:10 +0200)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Tue, 10 Sep 2013 07:10:25 +0000 (09:10 +0200)
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseRF.java

index 0983ab3ae0ed44d393e9415678d81916a70c8daf..5f573a1db9880490f964354b99bfc03f572f4bff 100644 (file)
  */
 package com.google.gwt.query.client.plugins.deferred;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Set;
 
 import javax.validation.ConstraintViolation;
 
 import com.google.gwt.query.client.plugins.deferred.Deferred.DeferredPromiseImpl;
 import com.google.web.bindery.requestfactory.shared.Receiver;
+import com.google.web.bindery.requestfactory.shared.Request;
+import com.google.web.bindery.requestfactory.shared.RequestContext;
 import com.google.web.bindery.requestfactory.shared.ServerFailure;
 
 /**
@@ -27,7 +31,10 @@ import com.google.web.bindery.requestfactory.shared.ServerFailure;
  *    Request<SessionProxy> req1 = loginFact.api().login(null, null);
  *    Request<UserProxy> req2 = srvFact.api().getCurrentUser();
  *    
+ *    // We can use `when` to append different requests
  *    Promise requestingAll = Deferred.when(new PromiseRF(req1), new PromiseRF(req2);
+ *    // Or we can use just one promise for multiple RF requests
+ *    Promise requestingAll = new PromiseRF(req1, req2);
  *    
  *    requestingAll.done(new Function() {
  *        public void f() {
@@ -43,19 +50,50 @@ import com.google.web.bindery.requestfactory.shared.ServerFailure;
  * </pre>
  */
 public class PromiseRF extends DeferredPromiseImpl {
-  public <T> PromiseRF(com.google.web.bindery.requestfactory.shared.Request<T> request) {
-    request.fire(new Receiver<T>() {
-      public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
-        dfd.reject(new ServerFailure("ConstraintViolation"), violations);
-      }
+  private int total = 0;
+  private List<Object> responses = new ArrayList<Object>();
+  private List<RequestContext> contexts = new ArrayList<RequestContext>();
+  
+  /**
+   * Fire a RF Request.
+   */
+  public <T> PromiseRF(Request<T> request) {
+    this(new Request<?>[] {request});
+  }
 
-      public void onFailure(ServerFailure error) {
-        dfd.reject(error);
+  /**
+   * Fire multiple RF Requests.
+   *
+   * Unlike RequestContext.append which only supports compatible requests,
+   * we can append any kind of requestContexts here.
+   */
+  public PromiseRF(Request<?>[] requests) {
+    for (Request<?> request : requests) {
+      total ++;
+      request.to(new Receiver<Object>() {
+        public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
+          dfd.reject(new ServerFailure("ConstraintViolation"), violations);
+        }
+        public void onFailure(ServerFailure error) {
+          dfd.reject(error);
+        }
+        public void onSuccess(Object response) {
+          responses.add(response);
+          // Resolve only when all requests have been received
+          if (responses.size() == total) {
+            dfd.resolve(responses.toArray(new Object[responses.size()]));
+          }
+        }
+      });
+      if (!contexts.contains(request.getRequestContext())) {
+        contexts.add(request.getRequestContext());
       }
+    }
 
-      public void onSuccess(T response) {
-        dfd.resolve(response);
-      }
-    });
+    // We fire each context instead of appending them so as we can deal
+    // with different request factories.
+    for (RequestContext ctx : contexts) {
+      ctx.fire();
+    }
   }
 }