public class AsyncResource<V>
- Object
- Observable
- AsyncResource
Known subtypesClassicDiscovery, BleScan, Oauth2.RefreshTokenRequest, AsyncMedia.PauseRequest, AsyncMedia.PlayRequest, BrowserWindow.EvalRequest, MessageEvent.PromptPromise
Nested types
class AsyncResource.AsyncExecutionException | Exception to wrap exceptions that are thrown during asynchronous execution. |
class AsyncResource.CancellationException | Exception thrown when the AsyncResource is cancelled. |
Constructors
public AsyncResource() |
Methods
Inherited methods
Constructor details
AsyncResource
public AsyncResource()Method details
isCancelled
public static boolean isCancelled(Throwable t)Parameters
tThrowable- The exception to check for a cancellation.
Returns
all
public static AsyncResource<Boolean> all(AsyncResource<?>... resources)Parameters
resourcesAsyncResource<?>...- One ore more resources to wrap.
Returns
all
public static AsyncResource<Boolean> all(Collection<AsyncResource<?>> resources)Parameters
resourcesCollection<AsyncResource<?>>- One ore more resources to wrap.
Returns
await
public static void await(Collection<AsyncResource<?>> resources)
throws AsyncResource.AsyncExecutionExceptionParameters
resourcesCollection<AsyncResource<?>>- The resources to wait for.
await
public static void await(AsyncResource<?>... resources)
throws AsyncResource.AsyncExecutionExceptionParameters
resourcesAsyncResource<?>...- The resources to wait for.
cancel
public boolean cancel(boolean mayInterruptIfRunning)Returns
True if the resource loading was cancelled. False if the loading was already done.
Cancellation notifies observers, which is how a waiter learns the
resource became terminal. It used to call setChanged() and stop there,
leaving the flag set with nothing notified: a thread inside get()
adds an observer, checks isDone(), and then waits, so a cancel
landing after that check woke nobody and the waiter blocked forever on
a resource that had already finished.
It deliberately does not run the callbacks registered through
ready(SuccessCallback) or except(SuccessCallback). Cancelling
means the caller has stopped listening, and publishing to it anyway
would contradict a contract the rest of the framework is built on and
tests – see the AI language and vision suites, which assert that a
cancelled operation delivers neither a value nor an error even when the
backend answers afterwards. Observers are the internal wake mechanism
for get() and waitFor(); they are not the application’s callbacks.
One consequence worth knowing: cleanup wired through
onResult(AsyncResult) does not run on cancellation either. Anything
that must be released has to be released by whoever owns it. A
per-operation timeout timer, for instance, survives until its deadline
and then retires itself on finding the resource already done.
waitFor
public void waitFor()get
public V get()Gets the resource synchronously. This will wait until either the resource failed with an exception, or the loading was canceled, or was done without error.
If on edt, this uses invokeAndBlock to block safely.
Returns
Throws
AsyncExecutionException- if the resource failed with an error. To get the actual error, use
Throwable#getCause().
get
public V get(int timeout)
throws InterruptedExceptionGets the resource synchronously. This will wait until either the resource failed with an exception, or the loading was canceled, or was done without error.
If on edt, this uses invokeAndBlock to block safely.
Parameters
timeoutint- Timeout
Returns
Throws
AsyncExecutionException- if the resource failed with an error. To get the actual error, use
Throwable#getCause(). InterruptedException- if timeout occurs.
get
public V get(V defaultVal)Returns
isCancelled
public boolean isCancelled()isDone
public boolean isDone()isReady
public boolean isReady()ready
public AsyncResource<V> ready(SuccessCallback<V> callback, EasyThread t)Runs the provided callback when the resource is ready.
If an EasyThread is provided, then the callback will be run on that
thread. If an EasyThread is not provided, and this call is made on the EDT, then
the callback will be run on the EDT. Otherwise, the callback will occur on
whatever thread the #complete(java.lang.Object) call is called on.
Parameters
callbackSuccessCallback<V>- Callback to run when the resource is ready.
tEasyThread- Optional EasyThread on which the callback should be run.
Returns
ready
public AsyncResource<V> ready(SuccessCallback<V> callback)Runs the provided callback when the resource is ready.
If this call is made on the EDT, then the callback will be run on the EDT. Otherwise, it will be run on whatever thread the complete() methdo is invoked on.
Parameters
callbackSuccessCallback<V>- The callback to be run when the resource is ready.
Returns
except
public AsyncResource<V> except(SuccessCallback<Throwable> callback, EasyThread t)Sets callback to run if an error occurs.
If an EasyThread is provided, then the callback will be run on that
thread. If an EasyThread is not provided, and this call is made on the EDT, then
the callback will be run on the EDT. Otherwise, the callback will occur on
whatever thread the #complete(java.lang.Object) call is called on.
Parameters
callbackSuccessCallback<Throwable>- Callback to run on error.
tEasyThread- Optional EasyThread to run callback on.
Returns
except
public AsyncResource<V> except(SuccessCallback<Throwable> callback)Parameters
callbackSuccessCallback<Throwable>- The callback to run in case of error.
complete
public void complete(V value)Parameters
valueV- The value to set for the resource.
error
public void error(Throwable t)await
public void await()
throws AsyncResource.AsyncExecutionExceptionaddListener
public void addListener(AsyncResource<V> resource)onResult
public void onResult(AsyncResult<V> onResult)Parameters
onResultAsyncResult<V>- A callback that handles both the ready() case and the except() case. Use
#isCancelled(java.lang.Throwable)to test the error parameter ofjava.lang.Throwable)to see if if was caused by a cancellation.
asPromise
public Promise<V> asPromise()Promise