|
20 | 20 | import java.util.concurrent.FutureTask;
|
21 | 21 |
|
22 | 22 | import rx.Observable;
|
| 23 | +import rx.Observer; |
23 | 24 | import rx.Scheduler;
|
24 | 25 | import rx.Scheduler.Inner;
|
| 26 | +import rx.Subscriber; |
| 27 | +import rx.Subscription; |
25 | 28 | import rx.schedulers.Schedulers;
|
26 | 29 | import rx.subjects.AsyncSubject;
|
| 30 | +import rx.subjects.PublishSubject; |
| 31 | +import rx.subjects.Subject; |
| 32 | +import rx.subscriptions.SerialSubscription; |
27 | 33 | import rx.util.async.operators.Functionals;
|
28 | 34 | import rx.util.async.operators.OperationDeferFuture;
|
29 | 35 | import rx.util.async.operators.OperationForEachFuture;
|
@@ -1711,4 +1717,54 @@ public static <R> Observable<R> fromCallable(Callable<? extends R> callable, Sch
|
1711 | 1717 | public static <R> Observable<R> fromRunnable(final Runnable run, final R result, Scheduler scheduler) {
|
1712 | 1718 | return Observable.create(OperationFromFunctionals.fromRunnable(run, result)).subscribeOn(scheduler);
|
1713 | 1719 | }
|
| 1720 | + /** |
| 1721 | + * Runs the provided action on the given scheduler and allows propagation |
| 1722 | + * of multiple events to the observers of the returned StoppableObservable. |
| 1723 | + * The action is immediately executed and unobserved values will be lost. |
| 1724 | + * @param <T> the output value type |
| 1725 | + * @param scheduler the scheduler where the action is executed |
| 1726 | + * @param action the action to execute, receives an Observer where the events can be pumped |
| 1727 | + * and a Subscription which lets check for cancellation condition. |
| 1728 | + * @return an Observable which provides a Subscription interface to cancel the action |
| 1729 | + */ |
| 1730 | + public static <T> StoppableObservable<T> runAsync(Scheduler scheduler, |
| 1731 | + final Action2<? super Observer<? super T>, ? super Subscription> action) { |
| 1732 | + return runAsync(scheduler, PublishSubject.<T>create(), action); |
| 1733 | + } |
| 1734 | + /** |
| 1735 | + * Runs the provided action on the given scheduler and allows propagation |
| 1736 | + * of multiple events to the observers of the returned StoppableObservable. |
| 1737 | + * The action is immediately executed and unobserved values might be lost, |
| 1738 | + * depending on the subject type used. |
| 1739 | + * @param <T> the output value of the action |
| 1740 | + * @param <U> the output type of the observable sequence |
| 1741 | + * @param scheduler the scheduler where the action is executed |
| 1742 | + * @param subject the subject to use to distribute values emitted by the action |
| 1743 | + * @param action the action to execute, receives an Observer where the events can be pumped |
| 1744 | + * and a Subscription which lets check for cancellation condition. |
| 1745 | + * @return an Observable which provides a Subscription interface to cancel the action |
| 1746 | + */ |
| 1747 | + public static <T, U> StoppableObservable<U> runAsync(Scheduler scheduler, |
| 1748 | + final Subject<T, U> subject, |
| 1749 | + final Action2<? super Observer<? super T>, ? super Subscription> action) { |
| 1750 | + final SerialSubscription csub = new SerialSubscription(); |
| 1751 | + |
| 1752 | + StoppableObservable<U> co = new StoppableObservable<U>(new Observable.OnSubscribe<U>() { |
| 1753 | + @Override |
| 1754 | + public void call(Subscriber<? super U> t1) { |
| 1755 | + subject.subscribe(t1); |
| 1756 | + } |
| 1757 | + }, csub); |
| 1758 | + |
| 1759 | + csub.set(scheduler.schedule(new Action1<Inner>() { |
| 1760 | + @Override |
| 1761 | + public void call(Inner t1) { |
| 1762 | + if (!csub.isUnsubscribed()) { |
| 1763 | + action.call(subject, csub); |
| 1764 | + } |
| 1765 | + } |
| 1766 | + })); |
| 1767 | + |
| 1768 | + return co; |
| 1769 | + } |
1714 | 1770 | }
|
0 commit comments