Class AsyncResource<V>
- Direct Known Subclasses:
AsyncMedia.PauseRequest, AsyncMedia.PlayRequest, BrowserWindow.EvalRequest, MessageEvent.PromptPromise, Oauth2.RefreshTokenRequest
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classException to wrap exceptions that are thrown during asynchronous execution.static classException thrown when the AsyncResource is cancelled. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(AsyncResource<V> resource) Adds another AsyncResource as a listener to this async resource.static AsyncResource<Boolean> all(AsyncResource<?>... resources) Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready.static AsyncResource<Boolean> all(Collection<AsyncResource<?>> resources) Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready.Wraps this AsyncResource object as aPromisevoidawait()Waits and blocks until this AsyncResource is done.static voidawait(AsyncResource<?>... resources) Waits for a set of AsyncResources to be complete.static voidawait(Collection<AsyncResource<?>> resources) Waits for a set of AsyncResources to be complete.booleancancel(boolean mayInterruptIfRunning) Cancels loading the resource.voidSets the resource value.voidSets the error for this resource in the case that it could not be loaded.except(SuccessCallback<Throwable> callback) Sets callback to run if an error occurs.except(SuccessCallback<Throwable> callback, EasyThread t) Sets callback to run if an error occurs.get()Gets the resource synchronously.get(int timeout) Gets the resource synchronously.Gets the resource if it is ready.booleanChecks if the resource loading was cancelled.static booleanReturns true if the provided throwable was caused by a cancellation of an AsyncResource.booleanisDone()Checks if the resource loading is done.booleanisReady()Checks if the resource is ready.voidonResult(AsyncResult<V> onResult) Combines ready() and except() into a single callback with 2 parameters.ready(SuccessCallback<V> callback) Runs the provided callback when the resource is ready.ready(SuccessCallback<V> callback, EasyThread t) Runs the provided callback when the resource is ready.voidwaitFor()Wait for loading to complete.Methods inherited from class Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
-
Constructor Details
-
AsyncResource
public AsyncResource()
-
-
Method Details
-
isCancelled
Returns true if the provided throwable was caused by a cancellation of an AsyncResource.
Parameters
t: The exception to check for a cancellation.
Returns
True if the exception was caused by cancelling an AsyncResource.
Since
7.0
-
all
Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready. And will fire an exception if any of the provided resources fires an exception.
Parameters
resources: One ore more resources to wrap.
Returns
A combined AsyncResource.
Since
7.0
-
all
Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready. And will fire an exception if any of the provided resources fires an exception.
Parameters
resources: One ore more resources to wrap.
Returns
A combined AsyncResource.
Since
7.0
-
await
public static void await(Collection<AsyncResource<?>> resources) throws AsyncResource.AsyncExecutionException Waits for a set of AsyncResources to be complete. If any of them fires an exception, then this method will throw a RuntimeException with that exception as the cause.
Parameters
resources: The resources to wait for.
Since
7.0
-
await
public static void await(AsyncResource<?>... resources) throws AsyncResource.AsyncExecutionException Waits for a set of AsyncResources to be complete. If any of them fires an exception, then this method will throw a RuntimeException with that exception as the cause.
Parameters
resources: The resources to wait for.
Since
7.0
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) Cancels loading the resource.
Parameters
mayInterruptIfRunning
Returns
True if the resource loading was cancelled. False if the loading was already done.
-
waitFor
public void waitFor()Wait for loading to complete. If on EDT, this will use invokeAndBlock to safely block until loading is complete. -
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
The wrapped resource.
Throws
AsyncExecutionException: if the resource failed with an error. To get the actual error, useThrowable#getCause().
-
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.
Parameters
timeout: Timeout
Returns
The wrapped resource.
Throws
-
AsyncExecutionException: if the resource failed with an error. To get the actual error, useThrowable#getCause(). -
InterruptedException: if timeout occurs.
- Throws:
InterruptedException
-
get
-
isCancelled
public boolean isCancelled()Checks if the resource loading was cancelled. -
isDone
public boolean isDone()Checks if the resource loading is done. This will be true even if the resource loading failed with an error. -
isReady
public boolean isReady()Checks if the resource is ready. -
ready
Runs the provided callback when the resource is ready.
If an
EasyThreadis 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
-
callback: Callback to run when the resource is ready. -
t: Optional EasyThread on which the callback should be run.
Returns
Self for chaining
-
-
ready
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
callback: The callback to be run when the resource is ready.
Returns
Self for chaining.
-
except
Sets callback to run if an error occurs.
If an
EasyThreadis 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
-
callback: Callback to run on error. -
t: Optional EasyThread to run callback on.
Returns
Self for chaining.
-
-
except
Sets callback to run if an error occurs. 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 error() method is invoked on.
Parameters
callback: The callback to run in case of error.
-
complete
Sets the resource value. This will trigger the ready callbacks to be run.
Parameters
value: The value to set for the resource.
-
error
Sets the error for this resource in the case that it could not be loaded. This will trigger the error callbacks.
Parameters
t
-
await
Waits and blocks until this AsyncResource is done.
Throws
com.codename1.util.AsyncResource.AsyncExecutionException
-
addListener
Adds another AsyncResource as a listener to this async resource.
Parameters
resource
Since
7.0
-
onResult
Combines ready() and except() into a single callback with 2 parameters.
Parameters
onResult: @param onResult 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.
Since
7.0
-
asPromise
-