--- /dev/null
+/*
+ * 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
+ *
+ * <pre>
+ * 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));
+ * }
+ * });
+ * </pre>
+ */
+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);
+ }
+ };
+ }
+
+}
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;
}
});
}
-
- 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();
})
.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);
+ }
+ };
+ }
+ }
}