From 35724b48452c7f839e683692b8b584330ec82077 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Mon, 8 Apr 2013 23:24:30 +0200 Subject: [PATCH] Naming int idexes, and adding a couple of comments to inner function to understand better the code --- .../gwt/query/client/plugins/QueuePlugin.java | 28 +++++++++++-------- .../client/plugins/deferred/Callbacks.java | 2 +- .../client/plugins/deferred/Deferred.java | 15 ++++++---- 3 files changed, 27 insertions(+), 18 deletions(-) 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 a1955d6e..0834a3d8 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 @@ -148,24 +148,30 @@ public class QueuePlugin> extends GQuery { public Promise promise(final String name) { final Promise.Deferred dfd = Deferred(); - // This is the resolve function which will be added to each element - new Function() { - int l = 1; - public void f() { - if (--l == 0) { - dfd.resolve(QueuePlugin.this); - } - } + // This is the unique instance of the resolve function which will be added to each element. + final Function resolve = new Function() { + // Because it is an inner function, the counter cannot final outside the function + int count = 1; + // Inner functions don't have constructors, we use a block to initialize it { for (Element elem: elements()) { - // Add this hook function only to those elements with active queue + // Add this resolve function only to those elements with active queue if (queue(elem, name, null) != null) { emptyHooks(elem, name).add(this); - l++; + count++; } } } - }.f(this, name); + + public void f() { + if (--count == 0) { + dfd.resolve(QueuePlugin.this); + } + } + }; + + // Run the function and resolve it in case there are not elements with active queue + resolve.f(this, name); return dfd.promise(); } 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 4b71f2e0..1e43ad01 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 @@ -163,7 +163,7 @@ public class Callbacks { private void addAll(Object...o) { for (Object c : o) { - if (!done && c != null && (!opts.getUnique() || !stack.contains(c))) { + if (!done && stack != null && c != null && (!opts.getUnique() || !stack.contains(c))) { stack.add(c); } // In jQuery add always is run when memory is true even when unique is set 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 803ec601..6c9cf95a 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 @@ -35,6 +35,9 @@ public class Deferred extends GQuery implements Promise.Deferred { */ static class DeferredPromiseImpl implements Promise { + // Using 'int' instead of 'enum' because we use type as array index as well + private static final int DONE = 0, FAIL = 1, PROGRESS = 2; + // Private class used to handle `Promise.then()` private static class ThenFunction extends Function { // Used internally in ThenFunction, to resolve deferred object @@ -44,9 +47,9 @@ public class Deferred extends GQuery implements Promise.Deferred { this.type = type; } public void f() { - if (type == 0) dfd.resolve(getArguments()); - if (type == 1) dfd.reject(getArguments()); - if (type == 2) dfd.notify(getArguments()); + if (type == DONE) dfd.resolve(getArguments()); + if (type == FAIL) dfd.reject(getArguments()); + if (type == PROGRESS) dfd.notify(getArguments()); } } @@ -72,9 +75,9 @@ public class Deferred extends GQuery implements Promise.Deferred { // If filter function returns a promise we pipeline it and don't resolve this if (newArgs instanceof Promise) { Promise p = (Promise) newArgs; - p.done(new DoFunction(0)); - p.fail(new DoFunction(1)); - p.progress(new DoFunction(2)); + p.done(new DoFunction(DONE)); + p.fail(new DoFunction(FAIL)); + p.progress(new DoFunction(PROGRESS)); return; // Otherwise we change the arguments with the new args } else if (newArgs.getClass().isArray()) { -- 2.39.5