public Object f(Object... args) { Response response = arguments(0); if (response.getText().matches(jsonpCbRexp)) { return GQ.create(response.getText().replaceFirst(jsonpCbRexp, "$1")); } else { return GQuery.Deferred().reject().promise(); } } });
/** * Returns a dynamically generated Promise that is resolved once all actions * in the named queue have ended. */ public Promise promise(final String name) { final Promise.Deferred dfd = Deferred(); // 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 resolve function only to those elements with active queue if (queue(elem, name, null) != null) { emptyHooks(elem, name).add(this); count++; } } } 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(); }
/** * This function is called when the previous promise in the pipe * is resolved. */ public final Object f(Object... args) { return dfd != null && (cache == CacheType.ALL || cache == CacheType.RESOLVED && dfd.promise().isResolved() || cache == CacheType.REJECTED && dfd.promise().isRejected()) ? dfd.promise() : new PromiseFunction() { public void f(Deferred dfd) { FunctionDeferred.this.resolve = resolve; FunctionDeferred.this.reject = reject; FunctionDeferred.this.dfd = dfd; FunctionDeferred.this.f(dfd); } }; }
/** * Reset the cache so as a new invocation to this function will * execute it instead of restoring old values from cache. */ public FunctionDeferred resetCache() { if (dfd != null && !dfd.promise().isPending()) { dfd = null; } return this; } }
private Promise then(boolean continueFlow, final Function... f) { final Deferred newDfd = new com.google.gwt.query.client.plugins.deferred.Deferred(); done(new ThenFunction(newDfd, f, DONE, continueFlow)); fail(new ThenFunction(newDfd, f, FAIL, continueFlow)); progress(new ThenFunction(newDfd, f, PROGRESS, continueFlow)); return newDfd.promise(); }