Class AbstractPromise<D,F,P>
java.lang.Object
org.jdeferred2.impl.AbstractPromise<D,F,P>
- All Implemented Interfaces:
Promise<D,F, P>
- Direct Known Subclasses:
DeferredObject
-
Nested Class Summary
Nested classes/interfaces inherited from interface Promise
Promise.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<AlwaysCallback<? super D, ? super F>> protected final List<DoneCallback<? super D>> protected final List<FailCallback<? super F>> protected final org.slf4j.Loggerprotected final List<ProgressCallback<? super P>> protected Fprotected Dprotected Promise.State -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionalways(AlwaysCallback<? super D, ? super F> callback) This method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred.reject(Object)),AlwaysCallbackwill be triggered.done(DoneCallback<? super D> callback) This method will registerDoneCallbackso that when a Deferred object is resolved (Deferred.resolve(Object)),DoneCallbackwill be triggered.fail(FailCallback<? super F> callback) This method will registerFailCallbackso that when a Deferred object is rejected (Deferred.reject(Object)),FailCallbackwill be triggered.filter(DoneFilter<? super D, ? extends D_OUT> doneFilter) Equivalent tofilter(doneFilter, null, null)filter(DoneFilter<? super D, ? extends D_OUT> doneFilter, FailFilter<? super F, ? extends F_OUT> failFilter) Equivalent tofilter(doneFilter, failFilter, null)<D_OUT,F_OUT, P_OUT>
Promise<D_OUT, F_OUT, P_OUT> filter(DoneFilter<? super D, ? extends D_OUT> doneFilter, FailFilter<? super F, ? extends F_OUT> failFilter, ProgressFilter<? super P, ? extends P_OUT> progressFilter) This method will register filters such that when a Deferred object is either resolved (Deferred.resolve(Object)), rejected (Deferred.reject(Object)) or is notified of progress (Deferred.notify(Object)), the corresponding filter will be invoked.protected voidhandleException(CallbackExceptionHandler.CallbackType callbackType, Exception e) booleanQueries the state of this promise, returningtrueiff it isState.PENDING.booleanQueries the state of this promise, returningtrueiff it isState.REJECTED.booleanQueries the state of this promise, returningtrueiff it isState.RESOLVED.<D_OUT,F_OUT, P_OUT>
Promise<D_OUT, F_OUT, P_OUT> pipe(DonePipe<? super D, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> donePipe, FailPipe<? super F, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> failPipe, ProgressPipe<? super P, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> progressPipe) This method will register pipes such that when a Deferred object is either resolved (Deferred.resolve(Object)), rejected (Deferred.reject(Object)) or is notified of progress (Deferred.notify(Object)), the corresponding pipe will be invoked.pipe(DonePipe<? super D, ? extends D_OUT, ? extends F_OUT, ? extends P> donePipe, FailPipe<? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> failPipe) Equivalent topipe(DonePipe, FailPipe, null)Equivalent to {#code pipe(DonePipe, null, null)}pipeAlways(AlwaysPipe<? super D, ? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> alwaysPipe) This method will register a pipe such that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred.reject(Object)) the pipe will be invoked.progress(ProgressCallback<? super P> callback) This method will registerProgressCallbackso that when a Deferred object is notified of progress (Deferred.notify(Object)),ProgressCallbackwill be triggered.state()then(DoneCallback<? super D> callback) Equivalent toPromise.done(DoneCallback)then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback) Equivalent toPromise.done(DoneCallback).Promise.fail(FailCallback)then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback, ProgressCallback<? super P> progressCallback) protected voidtriggerAlways(AlwaysCallback<? super D, ? super F> callback, Promise.State state, D resolve, F reject) protected voidtriggerAlways(Promise.State state, D resolve, F reject) protected voidtriggerDone(D resolved) protected voidtriggerDone(DoneCallback<? super D> callback, D resolved) protected voidtriggerFail(F rejected) protected voidtriggerFail(FailCallback<? super F> callback, F rejected) protected voidtriggerProgress(ProgressCallback<? super P> callback, P progress) protected voidtriggerProgress(P progress) voidThis method will wait as long as the State is Pending.voidwaitSafely(long timeout) This method will wait when the State is Pending, and return when timeout has reached.
-
Field Details
-
log
protected final org.slf4j.Logger log -
state
-
doneCallbacks
-
failCallbacks
-
progressCallbacks
-
alwaysCallbacks
-
resolveResult
-
rejectResult
-
-
Constructor Details
-
AbstractPromise
public AbstractPromise()
-
-
Method Details
-
state
-
done
Description copied from interface:PromiseThis method will registerDoneCallbackso that when a Deferred object is resolved (Deferred.resolve(Object)),DoneCallbackwill be triggered. If the Deferred object is already resolved then theDoneCallbackis triggered immediately. You can register multipleDoneCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.progress(new DoneCallback(){ public void onDone(Object done) { ... } }); -
fail
Description copied from interface:PromiseThis method will registerFailCallbackso that when a Deferred object is rejected (Deferred.reject(Object)),FailCallbackwill be triggered. If the Deferred object is already rejected then theFailCallbackis triggered immediately. You can register multipleFailCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.fail(new FailCallback(){ public void onFail(Object rejection) { ... } }); -
always
Description copied from interface:PromiseThis method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred.reject(Object)),AlwaysCallbackwill be triggered. If the Deferred object is already resolved or rejected then theAlwaysCallbackis triggered immediately. You can register multipleAlwaysCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.AlwaysCallbacks are triggered after anyDoneCallbackorFailCallbackrespectively.promise.always(new AlwaysCallback(){ public void onAlways(State state, Object result, Object rejection) { if (state == State.RESOLVED) { // do something with result } else { // do something with rejection } } }); -
triggerDone
-
triggerDone
-
triggerFail
-
triggerFail
-
triggerProgress
-
triggerProgress
-
triggerAlways
-
triggerAlways
protected void triggerAlways(AlwaysCallback<? super D, ? super F> callback, Promise.State state, D resolve, F reject) -
progress
Description copied from interface:PromiseThis method will registerProgressCallbackso that when a Deferred object is notified of progress (Deferred.notify(Object)),ProgressCallbackwill be triggered. You can register multipleProgressCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.progress(new ProgressCallback(){ public void onProgress(Object progress) { // e.g., update progress in the GUI while the background task is still running. } }); -
then
Description copied from interface:PromiseEquivalent toPromise.done(DoneCallback) -
then
public Promise<D,F, thenP> (DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback) Description copied from interface:PromiseEquivalent toPromise.done(DoneCallback).Promise.fail(FailCallback)- Specified by:
thenin interfacePromise<D,F, P> - Parameters:
doneCallback- seePromise.done(DoneCallback)failCallback- seePromise.fail(FailCallback)- Returns:
thisfor chaining more calls
-
then
public Promise<D,F, thenP> (DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback, ProgressCallback<? super P> progressCallback) Description copied from interface:PromiseEquivalent toPromise.done(DoneCallback).Promise.fail(FailCallback).Promise.progress(ProgressCallback)- Specified by:
thenin interfacePromise<D,F, P> - Parameters:
doneCallback- seePromise.done(DoneCallback)failCallback- seePromise.fail(FailCallback)progressCallback- seePromise.progress(ProgressCallback)- Returns:
thisfor chaining more calls
-
filter
Description copied from interface:PromiseEquivalent tofilter(doneFilter, null, null) -
filter
public <D_OUT,F_OUT> Promise<D_OUT,F_OUT, filterP> (DoneFilter<? super D, ? extends D_OUT> doneFilter, FailFilter<? super F, ? extends F_OUT> failFilter) Description copied from interface:PromiseEquivalent tofilter(doneFilter, failFilter, null) -
filter
public <D_OUT,F_OUT, Promise<D_OUT,P_OUT> F_OUT, filterP_OUT> (DoneFilter<? super D, ? extends D_OUT> doneFilter, FailFilter<? super F, ? extends F_OUT> failFilter, ProgressFilter<? super P, ? extends P_OUT> progressFilter) Description copied from interface:PromiseThis method will register filters such that when a Deferred object is either resolved (Deferred.resolve(Object)), rejected (Deferred.reject(Object)) or is notified of progress (Deferred.notify(Object)), the corresponding filter will be invoked. The result of the filter will be used to invoke the same action on the returned promise.DoneFilterandFailFilterwill be triggered at the time the Deferred object is resolved or rejected. If the Deferred object is already resolved or rejected the filter is triggered immediately. Filters allow to transform the outcome of a promise into something else. This concept is equivalent to the map() method of the java stream API. If any of the filter is not specified (null), a default No Op filter is used. If your filter is returning aPromiseconsider usingPromise.pipe(DonePipe, FailPipe, ProgressPipe).Deferred deferred = new DeferredObject(); Promise promise = deferred.promise(); Promise filtered = promise.filter(new DoneFilterinvalid input: '<'Integer, Integer>() { Integer filterDone(Integer result) { return result * 10; } }); filtered.then(new DoneCallback() { void onDone(Integer result) { System.out.println(result); } }); deferred.resolve(1); // prints 10 - Specified by:
filterin interfacePromise<D,F, P> - Parameters:
doneFilter- the filter to execute when a result is available. Ifnull, useFilteredPromise.NoOpDoneFilterfailFilter- the filter to execute when a failure is available. Ifnull, useFilteredPromise.NoOpFailFilterprogressFilter- the filter to execute when progress info is available. Ifnull, useFilteredPromise.NoOpProgressFilter- Returns:
- a new promise for the filtered result, failure and progress.
-
pipe
-
pipe
-
pipe
public <D_OUT,F_OUT, Promise<D_OUT,P_OUT> F_OUT, pipeP_OUT> (DonePipe<? super D, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> donePipe, FailPipe<? super F, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> failPipe, ProgressPipe<? super P, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> progressPipe) Description copied from interface:PromiseThis method will register pipes such that when a Deferred object is either resolved (Deferred.resolve(Object)), rejected (Deferred.reject(Object)) or is notified of progress (Deferred.notify(Object)), the corresponding pipe will be invoked.DonePipeandFailPipewill be triggered at the time the Deferred object is resolved or rejected. If the Deferred object is already resolved or rejected the filter is triggered immediately. This method is similar to JQuery's pipe() method, where a newPromiseis returned by the the pipe filter instead of the original. This is useful to handle return values and then rewiring it to different callbacks. Pipes start a newDeferredobject. This allows to chain asynchronous calls. If your pipe does not do any asynchronous work consider usingPromise.filter(DoneFilter, FailFilter, ProgressFilter)promise.pipe(new DonePipeinvalid input: '<'Integer, Integer, String, Void>() { @Override Deferredinvalid input: '<'Integer, Void, Void> pipeDone(Integer result) { // Reject values greater than 100 if (result > 100) { return new DeferredObjectinvalid input: '<'Integer, Void, Void>().reject("Failed"); } else { return new DeferredObjectinvalid input: '<'Integer, Void, Void>().resolve(result); } } }).done(...) .fail(...);- Specified by:
pipein interfacePromise<D,F, P> - Parameters:
donePipe- the pipe to invoke when a result is available. Ifnull, result is piped unchangedfailPipe- the pipe to invoke when a failure is available. Ifnull, failure is piped unchangedprogressPipe- the pipe to execute when progress info is available. Ifnull, progress is piped unchanged- Returns:
- a new promise for the piped result, failure and progress.
-
pipeAlways
public <D_OUT,F_OUT> Promise<D_OUT,F_OUT, pipeAlwaysP> (AlwaysPipe<? super D, ? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> alwaysPipe) Description copied from interface:PromiseThis method will register a pipe such that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred.reject(Object)) the pipe will be invoked.AlwaysPipewill be triggered at the time the Deferred object is resolved or rejected. If the Deferred object is already resolved or rejected the filter is triggered immediately. This method is similar to JQuery's pipe() method, where a newPromiseis returned by the the pipe filter instead of the original. This is useful to handle return values and then rewiring it to different callbacks. Pipes start a newDeferredobject. This allows to chain asynchronous calls.promise.pipeAlways(new pipeinvalid input: '<'Integer, Integer, String, String, Void>() { @Override Promiseinvalid input: '<'Integer, Void, Void> pipeAlways(State state, Integer resolved, Integer rejected) { if (state == State.RESOLVED) { return new DeferredObjectinvalid input: '<'String, String, Void>().resolve("Success"); } else { return new DeferredObjectinvalid input: '<'String, String, Void>().reject("Failed"); } } }).done(...) .fail(...);- Specified by:
pipeAlwaysin interfacePromise<D,F, P> - Parameters:
alwaysPipe- the pipe to invoke when a result or failure is available.- Returns:
- a new promise for the piped result or failure.
-
isPending
-
isResolved
-
isRejected
-
waitSafely
Description copied from interface:PromiseThis method will wait as long as the State is Pending. This method will return fast when State is not Pending.- Specified by:
waitSafelyin interfacePromise<D,F, P> - Throws:
InterruptedException- if thread is interrupted while waiting
-
waitSafely
Description copied from interface:PromiseThis method will wait when the State is Pending, and return when timeout has reached. This method will return fast when State is not Pending.- Specified by:
waitSafelyin interfacePromise<D,F, P> - Parameters:
timeout- the maximum time to wait in milliseconds- Throws:
InterruptedException- if thread is interrupted while waiting
-
handleException
-