From e85c1a8f6bd970a62a1c91c427804a1fa36aee3f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sun, 17 Mar 2013 15:34:50 +0100 Subject: [PATCH] Implement Promises for GWT RPC services --- .../gwt/query/client/plugins/Deferred.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java index 2c60b104..fbef19d1 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java @@ -21,6 +21,7 @@ import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.plugins.callbacks.Callbacks; +import com.google.gwt.user.client.rpc.AsyncCallback; /** * Implementation of jQuery.Deferred for gwtquery. @@ -28,8 +29,8 @@ import com.google.gwt.query.client.plugins.callbacks.Callbacks; public class Deferred extends GQuery implements Promise.Deferred { /** - * public class used to create customized promises which can manipulate - * the asociated deferred object. + * Utility class used to create customized promises which can manipulate + * the associated deferred object. *
    *    Promise doSomething = new PromiseFunction() {
    *      @Override
@@ -41,11 +42,11 @@ public class Deferred extends GQuery implements Promise.Deferred {
    *    
    *    doSomething.progress(new Function() {
    *      public void f() {
-   *        Window.alert("" + arguments[0]);
+   *        String hi = getArgument(0);
    *      }
    *    }).done(new Function() {
    *      public void f() {
-   *        Window.alert("" + arguments[0]);
+   *        String done = getArgument(0);
    *      }
    *    });
    * 
@@ -60,7 +61,33 @@ public class Deferred extends GQuery implements Promise.Deferred { * new deferred is available. */ public abstract void f(Deferred dfd); - } + } + + /** + * Utility class used to create promises for RPC CallBacks. + *
+   *        PromiseRPC gretting = new PromiseRPC();
+   *        
+   *        gretting.fail(new Function(){
+   *          public void f() {
+   *            Throwable error = getArgument(0);
+   *          }
+   *        }).done(new Function(){
+   *          public void f() {
+   *            String response = getArgument(0);
+   *          }
+   *        });
+   * 
+ */ + public static class PromiseRPC extends DeferredPromiseImpl implements AsyncCallback { + public void onFailure(Throwable caught) { + dfd.reject(caught); + } + + public void onSuccess(T result) { + dfd.resolve(result); + } + } /** * Implementation of the Promise interface which is used internally by Deferred. @@ -72,10 +99,6 @@ public class Deferred extends GQuery implements Promise.Deferred { dfd = o; } - protected DeferredPromiseImpl(DeferredPromiseImpl o) { - dfd = o.dfd; - } - protected DeferredPromiseImpl() { dfd = new com.google.gwt.query.client.plugins.Deferred(); } -- 2.39.5