Last active
April 3, 2019 08:41
-
-
Save mgodave/7646319 to your computer and use it in GitHub Desktop.
Create a single value Observable from a Guava ListenableFuture.
This file contains 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 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