Skip to content

Instantly share code, notes, and snippets.

@mindwing
Created June 8, 2016 05:29
Show Gist options
  • Save mindwing/12f1db87d2dfe7810cc76313e5e872dc to your computer and use it in GitHub Desktop.
Save mindwing/12f1db87d2dfe7810cc76313e5e872dc to your computer and use it in GitHub Desktop.
Agera Explained - 08.Incrementally Agerifying legacy code
public class AsyncOperatorRepository<P, R> extends BaseObservable
implements Repository<Result<R>>, Callback<R> {
private final AsyncOperator<P, R> asyncOperator;
private final Supplier<P> paramSupplier;
private Result<R> result;
private Cancellable cancellable;
public AsyncOperatorRepository(AsyncOperator<P, R> asyncOperator,
Supplier<P> paramSupplier) {
this.asyncOperator = asyncOperator;
this.paramSupplier = paramSupplier;
this.result = Result.absent();
}
@Override
protected synchronized void observableActivated() {
cancellable = asyncOperator.request(paramSupplier.get(), this);
}
@Override
protected synchronized void observableDeactivated() {
if (cancellable != null) {
cancellable.cancel();
cancellable = null;
}
}
@Override
public synchronized void onResponse(R response) {
cancellable = null;
result = Result.absentIfNull(response);
dispatchUpdate();
}
@Override
public synchronized Result<R> get() {
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment