Callback - advantageous/reakt GitHub Wiki
Callback is a generic event handler which can be thought of as a callback handler.
This is like an async future or callback. This was modeled after QBit's callback and Guava's Callback, and JavaScripts callbacks. A Result
represents the result or error from an async operation
and is passed to onResult
.
package io.advantageous.reakt;
import org.junit.Test;
import static org.junit.Assert.*;
public class CallbackTest {
@Test
public void test() throws Exception {
TestService testService = new TestService();
Result<Employee>[] results = new Result[1];
Employee[] employee = new Employee[1];
testService.simple(result -> {
results[0] = result;
result.then(e -> employee[0] = e).catchError(error -> {
System.err.println(error.getMessage());
});
});
assertTrue(results[0].complete());
assertFalse(results[0].failure());
assertTrue(results[0].success());
assertNotNull(employee[0]);
}
@Test
public void testError() throws Exception {
TestService testService = new TestService();
Result<Employee>[] results = new Result[1];
testService.error(result -> {
results[0] = result;
});
assertTrue(results[0].complete());
assertTrue(results[0].failure());
assertFalse(results[0].success());
}
@Test
public void testException() throws Exception {
TestService testService = new TestService();
Result<Employee>[] results = new Result[1];
testService.exception(result -> {
results[0] = result;
});
assertTrue(results[0].complete());
assertTrue(results[0].failure());
assertFalse(results[0].success());
}
static class Employee {
private final String id;
Employee(String id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return id != null ? id.equals(employee.id) : employee.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
public static class TestService {
public void simple(Callback<Employee> callback) {
callback.reply(new Employee("Rick"));
}
public void error(Callback<Employee> callback) {
callback.reject("Error");
}
public void exception(Callback<Employee> callback) {
callback.reject(new IllegalStateException("Error"));
}
}
}
package io.advantageous.reakt;
import io.advantageous.reakt.impl.ResultImpl;
/**
* A generic event handler which can be thought of as a callback handler.
* <p>
* This is like an async future or promise.
* <p>
* This was modeled after QBit's callback, and JavaScripts callbacks.
* The {@link Result} result represents the result or error from an async operation.
*
* @param <T> type of result returned from callback
*/
public interface Callback<T> {
/**
* (Client view)
* A result was returned so handle it.
* <p>
* This is registered from the callers (or event receivers perspective).
* A client of a service would override {@code onResult}.
*
* @param result to handle
*/
void onResult(Result<T> result);
/**
* (Service view)
* This allows services to send back a failed result easily to the client/handler.
* <p>
* This is a helper methods for producers (services that produce results) to send a failed result.
*
* @param error error
*/
default void reject(final Throwable error) {
onResult(new ResultImpl<>(error));
}
/**
* (Service view)
* This allows services to send back a failed result easily to the client/handler.
* <p>
* This is a helper methods for producers (services that produce results) to send a failed result.
*
* @param errorMessage error message
*/
default void reject(final String errorMessage) {
onResult(new ResultImpl<>(new IllegalStateException(errorMessage)));
}
/**
* (Service view)
* This allows services to send back a result easily to the client/handler.
* <p>
* This is a helper methods for producers (services that produce results) to send a result.
*
* @param result result value to send.
*/
default void reply(final T result) {
onResult(new ResultImpl<>(result));
}
}