From: Manuel Carrasco MoƱino Date: Sat, 6 Apr 2013 20:28:02 +0000 (+0200) Subject: Fix pipelined promises when the function returned promised was rejected X-Git-Tag: release-1.4.0~56^2~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6eb702a3b7df12e572b214240271c6d52e011881;p=gwtquery.git Fix pipelined promises when the function returned promised was rejected --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java index f8536dad..a1955d6e 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java @@ -346,7 +346,7 @@ public class 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> 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) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java index a81c92e7..4b71f2e0 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java @@ -189,4 +189,8 @@ public class Callbacks { } return true; } + + public String status() { + return (stack == null ? 0 : stack.length()) + " " + done; + } } 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 11ce75c1..803ec601 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 @@ -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(); } } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java index 005b4cb4..f20426cb 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java @@ -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();