From: Manuel Carrasco MoƱino Date: Sun, 14 Apr 2013 18:06:49 +0000 (+0200) Subject: Fix a NPE in certain callback cases. Add a couple of useful and predefined functions. X-Git-Tag: release-1.4.0~49^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=96a0377cabada8eeed1ec157a11865ef6de4e0aa;p=gwtquery.git Fix a NPE in certain callback cases. Add a couple of useful and predefined functions. --- 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 c6d36d72..23ee2254 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 @@ -149,7 +149,7 @@ public class Callbacks { private boolean run(Object c, Object...o) { // Unbox array inside array when there is only an element. // It happens when running filters in Promise.then() - if (o.length == 1 && o[0].getClass().isArray()) { + if (o != null && o.length == 1 && o[0] != null && o[0].getClass().isArray()) { o = (Object[])o[0]; } if (c instanceof Callback) { 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 e0257426..c804784f 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 @@ -87,10 +87,48 @@ public class Deferred implements Promise.Deferred { // run the function with the new args to resolve this deferred doIt.f(); } - } + } protected com.google.gwt.query.client.plugins.deferred.Deferred dfd; + /** + * Utility function which can be used inside classes extending this to + * resolve this deferred in a call to any other promise method. + * + * Example: + *
+     * new PromiseFunction() {
+     *   public void f(final Deferred dfd) {
+     *     anotherPromise.done( resolve );
+     *   }
+     * }
+     * 
+ */ + protected Function resolve = new Function() { + public void f() { + dfd.resolve(arguments); + }; + }; + + /** + * Utility function which can be used inside classes extending this to + * reject this deferred in a call to any other promise method. + * + * Example: + *
+     * new PromiseFunction() {
+     *   public void f(final Deferred dfd) {
+     *     anotherPromise.done( reject );
+     *   }
+     * }
+     * 
+ */ + protected Function reject = new Function() { + public void f() { + dfd.reject(arguments); + }; + }; + protected DeferredPromiseImpl() { dfd = new com.google.gwt.query.client.plugins.deferred.Deferred(); }