aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-11-26 11:50:13 +0100
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-11-26 11:50:13 +0100
commitc922c32b544b5344ef91cbbb2669c38963de479d (patch)
treefde574c782fa3631d540ac977d3a05cfa5b4dec6
parent2b90b372ac110f564c08dafa17439cc028020c00 (diff)
downloadgwtquery-c922c32b544b5344ef91cbbb2669c38963de479d.tar.gz
gwtquery-c922c32b544b5344ef91cbbb2669c38963de479d.zip
Change PromiseRF namespace, so as we dont break the -strict flag. fixes issue 237
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java2
-rw-r--r--gwtquery-core/src/main/java/com/google/web/bindery/requestfactory/shared/gquery/PromiseRF.java100
2 files changed, 101 insertions, 1 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java
index 58dcf461..82d1ff74 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java
@@ -30,7 +30,7 @@ public class Deferred implements Promise.Deferred {
* Implementation of the Promise interface which is used internally by
* Deferred.
*/
- static class DeferredPromiseImpl implements Promise {
+ public static class DeferredPromiseImpl implements Promise {
// Using 'int' instead of 'enum' because we use them as indexes as well
private static final int DONE = 0, FAIL = 1, PROGRESS = 2;
diff --git a/gwtquery-core/src/main/java/com/google/web/bindery/requestfactory/shared/gquery/PromiseRF.java b/gwtquery-core/src/main/java/com/google/web/bindery/requestfactory/shared/gquery/PromiseRF.java
new file mode 100644
index 00000000..76be5b89
--- /dev/null
+++ b/gwtquery-core/src/main/java/com/google/web/bindery/requestfactory/shared/gquery/PromiseRF.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.web.bindery.requestfactory.shared.gquery;
+
+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;
+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;
+
+/**
+ * Utility class used to create promises for RequestFactory services.
+ * <pre>
+ * 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() {
+ * SessionProxy session = arguments(0, 0);
+ * UserProxy user = arguments(1, 0);
+ * }
+ * })
+ * .fail(new Function() {
+ * public void f() {
+ * ServerFailure failure = arguments(0);
+ * }
+ * });
+ * </pre>
+ */
+public class PromiseRF extends DeferredPromiseImpl {
+ 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});
+ }
+
+ /**
+ * 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());
+ }
+ }
+
+ // We fire each context instead of appending them so as we can deal
+ // with different request factories.
+ for (RequestContext ctx : contexts) {
+ ctx.fire();
+ }
+ }
+}