]> source.dussan.org Git - gwtquery.git/commitdiff
Fix pipelined promises when the function returned promised was rejected
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Sat, 6 Apr 2013 20:28:02 +0000 (22:28 +0200)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Sat, 6 Apr 2013 20:28:02 +0000 (22:28 +0200)
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java

index f8536dad6178e5267c2011575262ef821cfa4e60..a1955d6ec06ef68413dbb5138aa37e218b6f5330 100644 (file)
@@ -346,7 +346,7 @@ public class QueuePlugin<T extends QueuePlugin<?>> extends GQuery {
    * in the queue.
    */
   public void dequeueIfNotDoneYet(Element elem, String name, Object object) {
-    Queue<?> queue = queue(elem, name, null);
+    Queue queue = queue(elem, name, null);
     if (queue != null && object.equals(queue.peek())) {
       dequeueCurrentAndRunNext(elem, name);
     }
@@ -368,7 +368,7 @@ public class QueuePlugin<T extends QueuePlugin<?>> extends GQuery {
   }
 
   private void stop(Element elem, String name, boolean clear, boolean jumpToEnd) {
-    Queue<?> q = queue(elem, name, null);
+    Queue q = queue(elem, name, null);
     if (q != null) {
       Object f = q.peek();
       if (clear) {
index a81c92e7f7938969f6157ce450dea57f90fa359f..4b71f2e01c6d1beb42fd830acb79af91b7dbe208 100644 (file)
@@ -189,4 +189,8 @@ public class Callbacks {
     }
     return true;
   }
+  
+  public String status() {
+    return (stack == null ? 0 : stack.length()) + " " + done;
+  }
 }
index 11ce75c15a65a7a151a1da435937be544262723a..803ec601ab4c4bbb3b65b29da126dd38b556e4ca 100644 (file)
@@ -39,6 +39,10 @@ public class Deferred extends GQuery implements Promise.Deferred {
     private static class ThenFunction extends Function {
       // Used internally in ThenFunction, to resolve deferred object
       private class DoFunction extends Function {
+        int type;
+        public DoFunction(int type) {
+          this.type = type;
+        }
         public void f() {
           if (type == 0) dfd.resolve(getArguments());
           if (type == 1) dfd.reject(getArguments());
@@ -60,24 +64,26 @@ public class Deferred extends GQuery implements Promise.Deferred {
       }
 
       public void f() {
-        Object[] args = getArguments();
-        Function doIt = new DoFunction().setArguments(args);
+        final Object[] args = getArguments();
+        Function doIt = new DoFunction(type).setArguments(args);
         if (filter != null) {
           // We filter resolved arguments with the filter function
           Object newArgs = filter.setArguments(args).f(args);
-          // If filter function returns a promise we pipeline it
+          // If filter function returns a promise we pipeline it and don't resolve this
           if (newArgs instanceof Promise) {
             Promise p = (Promise) newArgs;
-            if (type == 0) p.done(doIt);
-            if (type == 1) p.fail(doIt);
-            if (type == 2) p.progress(doIt);
+            p.done(new DoFunction(0));
+            p.fail(new DoFunction(1));
+            p.progress(new DoFunction(2));
             return;
+          // Otherwise we change the arguments with the new args
           } else if (newArgs.getClass().isArray()) {
             doIt.setArguments((Object[])newArgs);
           } else {
             doIt.setArguments(newArgs);
           }
         }
+        // run the function with the new args to resolve this deferred
         doIt.f();
       }
     }    
index 005b4cb489c3361683d99b346d610428f8d3b042..f20426cbb846295703f49e22e22ea157c13a3764 100644 (file)
@@ -22,6 +22,7 @@ import static com.google.gwt.query.client.GQuery.document;
 
 import com.google.gwt.core.client.Duration;
 import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.query.client.Promise.Deferred;
 import com.google.gwt.query.client.plugins.ajax.Ajax;
 import com.google.gwt.query.client.plugins.deferred.Callbacks;
 import com.google.gwt.query.client.plugins.deferred.Callbacks.Callback;
@@ -327,6 +328,46 @@ public class GQueryDeferredTestGwt extends GWTTestCase {
       });
   }
   
+  public void testDeferredAjaxThenFail() {
+    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 Function() {
+        public Object f(Object... args) {
+          return new PromiseFunction() {
+            public void f(Deferred dfd) {
+              dfd.reject(arguments);
+            }
+          };
+        }
+      })
+      .done(new Function() {
+        public void f() {
+          finishTest();
+          fail();
+        }
+      })
+      .fail(new Function() {
+        public void f() {
+          assertEquals("message", arguments(0));
+          finishTest();
+        }
+      });
+  }
+
   public void testDeferredQueueDelay() {
     final int delay = 300;
     final double init = Duration.currentTimeMillis();