Skip to content

Instantly share code, notes, and snippets.

@mgodave
Last active April 3, 2019 08:41
Show Gist options
  • Save mgodave/7646319 to your computer and use it in GitHub Desktop.
Save mgodave/7646319 to your computer and use it in GitHub Desktop.
Create a single value Observable from a Guava ListenableFuture.
public static <T> Observable<T> create(ListenableFuture<T> future, Executor executor) {
AsyncSubject<T> subject = AsyncSubject.create();
future.addListener(() -> {
try {
T value = future.get();
subject.onNext(value);
subject.onCompleted();
} catch (Exception e) {
subject.onError(e);
}
}, executor);
return subject;
}
public static <T> Observable<T> create(ListenableFuture<T> future) {
return create(future, MoreExecutors.sameThreadExecutor());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment