Skip to content

Instantly share code, notes, and snippets.

@clemp6r
Last active August 29, 2015 14:18
Show Gist options
  • Save clemp6r/9e625d4ccaeb555bdcfc to your computer and use it in GitHub Desktop.
Save clemp6r/9e625d4ccaeb555bdcfc to your computer and use it in GitHub Desktop.
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