Last active
August 29, 2015 14:18
-
-
Save clemp6r/9e625d4ccaeb555bdcfc to your computer and use it in GitHub Desktop.
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
import com.github.clemp6r.futuroid.Async; | |
import com.github.clemp6r.futuroid.Future; | |
import java.util.concurrent.Callable; | |
import rx.Observable; | |
import rx.subjects.PublishSubject; | |
/** | |
* Helper for integrating Futuroid with Rx. | |
*/ | |
public class RxFuturoid { | |
/** | |
* Creates a Futuroid async task wrapped by an Rx Observable. | |
*/ | |
public static <T> Observable<T> start(Callable<T> task) { | |
PublishSubject<T> subject = PublishSubject.create(); | |
Future<T> future = Async.submit(task); | |
future.addSuccessCallback(result -> { | |
subject.onNext(result); | |
subject.onCompleted(); | |
}); | |
future.addFailureCallback(t -> { | |
subject.onError(t); | |
subject.onCompleted(); | |
}); | |
return subject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment