From: Manuel Carrasco MoƱino Date: Fri, 12 Apr 2013 08:41:29 +0000 (+0200) Subject: Adding a new utility class to easily use pipelining. Add a isPending method to promises X-Git-Tag: release-1.4.0~53^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7f8d07213b49568b1bacbf05f60be0599225cea0;p=gwtquery.git Adding a new utility class to easily use pipelining. Add a isPending method to promises --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Promise.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Promise.java index 3785b9c3..bb6a8e13 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Promise.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Promise.java @@ -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(); } 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 a00a39b3..0b49551c 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 @@ -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 index 00000000..f4ef1396 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java @@ -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 + * + *
+ *    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));
+ *      }
+ *    });
+ * 
+ */ +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); + } + }; + } + +} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java index b2ce198b..a5c010ff 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java @@ -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); + } + }; + } + } }