aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-02-27 20:14:22 +0100
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-02-27 20:14:22 +0100
commit21ebc50445074ac48899a309be6341be0421c6ea (patch)
treec0809bffcf8482ea9a0c7b873bba6e549ff7e3c2
parente7949180eba17c7b070bbcce4f75bd3db81d66b7 (diff)
downloadgwtquery-21ebc50445074ac48899a309be6341be0421c6ea.tar.gz
gwtquery-21ebc50445074ac48899a309be6341be0421c6ea.zip
memory elements should be fired after adding new elements, and memory should be reset when a new fire comes. More tests
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java55
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java17
2 files changed, 45 insertions, 27 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java
index db5bed00..58992a4e 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java
@@ -60,7 +60,7 @@ public class Callbacks {
private boolean done = false;
- private JsObjectArray<Object> memory = JsObjectArray.create();
+ private JsObjectArray<Object> memory = null;
public final CallbackOptions opts;
@@ -119,6 +119,8 @@ public class Callbacks {
* Disable a callback list from doing anything more.
*/
public Callbacks disable() {
+ callbacks = JsObjectArray.create();
+ memory = JsObjectArray.create();
done = true;
return this;
}
@@ -127,7 +129,17 @@ public class Callbacks {
* Call all of the callbacks with the given arguments.
*/
public Callbacks fire(Object... o) {
- run(opts.getMemory() ? memory.add(o).elements() : o);
+ if (!done) {
+ done = opts.getOnce();
+ for (Object c : callbacks.elements()) {
+ if (!run(c, o) && opts.getStopOnFalse()) {
+ break;
+ }
+ }
+ if (opts.getMemory()) {
+ memory = JsObjectArray.create().add(o);
+ }
+ }
return this;
}
@@ -139,28 +151,27 @@ public class Callbacks {
return this;
}
- @SuppressWarnings({"unchecked", "rawtypes"})
- private void run(Object... o) {
- if (!done) {
- done = opts.getOnce();
- for (Object c : callbacks.elements()) {
- if (c instanceof Callback) {
- boolean r = ((Callback)c).f(o);
- if (opts.getStopOnFalse() && !r) break;
- } else if (c instanceof Function) {
- ((Function)c).f(o);
- } else if (c instanceof com.google.gwt.core.client.Callback) {
- ((com.google.gwt.core.client.Callback)c).onSuccess(o);
- }
- }
- }
- }
-
private void addAll(Object...o) {
- for (Object i : o) {
- if (!opts.getUnique() || !callbacks.contains(i)) {
- callbacks.add(i);
+ for (Object c : o) {
+ if (!opts.getUnique() || !callbacks.contains(c)) {
+ callbacks.add(c);
+ }
+ // In jQuery add always is run when memory is true even when unique is set
+ if (opts.getMemory() && memory != null) {
+ run(c, memory.elements());
}
}
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ private boolean run(Object c, Object...o) {
+ if (c instanceof Callback) {
+ return ((Callback)c).f(o);
+ } else if (c instanceof Function) {
+ ((Function)c).f(o);
+ } else if (c instanceof com.google.gwt.core.client.Callback) {
+ ((com.google.gwt.core.client.Callback)c).onSuccess(o);
+ }
+ return true;
}
}
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 dfd3f28a..3f5919a5 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
@@ -30,7 +30,7 @@ public class GQueryDeferredTestGwt extends GWTTestCase {
}
private String result = "";
-
+
public void testCallbacks() {
Function fn1 = new Function() {
public void f() {
@@ -67,8 +67,6 @@ public class GQueryDeferredTestGwt extends GWTTestCase {
}
};
-
-
result = "";
Callbacks callbacks = new Callbacks();
callbacks.add( fn1 );
@@ -108,7 +106,9 @@ public class GQueryDeferredTestGwt extends GWTTestCase {
callbacks.fire( "foo" );
callbacks.add( fn2 );
callbacks.fire( "bar" );
- assertEquals(" f1: foo f1: foo bar f2: foo bar", result);
+ callbacks.remove(fn2);
+ callbacks.fire( "foobar" );
+ assertEquals(" f1: foo f2: foo f1: bar f2: bar f1: foobar", result);
result = "";
callbacks = new Callbacks("stopOnFalse");
@@ -123,12 +123,19 @@ public class GQueryDeferredTestGwt extends GWTTestCase {
assertEquals("", result);
result = "";
- callbacks = new Callbacks("once");
+ callbacks = new Callbacks("memory once unique");
+ callbacks.add( fn1 );
callbacks.add( fn1 );
callbacks.fire( "bar" );
assertEquals(" f1: bar", result);
callbacks.fire( "foo" );
assertEquals(" f1: bar", result);
+ callbacks.add( fn2 );
+ callbacks.add( fn2 );
+ assertEquals(" f1: bar f2: bar f2: bar", result);
+ callbacks.remove( fn1 );
+ callbacks.add( fn1 );
+ assertEquals(" f1: bar f2: bar f2: bar f1: bar", result);
}
}