]> source.dussan.org Git - gwtquery.git/commitdiff
Adding a new utility class to easily use pipelining. Add a isPending method to promises
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Fri, 12 Apr 2013 08:41:29 +0000 (10:41 +0200)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Fri, 12 Apr 2013 08:41:29 +0000 (10:41 +0200)
gwtquery-core/src/main/java/com/google/gwt/query/client/Promise.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java [new file with mode: 0644]
gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java

index 3785b9c31eecc74cf5ec8cec6900c635bf65fb90..bb6a8e1372817c192dfb9721c4496d69fd6a26bf 100644 (file)
@@ -110,4 +110,9 @@ public interface Promise {
    * Determine whether a Deferred object has been rejected.
    */
   boolean isRejected();
+
+  /**
+   * Determine whether a Deferred object is pending.
+   */
+  boolean isPending();
 }
index a00a39b3c5f03998aee4158df43132bea82f9c4a..0b49551c77217321b7f2fb234a4b9d75bb0d9029 100644 (file)
@@ -142,6 +142,10 @@ public class Deferred implements Promise.Deferred {
     public boolean isRejected() {
       return Promise.REJECTED.equals(state());
     }
+
+    public boolean isPending() {
+      return Promise.PENDING.equals(state());
+    }
   }
 
   /**
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java
new file mode 100644 (file)
index 0000000..f4ef139
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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.gwt.query.client.plugins.deferred;
+
+import com.google.gwt.query.client.Function;
+import com.google.gwt.query.client.Promise.Deferred;
+
+/**
+ * Utility class used to create customized functions with a deferred 
+ * execution in pipelined processes.
+ * 
+ * They have access to the associated deferred object via a method which
+ * will be called only in the case the previous promise is resolved
+ *
+ * <pre>
+ *    Promise doSomething = new PromiseFunction() {
+ *      @Override
+ *      public void f(Deferred dfd) {
+ *        dfd.notify("hi");
+ *        dfd.resolve("done");
+ *      }
+ *    };
+ *    
+ *    doSomething.then(new FunctionDeferred() {
+ *      public void f(Deferred dfd) {
+ *        dfd.resolve("deferred " + arguments(0));
+ *      }
+ *    });
+ * </pre>
+ */
+public abstract class FunctionDeferred extends Function {
+  
+  protected Deferred dfd;
+  
+  /**
+   * This function is called once the the previous promise in the
+   * pipe is resolved, and the new created deferred is available.
+   * 
+   * You have to override it, and resolve the new promise
+   */
+  protected abstract void f(Deferred dfd);
+  
+  /**
+   * This function is called when the previous promise in the pipe
+   * is resolved. 
+   */
+  public final Object f(Object... args) {
+    return new PromiseFunction() {
+      public void f(Deferred dfd) {
+        FunctionDeferred.this.dfd = dfd;
+        FunctionDeferred.this.f(dfd);
+      }
+    };
+  }
+
+}
index b2ce198befb0136eec572c4d839d4b619d13a864..a5c010ff57dd348698510ac8baf1624ef23538e4 100644 (file)
@@ -18,6 +18,7 @@ package com.google.gwt.query.client.deferred;
 import com.google.gwt.junit.client.GWTTestCase;
 import com.google.gwt.query.client.Function;
 import com.google.gwt.query.client.GQuery;
+import com.google.gwt.query.client.Promise.Deferred;
 import com.google.gwt.query.client.plugins.deferred.Callbacks;
 import com.google.gwt.query.client.plugins.deferred.Callbacks.Callback;
 import com.google.gwt.query.client.plugins.deferred.PromiseFunction;
@@ -162,34 +163,59 @@ public class DeferredTest extends GWTTestCase {
       }
     });
   }
-  
-  public void testDeferredAjaxThenFail() {
+
+  public void testDeferredThenDone() {
     delayTestFinish(5000);
-    
+
     GQuery
       .when(new PromiseFunction() {
         public void f(Deferred dfd) {
           dfd.resolve("message");
         }
       })
-      .then(new Function() {
-        public Object f(Object... args) {
-          return new PromiseFunction() {
-            public void f(Deferred dfd) {
-              dfd.resolve(arguments);
-            }
-          };
+      .then(new FunctionDeferred() {
+          public void f(Deferred dfd) {
+            dfd.resolve("then1 " + arguments[0]);
+          }
+      })
+      .then(new FunctionDeferred() {
+          public void f(Deferred dfd) {
+            dfd.resolve("then2 " + arguments[0]);
+          }
+      })
+      .fail(new Function() {
+        public void f() {
+          finishTest();
+          fail();
         }
       })
-      .then(new Function() {
-        public Object f(Object... args) {
-          return new PromiseFunction() {
-            public void f(Deferred dfd) {
-              dfd.reject(arguments);
-            }
-          };
+      .done(new Function() {
+        public void f() {
+          assertEquals("then2 then1 message", arguments(0));
+          finishTest();
+        }
+      });
+  }
+
+  public void testDeferredThenFail() {
+    delayTestFinish(5000);
+
+    GQuery
+      .when(new PromiseFunction() {
+        public void f(Deferred dfd) {
+          dfd.resolve("message");
         }
       })
+      .then(new FunctionDeferred() {
+          public void f(Deferred dfd) {
+            dfd.resolve("then1 " + arguments[0]);
+          }
+      })
+      .then(new FunctionDeferred() {
+          public void f(Deferred dfd) {
+            dfd.reject("then2 " + arguments[0]);
+          }
+      })
       .done(new Function() {
         public void f() {
           finishTest();
@@ -198,11 +224,21 @@ public class DeferredTest extends GWTTestCase {
       })
       .fail(new Function() {
         public void f() {
-          assertEquals("message", arguments(0));
+          assertEquals("then2 then1 message", arguments(0));
           finishTest();
         }
       });
   }
-  
 
+  public static abstract class FunctionDeferred extends Function {
+    public abstract void f(Deferred dfd);
+
+    public Object f(Object... args) {
+      return new PromiseFunction() {
+        public void f(Deferred dfd) {
+          FunctionDeferred.this.f(dfd);
+        }
+      };
+    }
+  }
 }