Created
June 8, 2016 05:29
-
-
Save mindwing/12f1db87d2dfe7810cc76313e5e872dc to your computer and use it in GitHub Desktop.
Agera Explained - 08.Incrementally Agerifying legacy code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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