java.lang.Object
org.praxislive.code.userapi.Async<T>
- Type Parameters:
T- result type
A lightweight holder for a future value, the result of an asynchronous
operation such as an actor call. An Async can also reflect the failure of
such an operation.
An Async can be explicitly completed, with a value or error. Completion can only happen once.
Async is not thread safe and is not designed for concurrent operation. Use from a single thread, or protect appropriately.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA queue for handling Async instances.static interfaceAsync.Task<T,R> A task intended to be run asynchronously and outside of the main component context. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidBind a target Async to complete when a source Async completes.booleanComplete the Async with the provided value.static <T> Async<T> completed(T value) Create an Async that is already completed with the given value.booleandone()Whether this Async has been completed, with or without error.error()Get the failure error or null.static <T> Async<T> extractArg(Async<Call> asyncCall, Class<T> type) Create an Async that will complete when the provided async call completes, by extracting the first call argument and attempting to map to the given type.static <T> Async<T> extractArg(Async<Call> asyncCall, Class<T> type, int argIdx) Create an Async that will complete when the provided async call completes, by extracting the indexed call argument and attempting to map to the given type.booleanComplete the Async unsuccessfully with the provided error.booleanfailed()Whether this Async completed with a failure.static <T> Async<T> Create an Async that is already failed with the given error.result()Get the result of this Async if completed without error, otherwise null.static <T> CompletableFuture<T> toCompletableFuture(Async<T> async) A utility method for linking an Async with aCompletableFuturefor passing to external APIs.
-
Constructor Details
-
Async
public Async()Construct an empty Async.
-
-
Method Details
-
result
Get the result of this Async if completed without error, otherwise null.- Returns:
- result or null
-
error
Get the failure error or null.- Returns:
- error or null
-
done
public boolean done()Whether this Async has been completed, with or without error.- Returns:
- true if done
-
failed
public boolean failed()Whether this Async completed with a failure.- Returns:
- true if failed
-
complete
Complete the Async with the provided value. If this Async is already completed, with or without failure, its state remains the same and this method returns false.- Parameters:
value- value to complete the Async- Returns:
- true if completed
-
fail
Complete the Async unsuccessfully with the provided error. If this Async is already completed, with or without failure, its state remains the same and this method returns false.- Parameters:
error- error to complete the Async- Returns:
- true if completed
-
bind
Bind a target Async to complete when a source Async completes. When the source Async completes, the target will be completed with the same result or error.- Type Parameters:
T- result type- Parameters:
source- source asynctarget- target async
-
completed
Create an Async that is already completed with the given value.- Type Parameters:
T- value type- Parameters:
value- value to complete the Async- Returns:
- new completed Async
-
failed
Create an Async that is already failed with the given error.- Type Parameters:
T- value type- Parameters:
error- error to fail the Async- Returns:
- new failed Async
-
extractArg
Create an Async that will complete when the provided async call completes, by extracting the first call argument and attempting to map to the given type. The returned Async will complete with an error if the call completes with an error, the argument isn't available, or the argument cannot be mapped to the given type.- Type Parameters:
T- type of created async- Parameters:
asyncCall- async calltype- class type of created async- Returns:
- created async
-
extractArg
Create an Async that will complete when the provided async call completes, by extracting the indexed call argument and attempting to map to the given type. The returned Async will complete with an error if the call completes with an error, the argument isn't available, or the argument cannot be mapped to the given type.- Type Parameters:
T- type of created async- Parameters:
asyncCall- async calltype- class type of created asyncargIdx- index of argument to extract- Returns:
- created async
-
toCompletableFuture
A utility method for linking an Async with aCompletableFuturefor passing to external APIs.IMPORTANT : do not use completable futures returned from this method inside component code. To react to Async completion from within component code, use an
Async.Queue.The completable future will automatically complete with the result or failure of the Async. The link is one way - the Async will not respond to any changes to the future.
- Type Parameters:
T- async and future type- Parameters:
async- async to link to created future- Returns:
- created future
-