co.easimart.EasimartTaskUtils Maven / Gradle / Ivy
package co.easimart;
import java.util.concurrent.CancellationException;
import bolts.AggregateException;
import bolts.Continuation;
import bolts.Task;
/** package */ class EasimartTaskUtils {
/**
* Converts a task execution into a synchronous action.
*/
//TODO (grantland): Task.cs actually throws an AggregateException if the task was cancelled with
// TaskCancellationException as an inner exception or an AggregateException with the original
// exception as an inner exception if task.isFaulted().
// https://msdn.microsoft.com/en-us/library/dd235635(v=vs.110).aspx
/* package */ static T wait(Task task) throws EasimartException {
try {
task.waitForCompletion();
if (task.isFaulted()) {
Exception error = task.getError();
if (error instanceof EasimartException) {
throw (EasimartException) error;
}
if (error instanceof AggregateException) {
throw new EasimartException(error);
}
if (error instanceof RuntimeException) {
throw (RuntimeException) error;
}
throw new RuntimeException(error);
} else if (task.isCancelled()) {
throw new RuntimeException(new CancellationException());
}
return task.getResult();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//region Task to Callbacks
/**
* Calls the callback after a task completes on the main thread, returning a Task that completes
* with the same result as the input task after the callback has been run.
*/
/* package */ static Task callbackOnMainThreadAsync(Task task,
final EasimartCallback1 callback) {
return callbackOnMainThreadAsync(task, callback, false);
}
/**
* Calls the callback after a task completes on the main thread, returning a Task that completes
* with the same result as the input task after the callback has been run. If reportCancellation
* is false, the callback will not be called if the task was cancelled.
*/
/* package */ static Task callbackOnMainThreadAsync(Task task,
final EasimartCallback1 callback, final boolean reportCancellation) {
if (callback == null) {
return task;
}
return callbackOnMainThreadAsync(task, new EasimartCallback2() {
@Override
public void done(Void aVoid, EasimartException e) {
callback.done(e);
}
}, reportCancellation);
}
/**
* Calls the callback after a task completes on the main thread, returning a Task that completes
* with the same result as the input task after the callback has been run.
*/
/* package */ static Task callbackOnMainThreadAsync(Task task,
final EasimartCallback2 callback) {
return callbackOnMainThreadAsync(task, callback, false);
}
/**
* Calls the callback after a task completes on the main thread, returning a Task that completes
* with the same result as the input task after the callback has been run. If reportCancellation
* is false, the callback will not be called if the task was cancelled.
*/
/* package */ static Task callbackOnMainThreadAsync(Task task,
final EasimartCallback2 callback, final boolean reportCancellation) {
if (callback == null) {
return task;
}
final Task.TaskCompletionSource tcs = Task.create();
task.continueWith(new Continuation() {
@Override
public Void then(final Task task) throws Exception {
if (task.isCancelled() && !reportCancellation) {
tcs.setCancelled();
return null;
}
EasimartExecutors.main().execute(new Runnable() {
@Override
public void run() {
try {
Exception error = task.getError();
if (error != null && !(error instanceof EasimartException)) {
error = new EasimartException(error);
}
callback.done(task.getResult(), (EasimartException) error);
} finally {
if (task.isCancelled()) {
tcs.setCancelled();
} else if (task.isFaulted()) {
tcs.setError(task.getError());
} else {
tcs.setResult(task.getResult());
}
}
}
});
return null;
}
});
return tcs.getTask();
}
//endregion
}